# 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.

- Type: PHP
- Runs: Frontend only
- Category: [Analytics & Tracking](https://wpide.com/library/category/analytics/)
- Tags: matomo, analytics, privacy, tracking
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/analytics-matomo/

## Code

```php
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 use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Matomo Analytics (Self-Hosted or Cloud)" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
