Event Tracking Setup Guide
Overview
Iβve implemented a centralized event tracking system for your Patterns Next application, with full Google Analytics 4 (GA4) integration. The system tracks user interactions across your site and sends data to GA4.
β Google Analytics 4 Setup Complete
The GA4 tracking is now fully configured with double-layer protection against dev/local execution:
- Environment Variable: Set
NEXT_PUBLIC_GA_IDin your.env.localfile - Script Loading: GA4 script only loads in production (
NODE_ENV === 'production') - Event Tracking: Additional runtime check blocks events on localhost/127.0.0.1/*.local
- TypeScript Support: Full type definitions for gtag included
Development Safety
β
GA script will NOT load in development - The script tag is conditionally rendered only when NODE_ENV === 'production'
β Events are blocked in dev - Even if script somehow loads, the analytics module detects localhost and blocks all events
β Console logging enabled - See all events in browser console during development
Quick Start
- Create a
.env.localfile (use.env.local.exampleas template):NEXT_PUBLIC_GA_ID=G-K1F96PRZNS - Start your development server:
npm run dev - Events will now be sent to Google Analytics 4 in production environments
Events Tracked
1. Bookmark Open
- Event Name:
bookmark_drawer_open - Category: engagement
- Triggered: When user opens the bookmark drawer
- Location: src/components/BookmarkDrawer.tsx
2. Bookmark Item Click
- Event Name:
bookmark_item_click - Category: engagement
- Triggered: When user clicks a saved pattern in the bookmark drawer
- Metadata: pageId, pageTitle, slug
- Location: src/components/BookmarkDrawer.tsx
3. Saved Pattern from Footer
- Event Name:
save_pattern_from_footer - Category: conversion
- Triggered: When user clicks βSave this patternβ button at bottom of detail pages
- Metadata: pageId, pageTitle
- Location: src/components/NotionPageRenderer.tsx
4. Saved Pattern from Card Thumbnail
- Event Name:
save_pattern_from_card - Category: conversion
- Triggered: When user clicks bookmark button on collection card
- Metadata: pageId, pageTitle, device (desktop/tablet/phone)
- Location: src/components/CollectionCard.tsx
5. Image Clicked / Zoom Opened
- Event Name:
image_clicked - Category: engagement
- Triggered: When user clicks on any image in a detail page
- Metadata: imageUrl (sanitized), pageTitle
- Location: src/components/NotionPageRenderer.tsx
File Structure
.env.local.example # Environment variable template
src/
βββ types/
β βββ gtag.d.ts # TypeScript definitions for GA4
βββ lib/
β βββ analytics.ts # Core analytics module with GA4 integration
βββ app/
β βββ layout.tsx # GA4 script injection
βββ components/
βββ BookmarkButton.tsx # Updated with tracking
βββ BookmarkDrawer.tsx # Updated with tracking
βββ CollectionCard.tsx # Updated with tracking
βββ NotionPageRenderer.tsx # Updated with tracking
Implementation Details
Analytics Module (src/lib/analytics.ts)
The analytics module provides:
analytics.trackEvent(event)- Generic event trackinganalytics.trackBookmarkDrawerOpen()- Bookmark drawer openanalytics.trackBookmarkItemClick(pageTitle, pageId, slug)- Bookmark item clickanalytics.trackSavePatternFromFooter(pageTitle, pageId)- Footer saveanalytics.trackSavePatternFromCard(pageTitle, pageId, device)- Card saveanalytics.trackImageClicked(imageUrl, pageTitle)- Image clickanalytics.trackRemoveBookmark(pageTitle, pageId)- Bookmark removal
Configuration
To configure analytics, update the analytics instance in your app:
import { analytics } from "@/lib/analytics";
// Enable/disable console logging (default: true)
analytics.configure({
enableLogging: process.env.NODE_ENV === "development",
// Google Analytics ID (optional)
googleAnalyticsId: process.env.NEXT_PUBLIC_GA_ID,
// Custom tracking endpoint (optional)
customEndpoint: process.env.NEXT_PUBLIC_ANALYTICS_ENDPOINT,
});
Integration with Google Analytics
β Already Configured! The GA4 integration is complete and ready to use.
Whatβs been set up:
- GA4 script loads only in production (
NODE_ENV === 'production') - src/app/layout.tsx - Uses
NEXT_PUBLIC_GA_IDenvironment variable - TypeScript types for gtag in src/types/gtag.d.ts
- GA4-compatible event parameter names
- Runtime protection: auto-disabled on localhost/127.0.0.1/*.local hostnames - src/lib/analytics.ts
To activate:
- Set
NEXT_PUBLIC_GA_ID=G-K1F96PRZNSin.env.local - Deploy or restart your dev server
- Events will automatically flow to GA4
Viewing Events in GA4:
- Go to your GA4 property β Reports β Realtime
- Events will appear as theyβre triggered
- Custom parameters (event_category, event_label, etc.) are included
Integration with Custom Backend
To send events to a custom tracking endpoint:
- Set environment variable:
NEXT_PUBLIC_ANALYTICS_ENDPOINT=https://your-api.com/analytics - Configure analytics:
analytics.configure({ customEndpoint: process.env.NEXT_PUBLIC_ANALYTICS_ENDPOINT, });
Event Data Structure
Each event contains:
{
eventName: string; // e.g., "save_pattern_from_footer"
eventCategory?: string; // e.g., "conversion"
eventLabel?: string; // e.g., page title
eventValue?: number; // numeric value (optional)
metadata?: Record<string, any>; // additional context
timestamp?: number; // auto-added
}
Console Output (Development)
In development mode, all events are logged to console:
[Analytics Event] {
eventName: 'save_pattern_from_footer',
eventCategory: 'conversion',
eventLabel: 'Grid Pattern',
timestamp: 1706729400000,
metadata: {
action: 'save',
source: 'footer',
pageId: 'abc123',
pageTitle: 'Grid Pattern',
component: 'NotionPageRenderer'
}
}
Testing Events
Development Environment
- Console Logging: Events are logged to browser console in development:
[Analytics Event] β (disabled on dev) { eventName: 'save_pattern_from_footer', eventCategory: 'conversion', eventLabel: 'Grid Pattern', timestamp: 1706729400000, metadata: {...} } - Force Enable Tracking: To test GA4 in localhost, modify src/lib/analytics.ts:
enableTracking: true // Force enable for testing
Production Environment
- Google Analytics Real-time:
- Open GA4 β Reports β Realtime β Events
- Perform actions on your site
- See events appear in real-time
- Browser DevTools:
- Open Network tab
- Filter by βcollectβ or βgtagβ
- See GA4 requests being sent
- GA4 DebugView (Recommended):
- Install Google Analytics Debugger extension
- Enable it and refresh your site
- View in GA4 β Configure β DebugView
Extending Analytics
To add new events, add methods to the Analytics class in src/lib/analytics.ts:
trackCustomEvent(params: string): void {
this.trackEvent({
eventName: "custom_event",
eventCategory: "engagement",
eventLabel: params,
metadata: {
// your metadata
},
});
}
Then import and use in components:
import { analytics } from "@/lib/analytics";
analytics.trackCustomEvent("value");
Privacy & Security
- Image URLs are sanitized before sending to avoid exposing sensitive data
- Only up to 50 characters of URLs are logged
- No personally identifiable information is captured
- Events can be disabled by setting
enableLogging: false
Notes
- All tracking is asynchronous and wonβt block user interactions
- Events are logged before state changes to ensure accurate data
- The tracking system is framework-agnostic and can be used anywhere in your app
- Console logging helps with development and debugging