Tracking into 3 different Matomo Servers / Websites

Hey there,

I know I can track to another Matomo Server with “addTracker” … but how is it possible to track into 3 different Matomo Servers (or Websites within 1 or 2 Matomo Servers)?

When “addTracker” is used more than once - Console: The method addTracker is registered more than once in “_paq” variable. Only the last call has an effect. Please have a look at the multiple Piwik trackers documentation: JavaScript Tracking Client: Integrate - Matomo Analytics (formerly Piwik Analytics) - Developer Docs - v3

thx
Andreas Schnederle-Wagner

in case someone questions the usecase:

we got some JS Scripts which inject 360° Panoramic Images into our Customers Websites, also we got some JS Scripts which inject Maps with those Panoramic Images into their Websites.

Example:

  • User A already got a Matomo Tracker on their Page - tracking their Stats.
  • They include our Pano Images JS - I add another Piwik Server with “addTracker”
  • They also include out Maps JS - I need to add just another Tracker to our Piwik Server with another Website ID …

:wink:

btw) is it possible to “setDocumentTitle” per Tracker? As I want to add something for the “viewer” and another Info to the “map” tracking … also the trackPageView should only be executed for our newly added Tracker - and not all 2 or 3 Instances?!? (according to Matomo Blog: Say you call _paq.push([‘disableCookies’]); _paq.push([‘trackPageView’]);, then both methods will be called on all tracker instances assuring they will behave the same and will track the same data into both Matomo websites.)

thx

You can call “addTracker” multiple times to track into as many instances as you want. In the past there was a notice logged when calling this method multiple times but it has been fixed in the latest beta AFAIK

1 Like

hey @thomas_matomo,
thx for your fast answer to this … because of the console notice I thought I may only call it once … :wink:

Anyone got a Hint for the “setDocumentTitle” per Tracker Instance Question? :slight_smile:

bye from Austria
Andreas

You can try something like window.piwikAsyncInit = function () {Piwik.getAsyncTracker(piwikurl, idsite).setDocumentTitle('...'); }; :slight_smile:

Actually you want to likely make this something like this:

window.piwikAsyncInit = function () {

Piwik.on('TrackerSetup', function (tracker) {
    if (tracker.getIdSite() == '...' && tracker.getTrackerUrl() == '...' ) {
        tracker.setDocumentTitle('...');
    }
});

Not tested though.

just tried it with your latest Code Snippet - but “tracker.getTrackerUrl()” is always empty in that case sigh

http://in.futureweb.at/temp/matomo.php

seems like it’s working like that: :slight_smile:

window.piwikAsyncInit = function () {
    var tracker1 = Piwik.getTracker('//stats.futureweb.at/piwik.php', 1082);
    var tracker2 = Piwik.getTracker('//stats.futureweb.at/piwik.php', 1082);

    tracker1.setDocumentTitle('Viewer');
    tracker2.setDocumentTitle('Map');
    
    tracker1.trackPageView();
    tracker2.trackPageView();
};

ok wasn’t that easy … as piwikAsyncInit got overwritten by seconds/third include of JS scripts for different Panos on same Page … but I guess now I got a working solution with window.onload Functions, Functions for piwikAsyncInit calls and so on, …

thx

In case someone is interested how I solved it / got same Usecase - I made a quick Test-Page with my implementation here: Multiple Matomo Testcase

  • On Page Footer there are 2 Matomo Trackers added by CMS
  • Within the viewer.js.php the Tracker for the Viewer is added
  • Within map.js.php the Tracker for the Map is added

If there already is an existing Matomo Object - I use it and simply add my own with getTracker()
If there is no Tracker - I load Matomo JS and create one

Actions within Viewer/Map are also tracked in the respective tracker …

So I guess this implementation should work regardless if there is Matomo used on the Site where my Panos get loaded or not …

Hope I haven’t overlooked something … :wink:

Hey,

Normally this code can help you if you want execute something ONLY after Piwik is loaded :

var d=document,
g=d.createElement('script'),
s=d.getElementsByTagName('script')[0],
r = false;
g.type='text/javascript';g.async=true;g.defer=true;g.src=u+'.js';
							
g.onload = s.onreadystatechange = function(){
	console.log(this.readyState);
	if( !r && (!this.readyState || this.readyState == 'complete' ) ){
		r = true;
		console.log('Piwik init ?', Piwik.initialized);
	}
}

Hey @Yann_Poirot,
if I’m in control for loading the JS - this would be a nice approach - but my Scripts can be included in 3rd Party Websites where I have no control what and when they load it … sometimes they Load Matomo in the Header (so before my Script is included), sometimes they Load it in the Footer of the Page (after my Script is included) and sometimes there is no Matomo at all :wink:

So it won’t work with that approach I guess :-/