Halloween Sale 15% off Ends in See the plans
PHP

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.

Don’t have WPIDE yet? Install it on your site

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

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.