14 KiB
GA4 Installation Methods Complete Guide
Comprehensive guide covering all three GA4 installation methods with code examples and platform-specific instructions.
Installation Method Comparison
Quick Decision Matrix
| Method | Best For | Technical Level | Flexibility | Maintenance |
|---|---|---|---|---|
| CMS Plugin | WordPress, Shopify, Wix | Beginner | Low | Low effort |
| gtag.js | Developers, custom sites | Intermediate | Medium | Code changes |
| Google Tag Manager | Most websites, teams | Beginner-Advanced | High | No code changes |
When to Use Each Method
Use CMS Plugin When:
- Using popular CMS (WordPress, Shopify, Wix, Squarespace)
- Want click-and-configure setup
- No technical expertise required
- Single tracking platform (GA4 only)
Use gtag.js When:
- Have code access to website
- Only need Google products (GA4, Google Ads)
- Want lightweight implementation
- Comfortable editing HTML/JavaScript
Use Google Tag Manager When:
- Need multiple tracking tags
- Want flexibility without code changes
- Team collaboration required
- Need testing/version control
- Plan to add more tags later
- Recommended for 90% of websites
Method 1: CMS Plugin Installation
WordPress Installation
Recommended Plugins:
- Site Kit by Google (Official Google plugin)
- GA Google Analytics (MonsterInsights)
- ExactMetrics (formerly GoogleAnalytics by Yoast)
- Insert Headers and Footers (manual code injection)
Site Kit by Google (Recommended)
Step 1: Install Plugin
- WordPress Admin → Plugins → Add New
- Search "Site Kit by Google"
- Click "Install Now"
- Click "Activate"
Step 2: Connect Google Account
- Site Kit → Start Setup
- Sign in with Google Account (must have GA4 access)
- Allow Site Kit permissions
Step 3: Configure Analytics
- Site Kit will detect existing GA4 properties
- Select property to connect, or create new
- If creating new:
- Enter website URL
- Select timezone
- Confirm settings
- Activate Analytics module
Step 4: Verify Installation
- Site Kit dashboard shows Analytics data
- Check GA4 Realtime reports
- Confirm events appearing
Benefits:
- Official Google plugin
- Automatic updates
- Shows data in WordPress dashboard
- Easy setup wizard
MonsterInsights/ExactMetrics
Installation:
- Install plugin from WordPress repository
- Activate plugin
- Run setup wizard
- Connect Google Account
- Select GA4 property
- Configure tracking options
Features:
- Enhanced ecommerce tracking (premium)
- Form tracking
- User-friendly dashboard
- Popular posts widget
Shopify Installation
Native Integration (Recommended):
Step 1: Access Settings
- Shopify Admin → Settings
- Click "Customer events"
Step 2: Add GA4
- Click "Add custom pixel"
- Select "Google Analytics 4"
- Enter Measurement ID (G-XXXXXXXXXX)
- Name pixel (e.g., "GA4 Tracking")
- Save
Step 3: Verify
- Preview store
- Check GA4 DebugView
- Confirm page views and events
Manual Installation (Advanced):
Step 1: Access Theme Code
- Online Store → Themes
- Actions → Edit code
- Open
theme.liquid
Step 2: Add gtag.js
Insert before </head>:
<!-- Google tag (gtag.js) -->
<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>
Step 3: Save and Publish
Wix Installation
Step 1: Access Tracking & Analytics
- Wix Dashboard → Settings
- Marketing & SEO → Marketing Integrations
- Google Analytics
Step 2: Connect GA4
- Click "Connect"
- Choose "Connect to existing account"
- Sign in to Google
- Select GA4 property
- Click "Connect"
Step 3: Configure Settings
- Enable "Track events"
- Select events to track
- Save settings
Alternative: Manual Installation
- Settings → Custom Code
- Add code to "Header"
- Paste gtag.js snippet
- Apply to all pages
Squarespace Installation
Step 1: Access Analytics
- Settings → Analytics
- Google Analytics
Step 2: Add Measurement ID
- Select "Google Analytics 4"
- Enter Measurement ID
- Save
Step 3: Additional Tracking
- Settings → Advanced → Code Injection
- Add custom event tracking if needed
Method 2: gtag.js Direct Installation
Overview
gtag.js (Google Tag) is JavaScript library for implementing GA4 directly without tag management system.
Full Installation Code
<!-- Google tag (gtag.js) -->
<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>
Placement Requirements
Location: <head> section of HTML
Position:
- Immediately after
<head>opening tag - Before all other scripts (except meta tags)
- Above any custom gtag() calls
Complete HTML Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Website</title>
<!-- Google tag (gtag.js) - MUST BE FIRST -->
<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>
<!-- Other scripts after gtag -->
<link rel="stylesheet" href="styles.css">
</head>
<body>
<!-- Your content -->
</body>
</html>
Configuration Options
Basic Configuration:
gtag('config', 'G-XXXXXXXXXX');
With Additional Settings:
gtag('config', 'G-XXXXXXXXXX', {
'page_title': 'Custom Page Title',
'page_location': 'https://example.com/custom-url',
'send_page_view': true, // Default: true
'allow_google_signals': true, // For demographics
'allow_ad_personalization_signals': true
});
Custom Event Tracking
Simple Event:
gtag('event', 'button_click', {
'button_name': 'Subscribe',
'button_location': 'header'
});
Purchase Event:
gtag('event', 'purchase', {
'transaction_id': 'TXN_12345',
'value': 99.99,
'currency': 'USD',
'tax': 5.00,
'shipping': 10.00,
'items': [
{
'item_id': 'SKU_123',
'item_name': 'Product Name',
'price': 99.99,
'quantity': 1
}
]
});
Common Implementation Patterns
Track Button Click:
<button onclick="trackButtonClick()">Subscribe</button>
<script>
function trackButtonClick() {
gtag('event', 'button_click', {
'button_name': 'Subscribe',
'button_location': 'homepage_hero'
});
}
</script>
Track Form Submission:
<form id="contact-form" onsubmit="trackFormSubmit(event)">
<input type="email" name="email" required>
<button type="submit">Submit</button>
</form>
<script>
function trackFormSubmit(e) {
e.preventDefault();
gtag('event', 'form_submit', {
'form_name': 'Contact Form',
'form_id': 'contact-form'
});
// Submit form after tracking
setTimeout(() => {
e.target.submit();
}, 100);
}
</script>
Multiple GA4 Properties
Track to Multiple Properties:
// First property
gtag('config', 'G-XXXXXXXXXX');
// Second property
gtag('config', 'G-YYYYYYYYYY');
// Events automatically sent to both
gtag('event', 'purchase', {
'value': 99.99,
'currency': 'USD'
});
Method 3: Google Tag Manager Installation
Overview
GTM provides centralized tag management with no code changes for updates. Recommended for most implementations.
Prerequisites
Required:
- GTM container created at tagmanager.google.com
- Container ID (format: GTM-XXXXXXX)
- Admin/Editor access to GTM container
GTM Container Installation
Step 1: Get Container Code
- Log in to tagmanager.google.com
- Open your container
- Click container ID (GTM-XXXXXXX) at top
- Copy both code snippets
Step 2: Install on Website
Snippet 1: Head Section
Place immediately after <head>:
<!-- Google Tag Manager -->
<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>
<!-- End Google Tag Manager -->
Snippet 2: Body Section
Place immediately after <body>:
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-XXXXXXX"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->
Complete HTML Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>My Website</title>
<!-- Google Tag Manager -->
<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>
<!-- End Google Tag Manager -->
</head>
<body>
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-XXXXXXX"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->
<!-- Your content -->
</body>
</html>
GA4 Configuration Tag Setup
Step 1: Create GA4 Configuration Tag
- GTM Workspace → Tags → New
- Click "Tag Configuration"
- Select "Google Tag"
- Enter GA4 Measurement ID in "Tag ID" field
- Configure settings (optional):
- Configuration settings
- Fields to set
Step 2: Set Trigger
- Click "Triggering"
- Select "Initialization - All Pages"
- Save tag
Tag Name: "GA4 - Configuration" or "GA4 - Base Tag"
Tag Configuration:
Tag Type: Google Tag
Tag ID: G-XXXXXXXXXX
Configuration Settings:
- Allow Google Signals: true (optional)
- Allow ad personalization signals: true (optional)
Step 3: Test in Preview Mode
- Click "Preview" (top-right)
- Connect to your website
- Verify tag fires on Initialization
- Check GA4 DebugView for events
Step 4: Publish
- Click "Submit" (top-right)
- Enter version name: "GA4 Initial Setup"
- Add description
- Click "Publish"
GA4 Event Tag Setup
Create Custom Event Tag:
- Tags → New
- Tag Configuration → Google Tag
- Tag ID: G-XXXXXXXXXX
- Event Name: "button_click"
- Event Parameters:
- Parameter: button_name → Value: {{Click Text}}
- Parameter: button_location → Value: "header"
- Triggering: Click trigger
- Save tag
GTM Best Practices
Tag Naming:
- Use prefix: "GA4 - "
- Examples: "GA4 - Configuration", "GA4 - Purchase", "GA4 - Form Submit"
Container Organization:
- Folder for GA4 tags
- Folder for GA4 variables
- Clear naming conventions
Testing Before Publishing:
- Always use Preview mode
- Test all triggers
- Verify event parameters
- Check DebugView
Installation Verification (All Methods)
Quick Verification Steps
Step 1: Enable Google Analytics Debugger
- Install "Google Analytics Debugger" Chrome extension
- Enable extension
- Visit your website
Step 2: Check DebugView
- GA4 Property → Admin → DebugView
- Select your device from dropdown
- Confirm events appearing:
- session_start
- first_visit (if new user)
- page_view
Step 3: Check Realtime Reports
- GA4 Property → Reports → Realtime
- Confirm showing 1+ active users
- Verify events by name
Step 4: Verify Event Parameters
- DebugView → Click event
- Review parameters panel
- Confirm all expected parameters present
Troubleshooting Installation
No Data Appearing
Checklist:
- Correct Measurement ID (G-XXXXXXXXXX)?
- Code in correct location (
<head>for gtag/GTM)? - Website published/live?
- Tracking code on all pages?
- Ad blockers disabled for testing?
- Waited 24 hours? (DebugView is instant, reports delayed)
Solutions:
- Verify Measurement ID matches Data Stream
- Check browser console for errors
- Use Tag Assistant to diagnose
- Test in incognito mode
- Check if ad blocker blocking
Data Only in DebugView
Cause: Debug mode parameter enabled
Solution:
- Remove
debug_mode: truefrom events - Disable Google Analytics Debugger extension
- GTM: Exit Preview mode
- Wait 24-48 hours for standard reports
Duplicate Events
Cause: Multiple tracking implementations
Solutions:
- Check for both gtag.js AND GTM (remove one)
- Check for plugin AND manual code (remove one)
- Verify not tracking to same ID twice
- Check theme and plugins for conflicts
Wrong Data Stream
Symptoms: Events appear but in wrong property/stream
Solution:
- Verify Measurement ID
- Check Data Streams list
- Confirm using correct ID for platform
- Update code with correct ID
Migration from Universal Analytics
Running Both UA and GA4
Recommended: Run both during transition period
Implementation:
If using gtag.js:
<!-- Universal Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXXX-X"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-XXXXXXXXX-X');
gtag('config', 'G-XXXXXXXXXX'); // GA4
</script>
If using GTM:
- Keep existing UA tag
- Add new GA4 Configuration tag
- Both fire simultaneously
- No conflicts between UA and GA4
Additional Resources
- Official Google: Installation Guide
- Official Google: Verify Installation
- See verification-checklist.md for detailed testing procedures
- See ga4-gtm-integration skill for advanced GTM configurations