Matomo server side tracking with Cloudflare

My first post here.

I enabled Matomo server-side tracking with PHP. It works and the page load speed is much faster than using javascript. However, when enabled, it seems Cloudflare is caching requests, and traffic drops increasingly as more pages are cached.

How can I force Cloudflare not to cache Matomo? I searched these and other forums and only found this:
https://community.cloudflare.com/t/analytics-does-not-work-properly-with-cloudflare-apo/270403

Here’s the code I was using:

<?php
// Required variables
$matomoSiteId = 1;                  // Site ID
$matomoUrl = "https://analytics.mydomain.com"; // Your matomo URL
$matomoToken = "removed";                  // Your authentication token

// Optional variable
$matomoPageTitle = "";              // The title of the page

// Load object
require_once("MatomoTracker.php");

// Matomo object
$matomoTracker = new MatomoTracker((int)$matomoSiteId, $matomoUrl);

// Set authentication token
$matomoTracker->setTokenAuth($matomoToken);

// Track page view
$matomoTracker->doTrackPageView($matomoPageTitle);
?>

Thanks for your help.

Assuming you do the serverside tracking inside the page code itself the only thing you can do is to not do any caching on the pages. An alternative way of serverside tracking is requesting a seperate “page” that does the server side tracking and exclude that URL from caching in cloudflare.

1 Like

Thanks for replying.

The above code I have pasted in the header template (so it’s on all pages).

Based on what you stated, if I tell Cloudflare to ignore MatomoTracker.php and PiwikTracker.php URLs paths. Would that solve the issue? Or is it more complex than adding ignore-cache page rules?

If the page is cached by Cloudflare… then your server doesn’t even know the page has loaded.

That’s exactly the point in having a CDN!!

You simply cannot have both the benefits of PHP server side tracking AND the benefits of a CDN like Cloudflare.

Do server side tracking on your server, or client side with Cloudflare.

IMO there’s no reliable alternative here.

Except for Cloudflare Analytics (that’s essentially based upon Cloudflare’s logs) but that’s another thing.

1 Like