Skip to Content
InstallationWordPress

WordPress Installation

Install Ghost Metrics on your WordPress website.

Coming Soon: Official Plugin

We’re currently developing an official Ghost Metrics plugin for WordPress that will make installation and configuration even easier. Stay tuned for updates.

In the meantime, you can install Ghost Metrics using one of the methods below.


The safest and easiest method is using a code snippets plugin like WPCode or Code Snippets.

Using WPCode

  1. In your WordPress admin, go to PluginsAdd New Plugin
  2. Search for “WPCode”
  3. Install and activate WPCode (listed as “WPCode – Insert Headers and Footers + Custom Code Snippets”)
  4. Go to Code SnippetsHeader & Footer
  5. Paste your Ghost Metrics tracking code in the Header box — this places it in the <head> of every page, which is exactly where it belongs:
<!-- Ghost Metrics Tag Manager --> <script> var _mtm = window._mtm = window._mtm || []; _mtm.push({'mtm.startTime': (new Date().getTime()), 'event': 'mtm.Start'}); (function() { var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.async=true; g.src='https://example.ghostmetrics.cloud/js/container_XXXXXXXX.js'; s.parentNode.insertBefore(g,s); })(); </script> <!-- End Ghost Metrics Tag Manager -->
  1. Click Save Changes

Using Code Snippets Plugin

  1. Install and activate the Code Snippets plugin
  2. Go to SnippetsAdd New
  3. Name it “Ghost Metrics Tracking”
  4. Add this PHP code:
add_action('wp_head', function() { ?> <!-- Ghost Metrics Tag Manager --> <script> var _mtm = window._mtm = window._mtm || []; _mtm.push({'mtm.startTime': (new Date().getTime()), 'event': 'mtm.Start'}); (function() { var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.async=true; g.src='https://example.ghostmetrics.cloud/js/container_XXXXXXXX.js'; s.parentNode.insertBefore(g,s); })(); </script> <!-- End Ghost Metrics Tag Manager --> <?php });
  1. Set the scope to Only run on site front-end
  2. Save and activate the snippet

Option 2: Theme Functions File

Add tracking code via your theme’s functions.php file.

Important: Use a child theme to prevent losing changes when your theme updates. Note that modern block themes (Twenty Twenty-Four and later) put the file editor under Tools → Theme File Editor, and many hosts disable the file editor entirely for security — if you don’t see it, use Option 1 instead.

  1. Go to AppearanceTheme File Editor (or Tools → Theme File Editor on block themes)
  2. Select your child theme’s functions.php
  3. Add this code at the end:
/** * Ghost Metrics Tracking Code */ function ghost_metrics_tracking_code() { ?> <!-- Ghost Metrics Tag Manager --> <script> var _mtm = window._mtm = window._mtm || []; _mtm.push({'mtm.startTime': (new Date().getTime()), 'event': 'mtm.Start'}); (function() { var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.async=true; g.src='https://example.ghostmetrics.cloud/js/container_XXXXXXXX.js'; s.parentNode.insertBefore(g,s); })(); </script> <!-- End Ghost Metrics Tag Manager --> <?php } add_action('wp_head', 'ghost_metrics_tracking_code');
  1. Click Update File

You can edit your theme’s header.php directly, but this is not recommended because:

  • Changes are lost when the theme updates
  • It’s easy to break your site with a typo
  • Block themes don’t have a header.php at all

If you must use this method, add the tracking code inside the <head> section (as high as possible) in your child theme’s header.php file.


Caching and Optimization Plugins

Page caching (WP Rocket, W3 Total Cache, LiteSpeed Cache, etc.) is compatible with the tracking code — the snippet gets baked into the cached HTML. Two things to watch:

  1. Purge after installing. Cached pages generated before you added the snippet keep being served without it until you clear the plugin cache, any CDN cache (Cloudflare, etc.), and test in an incognito window.
  2. Exclude the snippet from JavaScript optimization. Features like WP Rocket’s “Delay JavaScript execution” or Autoptimize’s combine/defer can postpone or break the tracking loader. Add your Ghost Metrics domain (or the snippet) to the plugin’s JS exclusion list.

If you use a cookie-consent plugin (Complianz, CookieYes, etc.), it may automatically block unrecognized scripts until consent is given. Categorize the Ghost Metrics snippet appropriately in the plugin so tracking behaves the way your privacy policy intends.

WordPress Multisite

For WordPress Multisite installations, you have two options:

  • Network-wide tracking: Network-activate a snippets plugin such as WPCode and add the snippet once for all sites (or add the wp_head hook in a shared mu-plugin)
  • Per-site tracking: Add each site’s own tracking code individually

Contact support if you need help configuring tracking for a Multisite network.

Next Steps

Last updated on