How do you correctly track metrics in multiple websites in Piwik 3.0.1?

I recently installed Piwik 3.0.1 to track a large(ish) organization’s intranet pages. I need to track metrics for the different teams’ pages in different websites (all on the same Piwik instance), so I can assign users per team to view their team’s data. I also need to be able to see all the data in a “global” website, so I can track usage overall.

I have tried multiple approaches, but can’t get it to work. Here’s what I tried.

Multiple trackers on a single page

Ideally (with the CMS we use) I’d like to use 2 different trackers: one “specific” tracker for the team (which is at the top of all their pages), and one “global” tracker (which is at the bottom of all pages). I’m using the Javascript tracker as it is generated by Piwik.

While both trackers seem to track Visits, Visitors, Page Views, Devices, Software, and so on, Downloads and Outlinks only get tracked through the bottom (i.e., last) tracker. So that’s not a valid solution.

One other possible solution I thought could work was to increase the delay for the link tracking (which would impact both Downloads and Outlinks). I tried by adding the delay as follows:

var _paq = _paq || [];
_paq.push(['trackPageView']);
_paq.push(['setLinkTrackingTimer', 750]); // increase to 750 milliseconds
_paq.push(['enableLinkTracking']);
(function() { ... })();

That did not work though.

Single tracker pushing data to 2 sites

I then tried configuring a single tracker to push the data into two sites (as described here). This is the code I used:

<script type="text/javascript">
	var _paq = _paq || [];
	_paq.push(['trackPageView']);
	_paq.push(['enableLinkTracking']);
	(function() {
		var u = "//my.server.com/piwik/";
		_paq.push(['setTrackerUrl', u + 'piwik.php']);
		_paq.push(['setSiteId', '24']);
		var websiteIdDuplicate = 25;
		_paq.push(['addTracker', piwikUrl = null, websiteIdDuplicate]);
		var d = document,
			g = d.createElement('script'),
			s = d.getElementsByTagName('script')[0];
		g.type = 'text/javascript';
		g.async = true;
		g.defer = true;
		g.src = u + 'piwik.js';
		s.parentNode.insertBefore(g, s);
	})();
</script>

When I add this code to a page, all the metrics get tracked into the first website (SiteId 24), but no data at all is tracked in the 2nd one (SiteID 25). So in a way this is worse than the multiple trackers.

Synchronous vs. Asynchronous Trackers

Some research (FAQ, User Guides, Forums) seems to indicate this is because the trackers run asynchronously, and that synchronous trackers should be used. I don’t know how to do that though, do I simply need to set:

g.async = false;

and possibly also:

g.defer = false;

Would that be enough?

Also, I’m not entirely sure anymore if synchronous trackers are still supported with 3.x, and what the differences (drawbacks) are to using a synchronous tracker instead of an asynchronous one. If someone could clarify this as well that would be good for my own mental sanity.

My question

So, my “big” question is: how do I correctly setup the tracker(s) so that I can track the metrics in multiple websites? Any help is appreciated, as I’ve been struggling with this for the better part of 3 days now, and I just can’t figure it out.

I’m in the same situation: I need to send track metrics to multiples websites hosted by the same piwik instance and… I’m not able to do it :disappointed_relieved:

I’ve tried same approaches than you did with no success at all.

The only thing that remains to try is tracking with image: it will require to collect as much params as I could by javascript and… I’m so lazy…:pensive:

I’ll try to find time to try with image tracking and I’ll back here if I success.

Finally, I could workarround the problem using a variant of the js used to send hits to several “piwiks”.
The code follows:

<script type="text/javascript">
	  var _paq = _paq || [];
	  _paq.push(['trackPageView']);
	  _paq.push(['enableLinkTracking']);

	  (function() {
		var u="//piwik.mysite.com/analytics/";
		_paq.push(['setTrackerUrl', u+'piwik.php']);
		_paq.push(['setSiteId', '10']);
		
		var secondaryTracker = 'http://piwik.mysite.com/analytics/piwik.php';
		var secondaryWebsiteId = 11;
		_paq.push(['addTracker', secondaryTracker, secondaryWebsiteId]);

		var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
		g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);
	  })();
</script>
1 Like

My hero :slight_smile:

I just tested this (briefly), and it seems to work perfectly. Thank you for not giving up, and sharing your code!

There is one thing I would probably change: for your secondaryTracker you specify http://, whereas the first tracker just uses //. I think that just using // may a better solution, as then it doesn’t matter whether your Piwik server is http or https.

You are right: it is a better solution to use a protocol independent URL por the second tracker, definitely.

In case you are doing this to get aggregate over multiple websites or so, maybe the plugin RollUpReporting will be useful: http://plugins.piwik.org/RollUpReporting

Yes, I know about that plugin but, indeed, I only need to feed multiple websites during a “not so long” period of time: I’m running a performance test on Piwik in order to know its limits and its scalability.

Thank you anyway.