Matomo tracking pixel for Pure Blog without Javascript
| 2 min read
For a while now I've been using this tracking code that I pieced together for Matomo Analytics and Pure Blog. With some modifications it will also work for websites that run on WordPress, or any website that uses PHP really.
This tracking pixel replaces the default Javascript Tracker Matomo provides. It re-creates most of the functions such as accurate page titles and paths, plus incidental data that comes with any request made to your server.
Why use this instead of the Javascript tracker?
Many crawlers and bots, like AI crawlers, do not use Javascript. On top of that, browsers also allow for blocking javascript trackers more easily as part of privacy protections. My little blog does not truly track people, but I do want to know if people visit my websites and what pages they look at. You know, basic common sense tracking that does not violate anyone's privacy. Using a tracker that does not get blocked by common methods will help me with that.
Setup
To use it, simply change the $matomo_url variable to point to your matomo setup, also make sure the $site_id is set to the correct number. If you have only one website in Matomo this ID is probably 1, check the Matomo tracking code for your Site ID to make sure.
The entire code goes in the footer of your website where you'd normally place the Javascript tracker.
I use this code with a self-hosted setup, but it should work for hosted/cloud configurations as well.
<!-- Start Matomo Code -->
<?php
// Configuration
$matomo_url = "https://your.matomo.setup/";
$site_id = 1;
// End configuration
$ua = isset($_SERVER['HTTP_USER_AGENT']) ? trim($_SERVER['HTTP_USER_AGENT']) : '';
$uri_path = trim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/');
$uri_segments = explode('/', $uri_path);
$is_bot = false;
$is_home_root = ($uri_path === '' || $uri_path === 'index.php');
$is_tag_page = (isset($uri_segments[0]) && $uri_segments[0] === 'tag');
$is_search = (isset($_GET['q']) && $_GET['q'] !== '');
$is_home = ($is_home_root || $is_tag_page || $is_search);
$is_post = (!$is_home && isset($post) && is_array($post) && isset($post['title']));
$is_page = (!$is_home && isset($page) && is_array($page) && isset($page['title']) && !$is_post);
// Figure out where we are
if ($is_post) {
$slug = $post['slug'];
$title = $post['title'];
} elseif ($is_page) {
$slug = $page['slug'];
$title = $page['title'];
} elseif ($is_tag_page) {
$tag_name = isset($uri_segments[1]) ? urldecode($uri_segments[1]) : 'Archive';
$slug = $uri_path; // Keeps the 'tag/tag-name' path intact for the tracker
$title = "Tag: " . ucwords(str_replace('-', ' ', $tag_name));
} elseif ($is_search) {
// Capture the clean search query string for the analytics title
$search_query = trim(strip_tags(htmlspecialchars($_GET['q'])));
$slug = 'search?q=' . urlencode($search_query);
$title = "Search: " . $search_query;
} else {
// Usually the homepage, but also a fallback
$slug = '';
$title = $config['site_title'] ?? 'Home';
}
// Try to detect bots
if (!empty($ua)) {
// A case-insensitive regex for common AI crawlers and bots
$bot_pattern = '/(gptbot|chatgpt|claudebot|claude|perplexity|gpt-|ai-search|ai-crawler|applebot-extended|cohere-ai|meta-externalagent)/i';
if (preg_match($bot_pattern, $ua)) {
$is_bot = true;
}
if (strpos(strtolower($ua), 'curl') !== false && strpos(strtolower($ua), 'ai') !== false) {
$is_bot = true;
}
}
// Set up tracking pixel
$vars = array(
"idsite" => $site_id,
"rec" => '1',
"action_name" => $title,
"url" => get_base_url() . '/' . $slug,
"urlref" => (isset($_SERVER['HTTP_REFERER'])) ? trim($_SERVER['HTTP_REFERER']) : '',
"ua" => $ua,
"rand" => time()
);
if ($is_bot) $vars["recMode"] = '1'; // Is it a bot?
$vars = http_build_query($vars);
?>
<img referrerpolicy="no-referrer-when-downgrade" src="<?php echo $matomo_url; ?>matomo.php?<?php echo $vars; ?>" style="border:0" width="0" height="0" alt="" />
<!-- End Matomo Code -->