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.
Option 1: Code Snippets Plugin (Recommended)
The safest and easiest method is using a code snippets plugin like WPCode or Code Snippets.
Using WPCode
- In your WordPress admin, go to Plugins → Add New Plugin
- Search for “WPCode”
- Install and activate WPCode (listed as “WPCode – Insert Headers and Footers + Custom Code Snippets”)
- Go to Code Snippets → Header & Footer
- 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 -->- Click Save Changes
Using Code Snippets Plugin
- Install and activate the Code Snippets plugin
- Go to Snippets → Add New
- Name it “Ghost Metrics Tracking”
- 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
});- Set the scope to Only run on site front-end
- 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.
- Go to Appearance → Theme File Editor (or Tools → Theme File Editor on block themes)
- Select your child theme’s
functions.php - 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');- Click Update File
Option 3: Header.php (Not Recommended)
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.phpat 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:
- 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.
- 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.
Consent Plugins
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_headhook 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.