How to add analytics to WordPress without a plugin

Self-hosted WordPress counts no visitors on its own, and adding a tool doesn't take a plugin. One small must-use file, or a child theme's functions.php, hooks the tag into wp_head, where theme updates can't remove it. Then clear the caches and test logged out.

10 min read

Summarize with

In this article
  1. 1What WordPress already counts
  2. 2Before you start
  3. 3Which of the three to use
  4. 4Option 1: a must-use file
  5. 5Option 2: functions.php in a child theme
  6. 6Option 3: header.php, classic themes only
  7. 7Leave out your own visits
  8. 8Clear the caches, then check
  9. 9If the numbers look wrong
  10. 10Does the site still need a cookie banner?
  11. 11The short version

An analytics tool doesn't need a WordPress plugin. Its tag has to reach the head of every page, and WordPress already has a standard way to put things there: the wp_head hook, which classic and block themes both run. Hook into it from one small file you write yourself, and the tag stays put through theme updates.

This guide covers three places for the tag on a self-hosted WordPress site, which one suits your theme, how to leave out your own visits, and how to check that it works. The steps work for any analytics tool that installs with a script tag. The WordPress details come from its documentation and its source code, as of November 2026.

What WordPress already counts

Which WordPress you run decides where you start:

Self-hosted WordPress

The software from WordPress.org, on a host you pick, has no visitor report of its own. The dashboard widgets WordPress documents cover posts, comments, drafts and WordPress news, not traffic. Counting visitors means adding a tool, and this guide is about doing that.

WordPress.com

Stats come with every plan. According to WordPress.com's support page, free sites see the last seven days of views, visitors, likes and comments, the Personal plan adds all-time traffic, and UTM and device stats need Premium or higher. Adding header code isn't possible on the free plan, and on paid plans WordPress.com's own guide does it with a plugin.

Before you start

Sign up for the tool you picked and add your site under the address visitors use, the one in Settings → General → Site Address (URL). Then copy the snippet it gives you, usually a single <script> tag shaped roughly like this:

<script defer src="https://analytics.example.com/script.js" data-site="YOUR-SITE-ID"></script>

That's a placeholder, not a working tag. Copy the exact one from your tool's dashboard, not from an email or a document, where straight quotes can turn into curly ones and quietly break it. Then check three things:

  • Which kind of theme you have. WordPress only shows an Editor item under Appearance when the active theme is a block theme. Classic themes show Customize instead, and in recent versions a Design or Patterns item, which isn't the same thing. Under Appearance → Themes, the active theme's details say “This is a child theme of …” if it is one.
  • That you can reach the files. Over SFTP, or in your host's file manager. A typo in a PHP file can stop the site from loading, admin screens included, and then the files are the only way to undo it.
  • That you have a backup. WordPress's guide to editing files says to back up first, and notes that changes made in its built-in editors are live at once. Edit in a plain text editor, not a word processor.

Which of the three to use

Must-use filefunctions.php in a child themeheader.php in a child theme
Block themesYesYesNo, there is no header.php
Classic themesYesYesYes
After a theme updateStill thereStill thereStill there, but your copy misses later changes to the parent's file
After switching themesStill thereGoneGone
Edit from the adminNo, needs file accessYes, in the Theme File EditorYes, once it's copied over with file access

For most sites, that makes it the must-use file. If you already run a child theme and would rather edit from the admin, its functions.php lasts just as well until you change themes. header.php is the last resort.

Option 1: a must-use file

A must-use plugin isn't an analytics plugin from the directory. It's one PHP file you write yourself and put in a special folder, and WordPress runs it on its own. Its documentation says must-use plugins are enabled automatically and can't be disabled from the admin screens. To turn one off, you delete the file.

  1. Open wp-content over SFTP or in the file manager, and create a folder called mu-plugins if there isn't one.
  2. Create a file in it, for example site-analytics.php.
  3. Paste this, with your tool's tag in place of the placeholder line, and save.
wp-content/mu-plugins/site-analytics.php
<?php
/**
 * Plugin Name: Site analytics
 * Description: Prints the analytics tag in the head of every front-end page.
 */

add_action( 'wp_head', function () {
    ?>
    <script defer src="https://analytics.example.com/script.js" data-site="YOUR-SITE-ID"></script>
    <?php
} );

The function closes PHP with ?>, prints your tag as plain HTML and opens PHP again, so the tag goes in exactly as your tool gave it, quotes and all. wp_head is the hook WordPress runs inside the <head> of every front-end page. Classic themes call it from header.php, and for block themes WordPress writes the head itself and calls it there. If your tool asks for its tag at the end of the page instead, use wp_footer, which runs just before </body>.

  • The file sits directly in mu-plugins. WordPress only loads PHP files at the top level of that folder, not in subfolders.
  • Leave out a closing ?> at the end. Blank lines after one are a classic cause of the “headers already sent” warning, as WordPress's troubleshooting FAQ explains. Save the file as UTF-8 without a BOM for the same reason.
  • Look for it under Plugins → Must-Use, not in the main list of plugins.
  • On a multisite network, it runs on every site. If the tag belongs to one site only, put it in a child theme that only that site uses.

Option 2: functions.php in a child theme

Every theme can have a functions.php, which WordPress loads along with the theme. The same snippet works there, without the first lines:

wp-content/themes/your-child-theme/functions.php
add_action( 'wp_head', function () {
    ?>
    <script defer src="https://analytics.example.com/script.js" data-site="YOUR-SITE-ID"></script>
    <?php
} );

Add it at the end of the file. The file already starts with <?php, so don't add another. If it ends with ?>, paste above that line. If the child theme has no functions.php yet, create one over SFTP or in the file manager, with <?php on its first line and the snippet below it.

Put it in a child theme, never in the parent theme. WordPress's handbook on child themes warns that a change to the parent theme's functions.php disappears at the next theme update. A child theme's functions.php doesn't replace the parent's. Both are loaded, the child's just before the parent's, so adding to it changes nothing else.

To edit it in the admin, open the Theme File Editor: under Appearance on a classic theme, under Tools on a block theme, and on a multisite network under Themes in the Network Admin. Pick the child theme under Select theme to edit before you change anything. If the item is missing, the site's wp-config.php may set DISALLOW_FILE_EDIT, which WordPress suggests as a safeguard, and you'll need the files instead.

No child theme yet? Moving the site to a new theme is a bigger change than one tag needs. The must-use file does the same job without it.

Option 3: header.php, classic themes only

Classic themes write the head of each page in header.php, and you can paste the tag there by hand:

  1. Copy header.php from the parent theme into the child theme's folder, over SFTP or in the file manager. The child theme's copy of a template takes the place of the parent's.
  2. Open the copy and find <?php wp_head(); ?>, usually just before the closing </head>.
  3. Paste the tag on its own line between the two, and save.
wp-content/themes/your-child-theme/header.php (the end of the head)
    <?php wp_head(); ?>
    <script defer src="https://analytics.example.com/script.js" data-site="YOUR-SITE-ID"></script>
</head>

It's the most fragile of the three. Block themes have no header.php to edit: they build pages from HTML templates and template parts such as parts/header.html, and WordPress writes the head itself. And from the moment you copy the file, changes the theme's authors make to their own header.php no longer reach your site.

Some classic themes also have a box for header scripts in their settings. That works too, but the box is part of the theme, so the tag goes when the theme does.

Put the tag where theme updates can't reach it: a must-use file, or a child theme.

Leave out your own visits

If you read your own site while logged in to WordPress, those visits end up in the numbers, often under direct traffic. WordPress knows who is logged in, so the tag can leave them out. In either PHP snippet above, add a check at the start of the function:

add_action( 'wp_head', function () {
    if ( is_user_logged_in() ) {
        return;
    }
    ?>
    <script defer src="https://analytics.example.com/script.js" data-site="YOUR-SITE-ID"></script>
    <?php
} );

is_user_logged_in() is true for anyone logged in, so this also leaves out editors and authors, and on a site where visitors have accounts, every logged-in member or customer. If their visits matter to you, skip it. It doesn't catch you when you browse logged out or on another device. In header.php, put <?php if ( ! is_user_logged_in() ) : ?> on the line above the tag and <?php endif; ?> on the line below it instead.

Clear the caches, then check

If the site runs a caching plugin, a cache at the host or a CDN, visitors may still get stored copies of pages built before the tag existed. Clear each cache you have: the plugin's, then the host's, then the CDN's. Then, straight away:

  1. Open the site in a private window, logged out, with any ad or tracker blocker turned off for the test.
  2. View the page source and search for the script's address. It should be inside the <head>.
  3. Open the developer tools, go to the Network tab and reload. Filter by your tool's domain. You should see the script load, and usually a second request that sends the pageview.
  4. Open the tool's live or real-time view, if it has one, and look for your visit.
  5. Click through to a post, a page and an archive, not only the home page, and check that each one is counted.

If the numbers look wrong

  • Not in the page source. A cache is still serving old copies, the snippet went into a theme that isn't the active one, the must-use file is in a subfolder or doesn't end in .php, or you're logged in with the logged-in check on.
  • In the source, but nothing loads. A blocker stopped it, or the tag broke on the way: curly quotes, a missing </script>, or a Content Security Policy from a security plugin or the host that doesn't list the tool's domain.
  • The tag looks different from the one you pasted. An optimisation plugin that combines, delays or rewrites scripts may have changed it. Exclude the analytics script from those features and clear the cache again.
  • The script loads, but no visits appear. The address in the tool doesn't match the one visitors use. Compare it with the Site Address in Settings → General, www against no www.
  • Everything counted twice. The tag is on the page twice: in the must-use file and functions.php, or added once more by an old plugin or a theme setting. Search the page source for the script's address and remove the extra copy.
  • The site breaks after the edit. Open the file over SFTP or in the file manager and take your change out, then look for a missing semicolon or bracket, a second <?php, or blank lines after a closing ?>. WordPress sends notices about critical errors and recovery mode to the Administration Email Address in Settings → General, so check that inbox too.
  • Tracking stopped after a theme update. The snippet was in the parent theme. Move it to a must-use file or a child theme.
  • Two tools disagree. They will, by a margin. Check that both rise and fall on the same days, and read why no two analytics tools agree on your traffic for where the gaps come from.

A cookie-free analytics script removes one reason for a banner. The rest of the site can still set cookies. WordPress itself sets them when someone logs in and when a commenter ticks the box to save their name, email and website for next time. Plugins, embedded videos, fonts and forms can add their own. To see what your site stores, open the developer tools, clear the site data, browse a few pages logged out and leave a test comment with that box ticked. Whatever appears under Cookies and Local storage came from somewhere. Whether you can do without a banner depends on that whole list, and what it takes to run a website without a cookie banner goes through it.

The short version

  • On WordPress.com, look at Stats and your plan first.
  • On self-hosted WordPress, put the tag in a must-use file in wp-content/mu-plugins.
  • Or in a child theme's functions.php, never the parent's.
  • header.php only on a classic theme, and only in a child theme.
  • Optionally, skip the tag for logged-in users.
  • Clear every cache, then check the source, the Network tab and the live view.

Lomen is one of the tools you could add this way. Its tag carries defer, your data-site-id and the script address https://lomenanalytics.com/api/public/track.js, with no API key, package or build step, so it works in any of the three places above. A second copy of the tag for the same site does nothing, so a leftover copy from an old plugin can't double the count, and pageviews only count from the domain you added and its subdomains, so a copy of the site on localhost or at your host's staging address stays out of the numbers, unless that address is a subdomain of yours. Until the first pageview arrives, the dashboard loads your live home page and says whether the tag is in its HTML, so a cache still serving the old page shows up there as a missing tag. It has no setting for leaving out your own visits, so use the logged-in check above. Its default tag sets no cookies and keeps nothing in the visitor's browser, and IP addresses are never stored. It has no custom events or funnels and doesn't connect to WooCommerce, though payments that go through your own Stripe account can sit on the same chart as visitors. Lomen is free during the public beta.

Numbers you can ask questions

Lomen is one cookie-free script: sources, campaigns and AI referrals on one dashboard, and an AI that answers from your own figures. Free during the beta, no credit card.