Skip to content

Content management systems

WordPress analytics with CrispStats

WordPress needs one wp_head action to print the CrispStats script tag in the head of every page. Put the action in a small site plugin of your own, so it survives theme updates, and there is no third-party analytics plugin to install or keep updated.

Updated

Set up CrispStats on WordPress

  1. 01

    Step 1: Copy the WordPress snippet

    In the dashboard, open your site’s Settings, select Tracking code and choose the WordPress tab. It has your public key and domain filled in.

  2. 02

    Step 2: Create a site plugin file

    Create the folder wp-content/plugins/crispstats/ and save this file in it, with your key and domain. A site plugin keeps the tag when you switch or update your theme.

    Replace the highlighted values with your own.

    wp-content/plugins/crispstats/crispstats.php
    <?php/*** Plugin Name: CrispStats* Description: Adds the CrispStats tracking script to every page.*/add_action('wp_head', function () {?><scriptdeferdata-site="cs_pk_your_site_key"data-domains="example.com"src="https://in.crispstats.com/d.js"></script><?php});
  3. 03

    Step 3: Activate the plugin

    Open Plugins in the WordPress admin and activate CrispStats. If you would rather not add a plugin, paste the add_action block at the end of your theme’s functions.php instead, in a child theme so a theme update does not remove it. The file already starts with <?php, so do not add a second opening tag.

  4. 04

    Step 4: Clear the page cache

    If you use a caching plugin or a host-level page cache, clear it, so visitors get pages that include the new tag.

Check that it works

Open your live site while signed out, or in a private window. The status row on the Tracking code tab shows Receiving events. once the first event is stored, and the visit appears on the Realtime screen. If nothing arrives, view the page source and search for d.js to confirm the tag is in the head.

What the install check shows once the first events land, on the demo site, Quillbook, on generated traffic.
  • What you get

    • Pageviews, unique visitors and sessions for every post and page WordPress renders.
    • Where readers come from: search, social, newsletters tagged with UTM parameters, and AI assistants such as ChatGPT and Perplexity.
    • Entry and exit pages, countries, devices and browsers, with no cookies and nothing stored on the reader’s device.
    • Optional outbound link clicks, file downloads and web vitals: turn them on under Extra events on the Tracking code tab, and the snippet gains data-outbound, data-downloads and data-vitals.
    • Goals and funnels from the pages you already have, and form submissions as custom events with crispstats("signup") or data-cs-event attributes.
  • Notes and limits

    • Keep the tag a classic script with defer or async. If an optimization plugin rewrites it to type="module", the script cannot read its options and does not track.
    • Keep your own visits out of the numbers by setting localStorage.crispstats_ignore in your browser, and leave out private paths with data-exclude. See exclude traffic.
    • The wp_head action fires where your theme calls wp_head() inside <head>. A theme that never calls it gets no tag.
    • CrispStats does not publish a WordPress plugin. The site plugin above is your own file of a few lines, with nothing to update.
    • WordPress sites served by nginx or behind Cloudflare can serve the script from their own domain with one forwarding rule: see the proxy guide.

The docs page covers the optional steps this guide leaves out.

Setup questions. Straight answers.

What people ask before they connect it.

  • CrispStats does not publish one, and you do not need one. A plugin file of a few lines that you create yourself adds the script to every page, and it has no settings screen, no database tables and no updates to install.

  • The core script is about 2.2 KB gzipped and loads with defer, so it never blocks the page from rendering. Optional modules add a little more, up to about 3.8 KB with all of them on.

  • It counts every browser that loads the page, including yours. Set the ignore flag in your own browser once, and your visits are left out from then on.

  • CrispStats sets no cookies and stores nothing on the visitor's device by default, and it never stores IP addresses. WordPress sets its own cookies for logged-in users and commenters, and plugins may add more, so the answer depends on the whole site. This is not legal advice.

  • The plugin file is yours, so there is nothing to buy on the WordPress side. Every account starts with a 14-day free trial with every feature and no card; then Starter is $5 a month for 250,000 events.

Not answered here? Write to hello@crispstats.com

Go further

Related

Your traffic, crisp and clear

One script, no cookies, and every feature from the first day. Set up takes about as long as pasting a snippet.