The complete guide on tracking your websites and web apps into multiple Matomos and how to do it easily & efficiently

Contents

Getting the tracking of your website and apps right is crucial to your success as you need to ensure the measured data is meaningful and correct. That’s why we, at InnoCraft, help our clients setting up their web tracking and digital measurement strategy. Some challenges include tracking your analytics data into multiple Matomo (Piwik) services as well as the tracking of single-page websites and web applications (covered in a previous article). In this blog post, we explain how to track your data into multiple Matomo websites correctly.

Embedding the tracking code

First of all you need to embed your JavaScript tracking code into your website or app as usual. If you haven’t done this yet: Log in to your Matomo, click on “Administration” in the top right and go to “Tracking Code”. There you have various options to adjust your tracking code to your needs.

Tracking the same data into different websites

Let’s assume you have set up the regular JavaScript tracking code and you want to track the same data into a second Matomo website. This second Matomo website can be either on the same Matomo installation or on a different Matomo. To do this, add the following line to your tracking code:

_paq.push(['addTracker', 'https://$yourPiwikDomain/piwik.php', idSite]);

It should look like this:

var u = '//$yourPiwikDomain';
_paq.push(['addTracker', u + '/piwik.php', 2]); // adds an additional tracker with siteId = 2 
_paq.push(['setSiteId', '1']); // configures your regular Matomo tracker
_paq.push(['setTrackerUrl', u + 'piwik.php']);

This will track the same data into website 1 and website 2 of your Matomo installation. You can also change the domain in addTracker to point it to a different Matomo installation:

_paq.push(['addTracker', '//$differentPiwikDomain/piwik.php', 2]);

All Matomo tracker methods that you call afterwards will be applied to all trackers. 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.

Tracking different data into different websites

If you want to track only certain data into one website, and different data into an additional website, you need to configure the trackers differently. For example, you want to enable link tracking only for one tracker, but not for the other. The problem is that calling _paq.push(['enableLinkTracking']); enables link tracking on all of your trackers. To workaround this limitation, you can configure your trackers differently like this:

window.piwikAsyncInit = function () {
    Matomo.on('TrackerSetup', function (tracker) {
      if (tracker.getSiteId() == 2 
         || tracker.getTrackerUrl() === '//$yourPiwikDomain/piwik.php') {
          tracker.enableLinkTracking();
	}
    });
};

Now it enables link tracking only for the tracker that is configured for a certain website ID or Matomo domain.

Accessing a previously generated tracker instance

When you configure a tracker via _paq.push, you create a so called “Async tracker” because Matomo will be loaded asynchronously and create the tracker instance as soon as it is loaded. If you need to get the instance of such a tracker, you can use the method Piwik.getAsyncTracker(trackerUrl, idSite). This can be useful if you have a single-page website and want to track different data into different websites:

window.addEventListener('hashchange', function() {
    if ('undefined' === typeof Matomo) {
        // Matomo might not be loaded yet
        return;
    }
    var tracker1 = Piwik.getAsyncTracker('//$yourPiwikDomain/piwik.php', var idSite = 1);
    var tracker2 = Piwik.getAsyncTracker('//$yourPiwikDomain/piwik.php', var idSite = 2);
    tracker1.setCurrentUrl('/' + window.location.hash.substr(1));
    tracker2.setCurrentUrl('/mywebsite/' + window.location.hash.substr(1));
});

Tracking different data into multiple Matomo installations without using “_paq”

Some users prefer to not use _paq.push and instead directly create tracker instances themselves using the method Piwik.getTracker(trackerUrl, idSite) like this:

window.piwikAsyncInit = function () {
    var tracker1 = Piwik.getTracker('//$yourPiwikdomain/piwik.php', var idSite = 1);
    tracker1.disableCookies();
    var tracker2 = Piwik.getTracker('//$yourPiwikdomain/piwik.php', var idSite = 2);
    tracker2.enableLinkTracking();

    tracker1.trackPageView();
    tracker2.trackPageView();
};

We usually don’t recommend creating trackers manually as it is more complicated and you need to make sure to configure trackers in the right order. For example to prevent the setting of any cookies, it is recommended to call disableCookies before calling any other methods. If you want to create your trackers manually and you use any of the following methods, make sure to call them in this order:

disableCookies(), setAPIUrl(), enableCrossDomainLinking(), setCookiePath(), setCookieDomain(), setDomains(), setUserId(), enableLinkTracking()

Roll-Up Reporting – the easy and efficient way

Often users track data into multiple websites because they need aggregated data over all their websites. They want to see all statistics for a single website, but also which pages were viewed across all their websites, or how much traffic they got from a specific website or search engine across all websites. This means they add a second tracker to all their websites and track data not only into the regular Matomo website, but also into one additional website that gives them statistics over all websites. This has several disadvantages:

  • Complexity in getting the tracking code right and the time needed to integrate and maintain it
  • Slower website performance because everything needs to be tracked into several websites. This can decrease your conversions and sales
  • Slower Matomo performance because it has to handle twice as much traffic. This means tracking becomes slower, generating the report becomes slower, and the database gets twice as big

Luckily, there is a better solution called Roll-Up Reporting. With Roll-Up Reporting, you can get aggregated data over all websites and / or for a group of websites without any of these disadvantages. It lets you create as many Roll-Ups as you wish and you can choose which websites’ data should be aggregated together into a new website.

We had customers who were able to remove one Matomo tracker because of this feature which resulted in less server costs, a faster website, and a faster Matomo. On top of all these advantages, it also lets you view the Visitor Log, Real-time Map, and other widgets and reports across several websites.

Questions?

If you got any questions, please let us know and get in touch with us. You can find more information about the Matomo JavaScript tracker on the Matomo Developer Zone. There is a section dedicated to Multiple Matomo Trackers.

Enjoyed this post?
Join the 160,000+ subscribers who receive the Matomo Newsletter straight to their inbox every month

Subscribe to our newsletter to receive regular information about Matomo. You can unsubscribe at any time from it. This service uses MadMimi. Learn more about it within our privacy Policy page.

Get started with Matomo

A powerful web analytics platform that gives you and your business 100% data ownership and user privacy protection.

No credit card required.

Free forever.

Get started with Matomo

A powerful web analytics platform that gives you and your business 100% data ownership and user privacy protection.

No credit card required.

Free forever.