How to use Matomo with AMP with WordPress

Had a hard time setting my self-hosted Matomo analytics with the AMP version of my blog.

Custom JavaScript can not be used, you need to use the pixel tracking or Image Tracking.

Go to your Matomo dashboard settings to retrieve the proper URL and the site ID.

AMP for WordPress

Make sure you're using the official plugin made by Google and Automattic:

AMP

Add the JSON configuration

Go to the WordPress Dashboard and find the Analytics section of the AMP plugin.

It should allow you to set a new JSON for tracking analytics.

Mine looks like this, I'm setting the Matomo site id, the mandatory rec=1 paramater, a random number to prevent caching and the canonical URL.

The cool thing the plugin takes care of the replacing the keyword RANDOM and ${canonicalUrl} with the actual values as the users load the site.

{
    "requests": {
        "pageview": "https://YOUR_SITE/matomo.php?idsite=XXXX&rec=1&rand=RANDOM&url=${canonicalUrl}"
    },
    "triggers": {
        "trackPageview": {
            "on": "visible",
            "request": "pageview"
        }
    }
}

Matomo documentation

There are more things you can pass into the tracking pixel for Matomo, head over the Tracking HTTP API documentation for full details:

https://developer.matomo.org/api-reference/tracking-api

AMP documentation

To further read or possible updated you might want to head to the official docs on how to track pixels using a JSON config:

https://amp.dev/documentation/guides-and-tutorials/optimize-and-measure/configure-analytics/use_cases/#using-amp-pixel

About Rick

Senior Front-end Software Engineer from Barcelona, Haidong Gumdo Instructor (korean martial art of the sword), street photographer, travel lover, TV addict, Boston Red Sox fan, and privacy advocate.

Leave a Reply

Add <code> Some Code </code> by using this tags.

*
*