≡ Menu

Using 2 or more Piwik installations to track one website

Piwik transitions report

First, you only have to load one instance of piwik.js regardless of the number of trackers you want to use. In the multiple Piwik trackers example below, we load the single instance from server-1.com/piwik.js.

Next, we need to create 2 instances of Piwik.getTracker: piwikTracker1 and piwikTracker2. Then we call the trackPageView with each tracker to register a page view with each Piwik installation.

Here’s how I implemented two Piwik installations to track one website. If you’re still using Piwik version prior to 2.3 then use Method 1, otherwise use the newer asynchronous javascript loading Method 2.

Method 1: Synchronous Tracker Object

<script type="text/javascript" src="http://server-1.com/piwik.js"></script>
<script type="text/javascript">
  try {
    var piwikTracker1 = Piwik.getTracker("http://server-1.com/piwik.php", 1);
    piwikTracker1.trackPageView();
    var piwikTracker2 = Piwik.getTracker("http://server-2.com/piwik.php", 1);
    piwikTracker2.trackPageView();
  } catch( err ) {}
</script>

Method 2: Asynchronous Tracker Object (Piwik 2.3 and later)

<script type="text/javascript">
  var _paq = _paq || [];
  _paq.push(['trackPageView']);
  _paq.push(['enableLinkTracking']);
  (function() {
    var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
    g.type='text/javascript';
    g.async=true;
    g.defer=true;
    g.src='http://server-1.com/piwik.js';
    s.parentNode.insertBefore(g,s);
  })();
  window.piwikAsyncInit = function () {
    try {
      var piwikTracker1 = Piwik.getTracker("http://server-1.com/piwik.php", 1);
      piwikTracker1.trackPageView();
      var piwikTracker2 = Piwik.getTracker("http://server-2.com/piwik.php", 1);
      piwikTracker2.trackPageView();
    } catch( err ) {}
  };
</script>

References:
1. Piwik Web Analytics Essentials by Stephan A. Miller
2. http://developer.piwik.org/guides/tracking-javascript-guide

{ 0 comments… add one }

Leave a Comment