Avoid loading piwik.php when only tracking some links

We have a situation where we want to track only certain links with Piwik. (Not entire page.)

We have done this so far by using piwikTracker.trackPageView(); on links with a certain class.

This method works for us, but every time the page is invoked piwik.php loads which causes unnecessary load on our web server. 95% of the time none of our links are clicked (we have an affiliate widget on another large site).

Is there any way we can run Piwik.getTracker(… only when a link is clicked? How do you verify that the tracker have loaded, and how do you make it safe in an asynchronous JS environment. Thanks for the input!

For your use case, what about this?

  • don’t use tracker.enableLinkTracking() or trackPageView()
  • instead, call tracker.addListener( clickable_element )

Let’s say your page contains:


<a href="http://example.com/link" class="trackthis">Track This Link</a>
<a href="http://example.org/link" class="trackthis">And this one</a>
<a href="http://example.net/link">But not this one</a>

For async JS (with jquery), you could use something like:


$('.trackthis').each( function( index, element ) {
  _paq.push( ['addListener', element] );
});

Hi vipsoft! Thank you for the feedback.

We are currently not using .enableLinkTracking() or trackPageView();
We have commented those lines out of the tracking code.

I am worried about the following line in the tracking code:


var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", 4);

Is the PHP file loaded automatically (thus creating load on the Piwik install) or only when a tracking method is invoked? (Regardless of which tracking method) As I said we are on a very high traffic site and do not wish to cause unnecessary load on our Piwik isntall when only 1% of the sites visitors click any of our links.

getTracker just instantiates an object. There’s no piwik.php request until one of the track*() methods is called.