Do I need to include all methods in setup tracking code piwik?

347 views Asked by At

I'm setting up piwik to track web analytics (for the first time) and I'm inserting the following initial tracking code provided by PIWIK:

          <!-- Piwik -->
<script type="text/javascript">
    var _paq = _paq || [];
    (function(){ var u=(("https:" == document.location.protocol) ? "https://url/" : "http://url/");
    _paq.push(['setSiteId', 2]);
    _paq.push(['setTrackerUrl', u+'piwik.php']);
    _paq.push(['trackPageView']);
    _paq.push(['enableLinkTracking']);
    var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.type='text/javascript'; g.defer=true; g.async=true; g.src=u+'piwik.js';
    s.parentNode.insertBefore(g,s); })();
</script>
<!-- End Piwik Code -->

my question here is do I need to include after _paq.push(['enableLinkTracking']); all of the methods I will be using like:

_paq.push(['setCustomVariable'])
_paq.push(['trackEvent'])

and so on? or can I leave the script as it is provided by PIWIK and all the other methods will work?

2

There are 2 answers

0
pchmn On BEST ANSWER

The tracking code that Piwik gave you, you need to insert it in all your pages you want to track, and you should leave it as it is provided.

The code for customVariable and trackEvent, you need to insert it in the specific html elements you want to track, and not in the tracking code !

As an exemple you have an index.html page you want to track, and in this page you want to add an EventTracking on a specific link. This is what you need to do ; at the bottom of your body in index.html add this tracking code :

<!-- Piwik -->
<script type="text/javascript">
    var _paq = _paq || [];
    (function(){ var u=(("https:" == document.location.protocol) ? "https://url/" : "http://url/");
    _paq.push(['setSiteId', 2]);
    _paq.push(['setTrackerUrl', u+'piwik.php']);
    _paq.push(['trackPageView']);
    _paq.push(['enableLinkTracking']);
    var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.type='text/javascript'; g.defer=true; g.async=true; g.src=u+'piwik.js';
    s.parentNode.insertBefore(g,s); })();
</script>
<!-- End Piwik Code -->

You don't have to put the trackEvent function in this tracking code because you will insert it directly in the link element of your page. So in the link element you want to add an EventTracking, you will add this code :

<a href="#" onclick="javascript:_paq.push(['trackEvent', 'Documentary', 'Play', 'Thrive']);"> Link </a>

For custom variables you need to act the same way !

Hope it will help

0
Gonerator On

If you want to use custom vars and event tracking you can just call the methods of tracking these later in the code itself, leaving initial tracking code intact.

if you were to use them there, then they would track custom var or event already, if the parameters were provided