Getting started: where do we add the custom code?

Hey there folks :slightly_smiling_face:

I am new to Matomo and was wondering if someone could let me know where we add the custom code snippets that are described in the docs? For instance where should I add the following snippet, for it to work properly?

custom
_paq.push([‘setDocumentTitle’, document.domain + “/” + document.title]);
_paq.push([‘trackPageView’]);

general code

<!-- Matomo -->
<script type="text/javascript">
  var _paq = window._paq || [];
  _paq.push(['trackPageView']);
  _paq.push(['enableLinkTracking']);
  (function() {
    var u="//{$PIWIK_URL}/";
    _paq.push(['setTrackerUrl', u+'matomo.php']);
    _paq.push(['setSiteId', {$IDSITE}]);
    var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
    **g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);**
  })();
</script>
<!-- End Matomo Code -->

thanks in advance :slight_smile:

Hi Linda,

That first line in the snippets which changes the data that you want to send to Matomo for a pageview should go directly before _paq.push([‘trackPageView’]); as is already show in the second line of your custom snippet. Just make sure you don’t get 2 lines that say push trackPageView as that will lead to double counting your pageviews in Matomo.

Also be aware that if you want to set your own values for dimensions and you get those values from the DOM (like document.domain and document.title) then the specific element you want to use to get the value from has to be already loaded by the browser for your code to work. In this specific instance that will be no problem as both the domain and title are pretty much guarantueed to be inspectable before the Matomo script does it’s thing.

1 Like

Thanks a mil Matsi :slight_smile: