Google Analytics 4 Setup Guide
Overview​
Your platform now has comprehensive Google Analytics 4 (GA4) integration that tracks:
- ✅ Page views across all routes
- ✅ Blog article views with category tracking
- ✅ Time spent on pages
- ✅ Scroll depth on blog articles
- ✅ Share and copy link actions
- ✅ And more custom events ready to use!
Step 1: Create a Google Analytics 4 Property​
-
Go to Google Analytics
- Sign in with your Google account
-
Create a new GA4 property:
- Click Admin (bottom left)
- Under "Property" column, click Create Property
- Enter your property name (e.g., "AI Agentification Platform")
- Select your timezone and currency
- Click Next
-
Business details:
- Select your industry category
- Choose business size
- Select how you plan to use Google Analytics
- Click Create
-
Accept the Terms of Service
-
Set up a Data Stream:
- Select Web platform
- Enter your website URL (e.g.,
https://agentification.academy) - Enter stream name (e.g., "Production Website")
- Click Create stream
-
Copy your Measurement ID:
- You'll see a Measurement ID like
G-XXXXXXXXXX - Copy this ID - you'll need it in the next step
- You'll see a Measurement ID like
Step 2: Add Measurement ID to Your Environment Variables​
For Development:​
- Open
.env.developmentor.env.local - Add this line:
(ReplaceVITE_GA_MEASUREMENT_ID=G-XXXXXXXXXX
G-XXXXXXXXXXwith your actual Measurement ID)
For Production (Vercel):​
- Go to your Vercel Dashboard
- Select your project
- Go to Settings → Environment Variables
- Add new variable:
- Name:
VITE_GA_MEASUREMENT_ID - Value:
G-XXXXXXXXXX - Environment: Select "Production" (and optionally Preview, Development)
- Name:
- Click Save
- Redeploy your application for changes to take effect
Step 3: Verify Installation​
Method 1: Using GA4 Real-Time Reports​
- Go to your GA4 property
- Click Reports → Realtime
- Visit your website in a new browser tab
- You should see your visit appear in real-time within ~30 seconds
Method 2: Using Browser Console​
- Open your website
- Open browser DevTools (F12)
- Go to Console tab
- You should see:
Google Analytics 4 initialized - Navigate between pages - you should see page views being tracked
Method 3: Using GA Debugger Chrome Extension​
- Install Google Analytics Debugger
- Enable the extension
- Visit your website
- Open browser console to see detailed GA4 event tracking
Step 4: What's Being Tracked​
Automatic Tracking:​
- All page views - Every route change is tracked
- Blog article views - Special tracking with article title and category
- Time on page - Tracked when users leave or navigate away
- Scroll depth - How far users scroll on blog articles
Custom Events Ready to Use:​
See src/services/analytics.js for all available tracking functions:
// Already implemented in ArticlePage:
trackBlogView(slug, title, category)
trackTimeOnPage(pageName, seconds)
trackScrollDepth(pageName, percentage)
trackEvent(category, action, label)
// Available for you to add:
trackCourseEnrollment(courseName, courseId)
trackVideoWatchTime(videoTitle, seconds)
trackSearch(searchTerm, resultCount)
trackUserLogin(method)
trackUserSignup(method)
trackFormSubmit(formName, formType)
Step 5: View Your Analytics​
Basic Reports (Available after 24-48 hours):​
- Real-time: See current visitors
- Engagement → Pages and screens: Most viewed pages
- Acquisition → User acquisition: Where users come from
- Demographics → Overview: User locations, languages
- Tech → Overview: Devices, browsers, OS
Blog-Specific Insights:​
Go to Reports → Engagement → Events:
view_blog- Blog article views with article detailstime_on_page- How long users spend on blog postsscroll_depth- How much of articles users readshare/copy_link- Article sharing behavior
Custom Reports:​
- Go to Explore tab
- Create custom reports to analyze:
- Most popular blog categories
- Average time spent per article
- User journey from blog to course enrollment
- Geographic distribution of blog readers
Step 6: Optional - Enhanced Tracking​
Add to Login/Signup Pages:​
In your LoginPage.jsx and SignupPage.jsx, add:
import { trackUserLogin, trackUserSignup } from '../src/services/analytics';
// In your login success handler:
trackUserLogin('email'); // or 'google', 'linkedin'
// In your signup success handler:
trackUserSignup('email');
Add to Course Enrollment:​
In your enrollment logic, add:
import { trackCourseEnrollment } from '../src/services/analytics';
trackCourseEnrollment(courseName, courseId);
Add Search Tracking:​
In your blog search component:
import { trackSearch } from '../src/services/analytics';
trackSearch(searchQuery, resultsCount);
Privacy & GDPR Compliance​
The GA4 setup includes:
- ✅ IP anonymization enabled (
anonymizeIp: true) - ✅ No personally identifiable information (PII) is tracked
- ✅ Cookie consent is recommended (not yet implemented)
Recommended: Add a cookie consent banner to comply with GDPR/CCPA. Libraries like:
Troubleshooting​
Analytics not showing in GA4:​
- ✅ Check console for "Google Analytics 4 initialized" message
- ✅ Verify
VITE_GA_MEASUREMENT_IDis set correctly - ✅ Make sure it starts with
G-notUA-(UA is old Universal Analytics) - ✅ Wait 24-48 hours for full reports (Real-time works immediately)
- ✅ Check you're in the correct property in GA4
Events not showing:​
- ✅ Use Real-time → Events to see events immediately
- ✅ Check browser console for errors
- ✅ Verify you're calling the tracking functions correctly
Need Help?​
Next Steps​
- Set up GA4 property (15 minutes)
- Add Measurement ID to environment variables
- Deploy to Vercel (with new env variable)
- Verify tracking using Real-time reports
- Wait 24-48 hours for full data
- Create custom reports for blog performance
- Optional: Add cookie consent banner
- Optional: Integrate GA4 API into admin dashboard (see below)
Future: GA4 API Integration in Admin Dashboard​
Want to display GA4 metrics directly in your admin panel? You can use the GA4 Reporting API to fetch:
- Top blog articles by views
- Average time on page
- Geographic breakdown
- Traffic sources
- Real-time visitor count
This would require:
- Setting up a Google Cloud project
- Enabling GA4 Reporting API
- Creating service account credentials
- Building a backend endpoint to fetch GA4 data
- Displaying metrics in
pages/admin/Analytics.jsx
Let me know if you'd like help implementing this!
Congratulations! 🎉 Your platform now has enterprise-grade analytics tracking.