Using two Piwik tracking codes (for two Piwik sites) on one page


#1

Hello there,

I have a website with a main page and a lot of small pages (in subdirectiories) generated by users, using the main page. For each small page I have a separate Piwik site, which lets users track statistics of their pages. I also have a Piwik site (id = 1) for the main page.

What I would like to do, is to track the statistics from all the small pages using the main Piwik site (id = 1), while still making it possible for the users to track the stats of their pages only. I tried to add two Piwik tracking codes to small pages, one with a site id appropriate for the specific small page, and one with site id of 1. Unfortunately, it didn’t work.

Is what I want to achieve possible with Piwik?


#2

To be precise, I wanted to do it like that:


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

	  (function() {
		var u=(("https:" == document.location.protocol) ? "https" : "http") + "://site-name.com/piwik/";
		_paq.push(["setTrackerUrl", u+"piwik.php"]);
		_paq.push(["setSiteId", "<?php echo htmlspecialchars($row['piwikSiteID']); ?>"]);
		_paq.push(['setCookiePath', '/<?php echo $_GET['id']; ?>']);
		var d=document, g=d.createElement("script"), s=d.getElementsByTagName("script")[0]; g.type="text/javascript";
		g.defer=true; g.async=true; g.src=u+"piwik.js"; s.parentNode.insertBefore(g,s);
	  })();
	</script>
	<!-- End Piwik Code -->

	<!-- Piwik -->
	<script type="text/javascript">
	  var _paq2 = _paq2 || [];
	  _paq2.push(["trackPageView"]);
	  _paq2.push(["enableLinkTracking"]);

	  (function() {
		var u=(("https:" == document.location.protocol) ? "https" : "http") + "://site-name.com/piwik/";
		_paq2.push(["setTrackerUrl", u+"piwik.php"]);
		_paq2.push(["setSiteId", "1"]);
		var d=document, g=d.createElement("script"), s=d.getElementsByTagName("script")[0]; g.type="text/javascript";
		g.defer=true; g.async=true; g.src=u+"piwik.js"; s.parentNode.insertBefore(g,s);
	  })();
	</script>
	<!-- End Piwik Code -->

Do You have any idea why doesn’t it work :)?


(Matthieu Aubry) #3

See: JavaScript Tracking Client: API Reference - Matomo Analytics (formerly Piwik Analytics) - Developer Docs - v3


(Tassoman) #4

The previous link is broken. the following is correct: Multiple Piwik Trackers


(Tassoman) #5

Ciao Matt,
I was reading documentation pages linked above about tracking two idSites in the same page but I still can’t understand how.

As far I know the examples provided inside that documentation are correct using the “old style” tracker. In the new javascript code we’re pushing items inside the _paq array so that’s my question: is enough to _paq.push([‘setSiteId’, 6]); twice in the same call?

Maybe we need to duplicate both?
[ul]
[li] _paq.push([‘setSiteId’, 6]);
[/li][li] _paq.push([‘trackPageView’]);
[/li][/ul]


(Matthieu Aubry) #6

I was reading documentation pages linked above about tracking two idSites in the same page but I still can’t understand how.

to track in two site IDs, you must use the SYNC tracker (the old tracker). it’s a bit tricky and not so well documented but should work


(Tassoman) #7

What about NOSCRIPT portion? Should be duped as many times as many idSites we want to track?
Using the old style synced tracker would be deprecated in the next future or will be always supported?
If there are no plans about supporting the synced tracker, would be an idea setting multiple IdSite in the asyncronous tracker?
I mean for example using an object:


_paq.push( { 'setSiteId' : [ 6, 8, 16 ] } ); 


(Matthieu Aubry) #8

If there are no plans about supporting the synced tracker, would be an idea setting multiple IdSite in the asyncronous tracker?

That’s an interesting idea actually! I never thought of that :slight_smile: maybe you could create an issue on the tracker? Issues · matomo-org/piwik · GitHub


(Tassoman) #9

Just opened Issue #7257, sorry for the delay but I was ill in the past days