Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

3. Tracking Visitors with Piwik > Using multiple trackers

Using multiple trackers

If you ever need to add more than one Piwik tracker per page, that is not a problem. You can do it and load piwik.js one time as it helps speed up page loading time. This works because each call to Piwik.getTracker will return a unique tracker object. It works even if each Piwik tracker is pointing at a different server. Here is an example of this in action:

<script type="text/javascript">
var protoCol = ('https:' == document.location.protocol ? 'https://' : 'http://');
document.write(unescape("%3Cscript src='" + protoCol + "URL_1/piwik.js' type='text/javascript'%3E%3C/script%3E"));
</script><script type="text/javascript">
try {
var piwikTracker1 = Piwik.getTracker(protoCol + URL_1/piwik.php", 1);
piwikTracker1.trackPageView();
var piwikTracker2 = Piwik.getTracker(protoCol + URL_2/piwik.php", 4);
piwikTracker2.trackPageView();
} catch( err ) {}
</script>

This time the first part of the script only creates a protoCol variable instead of a complete base URL, so that http:// or https:// can be appended appropriately to either URL that we will be getting a Piwik tracker from.

Since we only need to use one instance of piwik.js, we just grabbed this from the first Piwik URL where URL_1 represents this URL. We could just as easily have included piwik.js from URL_2. An even smarter option when it comes to saving load time is to download piwik.js from one of the Piwik installations and upload it to the site we are tracking, so that it can be included in the pages from a local file.

Then we created two instances of Piwik.getTracker, calling one piwikTracker1 and the other piwikTracker2. We set the Piwik tracker URL and website ID for each when we create the trackers. Then call trackPageView with each tracker to register a page view with each Piwik installation.

You can also set the Piwik tracker URL and website ID of a Piwik tracker instance after it has been created instead of at its instantiation, due to the flexibility of the Piwik API.

var piwikTracker = Piwik.getTracker();
piwikTracker.setSiteId(2);
piwikTracker.setTrackerUrl("http://mysite.com/piwik/");
piwikTracker.trackPageView();

Here, we replaced Piwik.getTracker("http://mysite.com/piwik/", 2); with four lines of code. It is more code, but it may help with readability while learning how Piwik works.

We mentioned downloading the piwik.js file from your Piwik installation and uploading it to your tracked sites for local inclusion. This eliminates one more DNS lookup a visitor's browser has to do and can speed up load time. Another thing that can speed up load time when using Piwik's tracking code is asynchronous tracking.

  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint