Google Analytics Setup in MakerKit
Add Google Analytics 4 (GA4) to your MakerKit application for page views, user tracking, and conversion measurement.
Google Analytics 4 (GA4) provides web analytics focused on marketing attribution, conversion tracking, and audience insights. Use it when your marketing team needs Google's ecosystem for ad optimization and reporting.
Prerequisites
Before starting, you need:
- A Google Analytics 4 property (create one here)
- Your GA4 Measurement ID (format:
G-XXXXXXXXXX)
Find your Measurement ID in GA4: Admin > Data Streams > Select your stream > Measurement ID.
Installation
Install the Google Analytics plugin using the MakerKit CLI:
npx @makerkit/cli@latest plugins installSelect Google Analytics from the list. This creates the plugin at packages/plugins/google-analytics.
Add the plugin as a dependency to the analytics package:
pnpm add "@kit/google-analytics@workspace:*" --filter "@kit/analytics" -DConfiguration
Register the provider in your analytics configuration:
packages/analytics/src/index.ts
import { createGoogleAnalyticsService } from '@kit/google-analytics';import { createAnalyticsManager } from './analytics-manager';import type { AnalyticsManager } from './types';export const analytics: AnalyticsManager = createAnalyticsManager({ providers: { 'google-analytics': createGoogleAnalyticsService, },});Add your Measurement ID to environment variables:
.env.local
NEXT_PUBLIC_GA_MEASUREMENT_ID=G-XXXXXXXXXXEnvironment Variables
| Variable | Required | Description |
|---|---|---|
NEXT_PUBLIC_GA_MEASUREMENT_ID | Yes | Your GA4 Measurement ID |
NEXT_PUBLIC_GA_DISABLE_PAGE_VIEWS_TRACKING | No | Set to true to disable automatic page view tracking |
NEXT_PUBLIC_GA_DISABLE_LOCALHOST_TRACKING | No | Set to true to disable tracking on localhost |
Development Configuration
Disable localhost tracking to avoid polluting your analytics during development:
.env.local
NEXT_PUBLIC_GA_MEASUREMENT_ID=G-XXXXXXXXXXNEXT_PUBLIC_GA_DISABLE_LOCALHOST_TRACKING=trueVerification
After configuration, verify the integration:
- Open your application in the browser
- Open Chrome DevTools > Network tab
- Filter by
google-analyticsorgtag - Navigate between pages and confirm requests are sent
- Check GA4 Realtime reports to see your session
Using with Other Providers
Google Analytics can run alongside other providers. Events dispatch to all registered providers:
packages/analytics/src/index.ts
import { createGoogleAnalyticsService } from '@kit/google-analytics';import { createPostHogAnalyticsService } from '@kit/posthog/client';import { createAnalyticsManager } from './analytics-manager';export const analytics = createAnalyticsManager({ providers: { 'google-analytics': createGoogleAnalyticsService, posthog: createPostHogAnalyticsService, },});This setup is common when marketing uses GA4 for attribution while product uses PostHog for behavior analysis.
Tracked Events
With the default configuration, Google Analytics receives:
- Page views: Automatically tracked on route changes
- User identification: When
analytics.identify()is called - Custom events: All events passed to
analytics.trackEvent()
Events from the App Events system (user.signedUp, checkout.started, etc.) are forwarded to GA4 through the analytics mapping.
GDPR Considerations
Google Analytics sets cookies and requires user consent in the EU. Integrate with the Cookie Banner component to manage consent:
import { useCookieConsent, ConsentStatus } from '@kit/ui/cookie-banner';import { analytics } from '@kit/analytics';import { useEffect } from 'react';function AnalyticsGate({ children }) { const { status } = useCookieConsent(); useEffect(() => { if (status === ConsentStatus.Accepted) { // GA is initialized automatically when consent is given // You may want to delay initialization until consent } }, [status]); return children;}Consider using Umami for cookie-free, GDPR-compliant analytics.
Troubleshooting
Events not appearing in GA4
- Verify your Measurement ID is correct
- Check that
NEXT_PUBLIC_GA_DISABLE_LOCALHOST_TRACKINGis nottruein production - GA4 has a delay of up to 24-48 hours for some reports. Use Realtime for immediate verification
Duplicate page views
- Ensure you have not called
trackPageViewmanually. MakerKit tracks page views automatically
Ad blockers
- Ad blockers often block Google Analytics. Consider using a proxy or server-side tracking for critical metrics
Frequently Asked Questions
Does MakerKit support Universal Analytics?
Can I use Google Tag Manager instead?
How do I track conversions?
Is server-side tracking supported?
Next Steps
- Learn about Analytics and Events for custom event tracking
- Add PostHog for product analytics
- Create a custom provider for other services