Matomo Analytics (Self-Hosted or Cloud)
Adds the Matomo tracking code to every public page, with optional cookieless tracking and without counting logged-in staff.
Opens WPIDE on
Code
add_action( 'wp_head', function () {
$matomo_url = ''; // For example 'https://analytics.example.com/' or 'https://yourname.matomo.cloud/'.
$site_id = 1; // Matomo site ID.
$cookieless = true; // true: no tracking cookies are set.
if ( '' === $matomo_url || current_user_can( 'edit_posts' ) ) {
return;
}
?>
<script>
var _paq = window._paq = window._paq || [];
<?php if ( $cookieless ) : ?>
_paq.push(['disableCookies']);
<?php endif; ?>
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function () {
var u = <?php echo wp_json_encode( trailingslashit( esc_url_raw( $matomo_url ) ) ); ?>;
_paq.push(['setTrackerUrl', u + 'matomo.php']);
_paq.push(['setSiteId', <?php echo wp_json_encode( (string) (int) $site_id ); ?>]);
var g = document.createElement('script');
g.async = true;
g.src = u + 'matomo.js';
document.head.appendChild(g);
})();
</script>
<?php
} );
Notes
Set $matomo_url to your Matomo address (with a trailing slash) and $site_id. Nothing is output while $matomo_url is empty.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.