How to Add Google Analytics to Your Website
Complete guide to setting up Google Analytics 4 on your website
Updated 2024-12-21
How to Add Google Analytics to Your Website
Google Analytics 4 (GA4) is essential for understanding your website's traffic, user behavior, and conversions. This guide walks you through adding GA4 to your website from scratch.
Prerequisites
Before you begin, you'll need:
- A Google account
- Access to your website's code or CMS
- Admin access to add tracking scripts
Step 1: Create a Google Analytics Account
- Go to analytics.google.com
- Click "Start measuring" or "Create Account"
- Enter your Account Name (e.g., your business name)
- Configure data sharing settings (recommended: keep defaults)
- Click "Next"
Step 2: Set Up a GA4 Property
- Enter your Property Name (e.g., "My Website")
- Select your Reporting Time Zone and Currency
- Click "Next"
- Choose your Industry Category and Business Size
- Click "Create"
Step 3: Create a Data Stream
- Select "Web" as your platform
- Enter your Website URL (e.g.,
https://example.com) - Enter a Stream Name (e.g., "Main Website")
- Click "Create Stream"
Step 4: Get Your Measurement ID
After creating your data stream, you'll see:
- Measurement ID: Format
G-XXXXXXXXXX - Stream URL: Your website URL
- Stream ID: Internal identifier
Important: Copy your Measurement ID - you'll need this for the next step.
Step 5: Add the Tracking Code
Option A: Google Tag (Recommended)
Add this code to the <head> section of every page:
<!-- 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>
Replace
G-XXXXXXXXXXwith your actual Measurement ID
Option B: Google Tag Manager
If you're using GTM:
- Open your GTM container
- Create a new Tag → Select "Google Analytics: GA4 Configuration"
- Enter your Measurement ID
- Set trigger to "All Pages"
- Publish the container
Option C: CMS-Specific Setup
| Platform | Method |
|---|---|
| WordPress | Install "Site Kit by Google" or "GA Google Analytics" plugin |
| Shopify | Settings → Online Store → Preferences → Google Analytics |
| Wix | Marketing Integrations → Google Analytics |
| Squarespace | Settings → Advanced → External Services |
| Webflow | Project Settings → Integrations → Google Analytics |
Step 6: Verify Installation
Method 1: Real-Time Reports
- Open your website in a new tab
- In GA4, go to Reports → Realtime
- You should see your visit appear within 30 seconds
Method 2: Google Tag Assistant
- Install the Tag Assistant Chrome extension
- Visit your website
- Check that GA4 tag shows as "connected"
Method 3: Browser Developer Tools
- Open DevTools (F12)
- Go to Network tab
- Filter by
collectorgoogle-analytics - Refresh the page and check for requests
Step 7: Configure Essential Settings
Once installed, configure these important settings:
Enable Enhanced Measurement
- Go to Admin → Data Streams → Your Stream
- Click Enhanced Measurement
- Enable:
- ✅ Page views
- ✅ Scrolls
- ✅ Outbound clicks
- ✅ Site search
- ✅ Video engagement
- ✅ File downloads
Set Up Conversions
- Go to Admin → Conversions
- Click "New Conversion Event"
- Enter event names like:
purchasegenerate_leadsign_upcontact_form_submit
Configure Data Retention
- Go to Admin → Data Settings → Data Retention
- Set retention to 14 months (maximum free)
- Enable "Reset user data on new activity"
Connecting GA4 to Glarify
Once GA4 is installed and collecting data:
- Go to Settings → Integrations in Glarify
- Click "Sync Properties from Google"
- Select your GA4 property from the list
- Click "Connect"
Your GA4 data will now appear in:
- Analytics Dashboard - Full GA4 reports
- All-in-One View - Combined with GSC data
- AI Insights - AI-powered analysis of your traffic
Troubleshooting
| Issue | Solution |
|---|---|
| No data appearing | Wait 24-48 hours for data to populate |
| Realtime works but reports empty | Check date range includes today |
| Double-counting pageviews | Remove duplicate gtag code snippets |
| Bot traffic inflating numbers | Enable bot filtering in Admin → Data Settings |
| GA4 not showing in Glarify | Ensure your Google account has Viewer access |
Best Practices
- Don't install multiple GA4 codes - This causes double-counting
- Use Google Tag Manager for complex setups - Easier to manage
- Set up cross-domain tracking if you have multiple domains
- Create custom events for important user actions
- Link to Google Search Console for SEO insights