Antlytics logoAntlytics
← Blog
5

WordPress Analytics Without a Plugin

A script tag in your theme is simpler than a plugin. Here's how to add privacy-friendly analytics to WordPress without installing a plugin.

WordPress Analytics Without a Plugin

WordPress has a plugin for everything. But for analytics, a script tag in your theme is often simpler, faster to install, and less prone to conflicts than a plugin.

Why skip the plugin

WordPress plugins add code to your site on every page load — not just the pages that need them. A well-maintained plugin is fine, but many WordPress analytics plugins:

A direct script tag in your theme:

Method 1: Theme editor (simplest)

  1. In your WordPress admin, go to Appearance → Theme Editor (or Appearance → Theme File Editor in newer WordPress versions).
  2. Open your theme's header.php file.
  3. Find the </head> closing tag.
  4. Add the tracking snippet immediately before </head>.
<?php /* Antlytics — get your tracking ID from https://www.antlytics.com */ ?>
<script>
(function(){
  var t="YOUR_TRACKING_ID",
      u="https://www.antlytics.com/api/ingest/pageview",
      k="ant_sid";
  function sid(){
    try{var s=sessionStorage.getItem(k);if(s)return s;
    s=crypto.randomUUID();sessionStorage.setItem(k,s);return s;}
    catch(e){return crypto.randomUUID();}
  }
  function send(){
    fetch(u,{method:"POST",headers:{"Content-Type":"application/json"},
    keepalive:true,body:JSON.stringify({tracking_id:t,
    pathname:location.pathname,referrer:document.referrer||undefined,
    session_id:sid()})}).catch(function(){});
  }
  send();
  window.addEventListener("popstate",send);
})();
</script>

Replace YOUR_TRACKING_ID with the UUID from your Antlytics dashboard.

Method 2: functions.php

A cleaner approach that does not modify your theme's HTML template directly:

// Add to your theme's functions.php
function antlytics_tracking_script() {
    $tracking_id = 'YOUR_TRACKING_ID'; // Replace with your UUID
    ?>
    <script>
    (function(){
      var t="<?php echo esc_js($tracking_id); ?>",
          u="https://www.antlytics.com/api/ingest/pageview",
          k="ant_sid";
      function sid(){
        try{var s=sessionStorage.getItem(k);if(s)return s;
        s=crypto.randomUUID();sessionStorage.setItem(k,s);return s;}
        catch(e){return crypto.randomUUID();}
      }
      function send(){
        fetch(u,{method:"POST",headers:{"Content-Type":"application/json"},
        keepalive:true,body:JSON.stringify({tracking_id:t,
        pathname:location.pathname,referrer:document.referrer||undefined,
        session_id:sid()})}).catch(function(){});
      }
      send();
      window.addEventListener("popstate",send);
    })();
    </script>
    <?php
}
add_action('wp_head', 'antlytics_tracking_script');

This uses WordPress's wp_head action to inject the script into the <head> on every page.

Verifying

  1. Visit your WordPress site in a browser without ad-blocker extensions.
  2. Open browser dev tools → Network tab.
  3. Navigate to a page and confirm a POST request to /api/ingest/pageview appears.
  4. Check your Antlytics dashboard for a new visitor.

Caching considerations

If you use a caching plugin (WP Rocket, W3 Total Cache, or similar), the script will be cached as part of your theme's HTML. This is fine — the script does not contain session-specific data and can be safely cached.

Do not add the tracking script to areas that are excluded from caching (like dynamic AJAX responses) — it should be in your page's <head>.

FAQ

Will this work with page builders? Yes. Page builders like Elementor and Divi still use WordPress's standard theme structure. The wp_head method works with all page builders.

What about Gutenberg (block editor) themes? Block themes use theme.json and block templates instead of PHP templates. Use the functions.php method (Method 2) as it works regardless of theme type.

Should I use a child theme? Yes, if you are modifying header.php directly. A child theme prevents your changes from being overwritten on theme updates. The functions.php method should also go in a child theme's functions.php.

Does this work with WordPress multisite? Yes, but each site in a network needs its own tracking ID and snippet. Add the snippet separately to each site's theme or use a network-level plugin to manage it.


Related: WordPress docs · Quick start guide · Antlytics implementation guides