• Multiple Trackers in Google Analytics & Piwik

    by  • Last modified:October 28, 2012 • Google Analytics, Piwik • 0 Comments

    Using multiple trackers on one web-page can come in handy in many situations. In Google Analytics, for example, it can come in handy when you already have multiple accounts, but you want one account to become a master account. Normally you should use account filters. I would only use a multi-tracker in a situation where there is preexisting data and it’s easier to add a multi-tracker than use filters. Google has the documentation here, but I wanted to give you the full example. Below is the full code you need, just add your account. You can also use this to add as many trackers as you want:

    <script type="text/javascript">
    
      var _gaq = _gaq || [];
    _gaq.push(
      ['_setAccount', 'UA-XXXXX-1'],
      ['_trackPageview'],
      ['b._setAccount', 'UA-XXXXX-2'],
      ['b._trackPageview']);
    
      (function() {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
      })();
    
    </script>
    

    In Piwik Analytics, because there is no such thing as website filters, multi-tracking is the only way to filter data. As a best practice, you should keep one siteid completely unfiltered. This way you can get all the raw unfiltered data in one siteid. The other siteid’s can then be used to block an IP or with PHP code exclude certain types of traffic. Below code will allow you to have one page load write data to three site id’s. Notice the third tracker only fires on the server listed. You can add as many siteids as you like, or modify the code as you wish.

    <!-- Piwik --> 
    <script type="text/javascript">
    var pkBaseURL = (("https:" == document.location.protocol) ? "https://localhost/piwik/" : "http://localhost/piwik/");
    document.write(unescape("%3Cscript src='" + pkBaseURL + "piwik.js' type='text/javascript'%3E%3C/script%3E"));
    </script>
    <script type="text/javascript">
    try {
    var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", 1);
    piwikTracker.trackPageView();
    piwikTracker.enableLinkTracking();
    
    var piwik2 = Piwik.getTracker(pkBaseURL + "piwik.php", 2);//filter
    piwik2.trackPageView();
    piwik2.enableLinkTracking();
    
    <?php
    if ($_SERVER["SERVER_NAME"] = "localhost"){?>
    var piwik3 = Piwik.getTracker(pkBaseURL + "piwik.php", 3);//keep only data from servername specified.
    piwik3.trackPageView();
    piwik3.enableLinkTracking();
    <?php } ?>
    
    } catch( err ) {}
                  
    </script>
    <noscript><p><img src="http://localhost/piwik/piwik.php?idsite=1" style="border:0" alt="" /></p></noscript>
    <!-- End Piwik Tracking Code -->
    

    About

    About The Author: Adrian has over 12 years experience in Digital Marketing and Analytics. He currently works as a Marketing Manager at Vanilla Forums, a modern forum software platform that allows clients to connect and engage their communities and customers. Adrian is certified in Google Analytics and advises the team at Piwik Analytics on their marketing activities. He lives and works in Montreal.