Skip to content
whovisited.

WordPress · Website analytics

Install WhoVisited on WordPress with a small site-specific plugin

Add the WhoVisited tracker through WordPress's script queue, keep it out of admin pages, and verify consent, duplicate snippets, and browser requests.

Keep analytics code out of a theme edit

A common way to add an analytics script to WordPress is to paste it into a theme header or a site-wide code box. That can work, but theme edits are easy to lose during a theme change, and adding the same snippet through a second plugin or tag manager can make troubleshooting harder. A small site-specific plugin gives the install one named place and uses WordPress's front-end script queue.

The example below uses the WordPress 6.3-or-newer script loading strategy argument. It enqueues the external tracker on front-end pages, marks it async, and adds the site's WhoVisited ID to that one generated script tag. It does not install a WordPress analytics plugin from WhoVisited; it is a small integration you control.

  1. Create or use a staging copy first so you can inspect the HTML and Network panel without confusing production reports.
  2. In WhoVisited, copy the installation code for the website record that owns your production domain. Confirm that domain is in the site's allowed origins.
  3. Do not paste a real customer ID or private account data into a tracking event while testing. The site ID identifies the website configuration and is intended to appear in browser code.

Create a tiny plugin to enqueue the tracker

Create `wp-content/plugins/whovisited-site-tracker/whovisited-site-tracker.php` with the following code. Replace the placeholder with the site ID from the installation snippet. The unique WordPress handle helps the script queue and the tag filter target only this integration.

php
<?php
/**
 * Plugin Name: WhoVisited Site Tracker
 * Description: Loads the WhoVisited browser tracker on the public site.
 */

function mysite_enqueue_whovisited_tracker() {
    wp_enqueue_script(
        'mysite-whovisited-tracker',
        'https://whovisited.net/tracker.js',
        array(),
        null,
        array( 'strategy' => 'async', 'in_footer' => false )
    );
}
add_action( 'wp_enqueue_scripts', 'mysite_enqueue_whovisited_tracker' );

function mysite_add_whovisited_site_id( $tag, $handle, $src ) {
    if ( 'mysite-whovisited-tracker' !== $handle ) {
        return $tag;
    }

    return str_replace(
        '<script ',
        '<script data-site-id="REPLACE_WITH_YOUR_SITE_ID" ',
        $tag
    );
}
add_filter( 'script_loader_tag', 'mysite_add_whovisited_site_id', 10, 3 );

The filter preserves the script tag WordPress generated—including its async strategy—and adds only the `data-site-id` attribute to the matching handle. Keep the handle string the same in both functions. Activate the plugin in WordPress, then view a public page's source and confirm exactly one `tracker.js` tag has the correct site ID.

If you are running a WordPress release older than 6.3, the array form of the final `wp_enqueue_script` argument does not configure a loading strategy. Upgrade WordPress or adapt the integration to your version and verify the final script tag; do not assume the code above produced `async` on older installations.

WordPress's enqueue system relies on the active theme calling `wp_head()` to print a header script. If the plugin is active but no tag appears in View Source, check the theme hook and the page template before changing the tracker code.

Make sure only one installation owns the tracker

Before activating the plugin on a live site, search the active theme, child theme, header/footer injection plugins, Google Tag Manager, and any existing analytics plugins for `tracker.js` or the same site ID. Remove the old WhoVisited installation if you move to this plugin. Do not add the snippet in all of those places “for safety.” One installation is easier to audit, and a second script with a different site ID may not behave as you expect because the tracker keeps a single page-level instance.

If you use Google Tag Manager for other vendors, either add WhoVisited there or use this plugin, then document which one owns it. Do not do both. This sample loads on public front-end pages through `wp_enqueue_scripts`; it does not add analytics to wp-admin screens.

Verify the request, not just the saved setting

  1. Open the page in a clean browser profile and inspect View Source. Confirm there is one WhoVisited `tracker.js` tag and the `data-site-id` matches the website record you meant to install.
  2. Open DevTools Network, reload the page, and check that `tracker.js` and the bootstrap request load. Then check the event request and response; downloading JavaScript alone does not show that an event was accepted.
  3. In WhoVisited, use the installation check after the test visit. It reports recent site-level activity, so confirm the test browser's request in Network rather than relying on a green status produced by another visitor.
  4. Test the final production hostname after WordPress, CDN, and hosting redirects. The site's allowed-origin setting must include the domain that actually serves the page.
  5. If a caching or optimization plugin combines, delays, or removes scripts, disable only that optimization for the staging test, verify the result, and then configure a narrow exclusion if required. Retest after changing the optimization settings.

Because the tracker is asynchronous, it is fetched while the page is being parsed and runs when available. A custom event fired immediately by another script can race the tracker; this installation example is for automatic page views. If your WordPress site also sends custom events, follow the WhoVisited guide for waiting until the tracker is ready instead of assuming script order.

References and further reading

  1. WordPress: wp_enqueue_script()
  2. WordPress: script_loader_tag hook
  3. WhoVisited: Install website analytics
  4. WhoVisited: Analytics tracker loads but no events appear
  5. WhoVisited: Prevent custom events from racing an async tracker in Next.js