Matomo analytics better example with custom tracking options

Hello,
I have implemented the basic code but I want to add some additional options and don’t know if this is the correct way of doing it.

I didn’t see anything with regards to having to enable / disable the tag manager for privacy purposes. I am assuming that it simply remains active.

<script>
  var _mtm = window._mtm = window._mtm || [];
  _mtm.push({'mtm.startTime': (new Date().getTime()), 'event': 'mtm.Start'});
  (function() {
    var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
    g.async=true; g.src='https://mydomain.net/piwik/js/container_fAREuJHL.js'; s.parentNode.insertBefore(g,s);
  })();
</script>

I am using this in Php through a CMS and using the cookie that is set if the user accepts or rejects below:

	<script>
	  var _paq = window._paq = window._paq || [];
	  /* tracker methods like "setCustomDimension" should be called before "trackPageView" */
	  _paq.push(['setSecureCookie', true]);
	  _paq.push(['setVisitorCookieTimeout', '604800']);
	  _paq.push(['rememberCookieConsentGiven']);
	  _paq.push(['setDownloadExtensions', "jpg|pdf"]);
	  _paq.push(['trackPageView']);
	  _paq.push(['enableLinkTracking']);
	  (function() {
		var u="//mydomain.net/piwik/";
		_paq.push(['setTrackerUrl', u+'matomo.php']);
		_paq.push(['setSiteId', '1']);
		var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
		g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);
	  })();
	</script>

and for the opt out:

	<script>
	_paq.push([function () {
		_paq.push(['optUserOut']);
		_paq.push(['forgetCookieConsentGiven']);
	}])
	</script>

I look forward to your answers.
Thanks in advance.

1 Like