Matomo Server-Side tracking with Google Tag Manager

Updated: Tuesday, August 13, 2024

Why Matomo in server-side?

Setting up Matomo in server-side with Google Tag Manager gives you a user-friendly working environment and all the benefits of server-side.

Bypassing ad blockers

If you’re already using Matomo in client-side, you may have noticed that ad blockers block requests that include the text matomo.

Setting up Matomo as a server-side application enables you to bypass ad blockers and collect a greater amount of data on your users.

Reduced site loading time

Loading a single browser-side script reduces the loading time of your website’s pages. By the way, this is an important metric used by Google to rank pages in search results.

Data governance

In large organizations, mastering data flows is essential to comply with security standards and legislative constraints.

When you install scripts directly in the browser (client-side), marketing platforms can collect what they want, and you have no control over it. In a server-side configuration, the data first passes through your server before being sent to the platforms. You’re in control of the flow, and can manipulate it as you see fit.

Two ways to send data to your server

With GA4 collection

flowchart LR
    subgraph Your website
        A[GA4 collection]
    end
    subgraph Your server
        A --> B
        B[GA4 client] --> C[Matomo tag]
        B --> D[Google Ads tag]
        B --> E[Tag GA4]
    end
    C --> F[Instance Matomo]
    D --> G[Google Ads]
    E --> H[GA4]

=> If you wish to use a GA4 collection, please follow the Addingwell documentation.

With a Matomo collection

flowchart LR
    subgraph Your website
        A[Matomo collection]
    end
    subgraph Your server
        A --> B
        B[Matomo client] --> C[Matomo tag]
    end
    C --> D[Instance Matomo]

If you’d like to use a Matomo collection, you can read this documentation.

Matomo client

Import

Click here to download the Matomo client.

Go to Google Tag Manager Server-Side and then to the Templates section.

Create a new client template in Google Tag Manager Server-Side
Create a new client template in Google Tag Manager Server Side

Then import the file you uploaded earlier.

Import Matomo client
Import Matomo client

Then click Save in the top right-hand corner.

Imported Matomo client
Imported Matomo client

Configuration

In the Clients section, click on New.

Select the Matomo client.

Matomo client configuration
Matomo client configuration
  1. Priority: if you use several clients, the client with the highest priority runs first on a given request. As a reminder, a request can only be claimed by a single client.

  2. Request Path: the path you will call when sending a Matomo request to your server. The default path is matomo.php, but it is strongly recommended that you change it to bypass ad blockers.

  3. JavaScript Library Path : the path you’ll call to deliver the Matomo tracking script. The default path is matomo.js but it is strongly recommended that you change it to bypass ad blockers. Cache time: 8 min.

  4. Allowed origins: list here the domain names that are allowed to call your server. This prevents another website from using your server to load the Matomo JavaScript library, for example.

For the example, I’ll choose abc as Request Path and def for Matomo’s JavaScript Library Path.

Matomo client configured
Matomo client configured

Configure collection on WEB (client-side)

Import

Go to the Templates section of Google Tag Manager WEB. Then click on Search Gallery in Tag Templates.

Tag search button in Google Tag Manager WEB gallery
Tag search button in Google Tag Manager WEB gallery

Import the Matomo Analytics - Configuration tag from Data Marketing School.

Data Marketing School's Matomo Analytics tag import from Google Tag Manager WEB gallery
Data Marketing School's Matomo Analytics tag import from Google Tag Manager WEB gallery

Configuration

On your website, before the Google Tag Manager code, you need to add the Matomo initialization code between the <head> tags.

<head>
    <!-- BEGIN MATOMO SCRIPT -->
    <script>
    var _paq = window._paq = window._paq || [];
    (function() {
        var u="https://your_subdomain.example.com/"; // your server URL
        _paq.push(['setTrackerUrl', u+'abc']); // Request Path configured in Matomo client
        _paq.push(['setSiteId', '1']); // Your Site ID
        var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
        g.async=true; g.src='https://your_subdomain.example.com/def'; s.parentNode.insertBefore(g,s); // JavaScript library path configured in Matomo
    })();
    </script>
    <!-- END MATOMO SCRIPT -->
    <!-- REPLACE ME: ADD GTM CODE HERE -->
</head>

In Google Tag Manager WEB, go to the Tags > New section. Then select the previously imported tag.

Configuring the Matomo Analytics tag - Configuration in Google Tag Manager WEB
Configuring the Matomo Analytics tag - Configuration in Google Tag Manager WEB
  1. Setup Type: select Server-Side.

Trigger

This tag should be triggered as soon as possible in your container. We’ll therefore use the Initialization - All Pages trigger.

Configuration of Matomo Analytics trigger - Configuration in Google Tag Manager WEB
Configuration of Matomo Analytics trigger - Configuration in Google Tag Manager WEB

Server-side Matomo Analytics tag

Import

Go to the Templates section of Google Tag Manager Server-Side. Then click on Search Gallery in Tag Templates.

Tag search button in Google Tag Manager Server-Side Gallery
Tag search button in Google Tag Manager Server-Side Gallery

Import the Matomo Analytics tag from Data Marketing School.

Data Marketing School's Matomo Analytics tag import from Google Tag Manager Server-Side gallery
Data Marketing School's Matomo Analytics tag import from Google Tag Manager Server-Side gallery

Configuration

Go to the Tags > New section. Then select the previously imported tag.

Configuring the Matomo Analytics tag in Google Tag Manager Server-Side
Configuring the Matomo Analytics tag in Google Tag Manager Server-Side
  1. Client Type: select Matomo Client.
  2. Matomo Instance URL : the URL of your Matomo instance (usually in the form <your_subdomain>.matomo.cloud).
  3. Auth Token: the authentication token in Matomo. Help to find it
  4. Site ID: the Matomo site identifier. If you leave this field empty, the site ID sent by the Matomo Analytics - Configuration tag in your WEB container will be used. Help to find it

Triggering

On the server side, the tag must be triggered for all Matomo client requests. We’ll therefore use a Custom trigger.

Client Name - Matomo trigger configuration
Trigger configuration Client Name - Matomo

Didn't find what you were looking for?

Get help from the Data Marketing Club

Join the Data Marketing Club