Add PostMetric to your WordPress site in minutes.
Installation
Method 1: Using a plugin (Recommended)
- Install a “Insert Headers and Footers” plugin
- Go to Settings → Insert Headers and Footers
- Add the tracking script to the Header section:
<script src="https://your-domain.com/api/track.js?site=YOUR_TRACKING_CODE"></script>
Replace YOUR_TRACKING_CODE with your tracking code from the dashboard.
Method 2: Edit theme files
- Go to Appearance → Theme Editor
- Select
header.php
- Add the tracking script before the closing
</head> tag:
<script src="https://your-domain.com/api/track.js?site=YOUR_TRACKING_CODE"></script>
Editing theme files directly will lose changes when the theme updates. Use a
child theme or plugin method instead.
Track custom goals
Track custom goals in your WordPress posts/pages:
<button onclick="postMetric('track', 'goal', { event: 'button_click' })">
Click me
</button>
Or in your theme’s JavaScript:
document.getElementById("my-button").addEventListener("click", function () {
postMetric("track", "goal", { event: "button_click" });
});
WooCommerce integration
Track purchases in WooCommerce:
add_action('woocommerce_thankyou', 'track_purchase');
function track_purchase($order_id) {
$order = wc_get_order($order_id);
?>
<script>
postMetric('track', 'goal', {
event: 'purchase',
value: <?php echo $order->get_total(); ?>
});
</script>
<?php
}
Next steps
Track custom goals
Learn how to track custom user actions