Implementation Guides
Step-by-step tutorials for implementing various analytics solutions in WordPress. Choose your preferred method and follow our detailed guides.
Google Analytics 4 Implementation
1 Plugin Method (Recommended for Beginners)
Recommended Plugin: MonsterInsights or GA Google Analytics
Install Plugin
Go to WordPress Admin → Plugins → Add New → Search for "MonsterInsights" or "GA Google Analytics"
Get GA4 Measurement ID
From Google Analytics → Admin → Data Streams → Your Website → Measurement ID (starts with G-)
Configure Plugin
Enter your Measurement ID in the plugin settings and enable tracking
Test Installation
Use Google Analytics Real-time reports to verify data collection
Code Example (MonsterInsights):
// The plugin handles this automatically
// No manual code required
2 Manual Implementation (Advanced Users)
Add to functions.php
Add the tracking code to your theme's functions.php file
Code Example:
function add_google_analytics() {
if (!current_user_can('manage_options')) {
?>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-XXXXXXXXXX');
</script>
<?php
}
}
add_action('wp_head', 'add_google_analytics');
3 Google Tag Manager Method (Recommended for Advanced Tracking)
Benefits: Easy to manage multiple tracking codes, advanced event tracking, no code changes needed
Create GTM Account
Sign up at tagmanager.google.com and create a container for your website
Install GTM Code
Add GTM container code to your WordPress site (head and body sections)
Configure GA4 Tag
Create a GA4 Configuration tag in GTM with your Measurement ID
Publish Container
Preview, test, and publish your GTM container
GTM Installation Code:
// Add to <head> section
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-XXXXXXX');</script>
// Add to <body> section (immediately after opening tag)
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-XXXXXXX"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
WooCommerce E-commerce Tracking
Enhanced e-commerce tracking for WooCommerce stores to track purchases, product views, and shopping behavior.
Using MonsterInsights Pro
- Install MonsterInsights Pro plugin
- Enable Enhanced E-commerce addon
- Configure e-commerce settings in GA4
- Test purchase tracking with test orders
Manual E-commerce Implementation
Note: This requires advanced PHP and JavaScript knowledge
Purchase Event Tracking:
// Add to WooCommerce thank you page
function woocommerce_ga4_purchase_tracking($order_id) {
$order = wc_get_order($order_id);
$items = array();
foreach ($order->get_items() as $item) {
$product = $item->get_product();
$items[] = array(
'item_id' => $product->get_sku() ?: $product->get_id(),
'item_name' => $product->get_name(),
'category' => implode('/', wp_get_post_terms($product->get_id(), 'product_cat', array('fields' => 'names'))),
'quantity' => $item->get_quantity(),
'price' => $product->get_price()
);
}
echo "<script>
gtag('event', 'purchase', {
'transaction_id': '{$order->get_order_number()}',
'value': {$order->get_total()},
'currency': '{$order->get_currency()}',
'items': " . json_encode($items) . "
});
</script>";
}
add_action('woocommerce_thankyou', 'woocommerce_ga4_purchase_tracking');
Custom Event Tracking
Track specific user interactions like downloads, form submissions, and button clicks.
File Download Tracking
// Track PDF downloads
document.addEventListener('DOMContentLoaded', function() {
document.querySelectorAll('a[href$=".pdf"]').forEach(function(link) {
link.addEventListener('click', function() {
gtag('event', 'file_download', {
'file_name': this.getAttribute('href'),
'link_text': this.innerText
});
});
});
});
Form Submission Tracking
// Track contact form submissions
document.getElementById('contact-form').addEventListener('submit', function() {
gtag('event', 'form_submit', {
'form_name': 'contact_form',
'form_destination': 'contact_page'
});
});
Scroll Depth Tracking
// Track scroll depth milestones
let scrollDepthMarks = [25, 50, 75, 90];
let scrollDepthReached = [];
window.addEventListener('scroll', function() {
let scrollPercent = Math.round((window.scrollY / (document.body.scrollHeight - window.innerHeight)) * 100);
scrollDepthMarks.forEach(function(mark) {
if (scrollPercent >= mark && !scrollDepthReached.includes(mark)) {
scrollDepthReached.push(mark);
gtag('event', 'scroll', {
'percent_scrolled': mark
});
}
});
});
Verification and Testing
Always verify your analytics implementation to ensure accurate data collection.
Testing Methods
- Google Analytics Real-time reports
- Google Analytics Debugger extension
- Google Tag Assistant
- Browser developer tools (Network tab)
- GA4 DebugView
What to Check
- Page views are being tracked
- Events fire correctly
- E-commerce data is accurate
- Tracking works on all devices
- No duplicate tracking codes
Testing Checklist:
- Visit your website and check GA4 Real-time reports
- Test all important user interactions (forms, purchases, downloads)
- Verify tracking on mobile devices
- Check that admin visits are excluded (if configured)
- Confirm GDPR compliance measures are working
Need Help with Implementation?
Check out our troubleshooting guide for common issues and solutions.
Troubleshooting Guide