Skip to content

WordPress

WordPress hides <head> behind theme templates, so adding Statable means either using a header-injection plugin or pasting a snippet into a child theme. Either takes about a minute.

This guide covers single-site, multisite, and WooCommerce. Works on classic and block (FSE) themes, WordPress 6.x, and PHP 7.4+.

Install

Use WPCode – Insert Headers and Footers + Custom Code Snippets by WPCode (formerly Insert Headers and Footers by WPBeginner). Free, in the official Plugin Directory, 3M+ active installs.

  1. In WordPress admin, go to Plugins → Add New.
  2. Search "WPCode" or "Insert Headers and Footers", click Install Now, then Activate.
  3. Go to Code Snippets → Header & Footer.
  4. Paste the snippet below into the Header box, replacing YOUR_SITE_ID:
<script defer src="https://statable.com/js/YOUR_SITE_ID/s.js"></script>
  1. Click Save Changes.

This survives theme switches and updates, and works with most caching plugins out of the box.

Legacy menu path

On older WPCode 1.x installs (or with "Headers & Footers mode" enabled in WPCode → Settings), the panel lives at Settings → Header & Footer instead.

Option 2: Manual edit (child theme)

If you'd rather not install another plugin, add the snippet directly. Always edit a child theme, never the parent, so updates don't overwrite your changes. Block themes (FSE) support functions.php too, even though theme.json handles styling.

In wp-content/themes/your-child-theme/functions.php:

<?php
add_action('wp_head', function () {
    ?>
    <script defer src="https://statable.com/js/YOUR_SITE_ID/s.js"></script>
    <?php
}, 5);

Priority 5 runs after wp_enqueue_scripts (priority 1) but before print_emoji_detection_script (7), wp_print_styles (8), and wp_print_head_scripts (9). The tag lands near the top of <head>, ahead of most third-party output.

Tracking custom events

Attach event handlers via wp_add_inline_script against a registered "dummy" handle:

<?php
add_action('wp_enqueue_scripts', function () {
    wp_register_script('statable-events', '', [], null, true);
    wp_enqueue_script('statable-events');
    wp_add_inline_script('statable-events', "
        document.addEventListener('submit', function (e) {
            if (e.target.matches('.contact-form')) {
                window.statable && window.statable.t('Contact Form Submitted');
            }
        });
    ");
});

The empty src is the canonical pattern. WordPress 5.7+ also exposes wp_print_inline_script_tag() if you'd rather skip the dummy handle and print a tag directly.

WooCommerce: track purchases

Tested against WooCommerce 9.x with HPOS (High-Performance Order Storage) enabled. wc_get_order(), $order->get_total(), and $order->get_currency() are HPOS-compatible.

<?php
add_action('woocommerce_thankyou', function ($order_id) {
    if (! $order_id) return;
    $order = wc_get_order($order_id);
    if (! $order) return;
    ?>
    <script>
      window.statable && window.statable.t('Purchase', {
        revenue: <?php echo esc_js($order->get_total()); ?>,
        currency: '<?php echo esc_js($order->get_currency()); ?>'
      });
    </script>
    <?php
});

$order->get_currency() returns the currency the order was placed in, not the active store currency. Useful if you run a multi-currency store.

Multisite networks

Each subsite needs its own Site ID. Add the snippet per-site:

  • Header-injection plugin. Install on each subsite separately. Each instance keeps its own settings.
  • Child-theme functions.php. Branch on get_current_blog_id() to pick the right Site ID per subsite.

Verify it's working

  • Open Statable Realtime in your dashboard.
  • Visit any page on your WordPress site. Use an incognito window if your own browser is opted out.
  • See Verify installation for full verification.

Common pitfalls

  • Caching and optimisation plugins. WP Rocket, LiteSpeed Cache, FlyingPress, W3 Total Cache, and Cloudflare APO can defer, combine, or minify scripts in ways that break data-id. Add statable.com/js/YOUR_SITE_ID/s.js to the plugin's "Excluded JavaScript Files" / "Exclude from Minify" / "Exclude from Combine" lists. WP Rocket: Settings → WP Rocket → File Optimization → Excluded JavaScript Files.
  • Editing a parent theme. Theme updates erase your changes. Use a child theme or a plugin.
  • Google Tag Manager already installed. Don't load Statable both directly and through GTM. Pick one. See Google Tag Manager.
  • Multilingual plugins (Polylang, WPML). One Site ID covers all languages. The script reports the page URL, so per-language reports work via URL filters in the dashboard.
  • Admin Bar pageviews. Logged-in admin sessions are still tracked. To exclude yourself, run localStorage.setItem('analytics_ignore', 'true') once in your browser console. See Verify installation.

See also: Install the tracking script, Custom events, JavaScript API.


Ready to take control of your web analytics? Try Statable free for 30 days — no credit card required, full feature access, GDPR-compliant by default. Start your free trial or view a live demo.