|
| 1 | +# Google Analytics Integration |
| 2 | + |
| 3 | +This document describes how Google Analytics 4 (GA4) has been integrated into the DataSpace Frontend application. |
| 4 | + |
| 5 | +## Setup |
| 6 | + |
| 7 | +### 1. Environment Configuration |
| 8 | + |
| 9 | +Add your Google Analytics tracking ID to your environment variables: |
| 10 | + |
| 11 | +```bash |
| 12 | +# .env.local |
| 13 | +NEXT_PUBLIC_GA_ID='G-XXXXXXXXXX' |
| 14 | +``` |
| 15 | + |
| 16 | +Replace `G-XXXXXXXXXX` with your actual Google Analytics 4 tracking ID. |
| 17 | + |
| 18 | +### 2. Files Added/Modified |
| 19 | + |
| 20 | +- `lib/gtag.ts` - Core Google Analytics utilities and tracking functions |
| 21 | +- `components/GoogleAnalytics/` - React component for GA initialization |
| 22 | +- `hooks/use-analytics.ts` - Custom hook for easy analytics tracking |
| 23 | +- `types/index.d.ts` - TypeScript definitions for gtag |
| 24 | +- `env.ts` - Environment variable validation |
| 25 | +- `app/[locale]/layout.tsx` - GA component integration |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +### Basic Tracking |
| 30 | + |
| 31 | +The Google Analytics component is automatically loaded in the main layout and will track page views automatically. |
| 32 | + |
| 33 | +### Custom Event Tracking |
| 34 | + |
| 35 | +Use the `useAnalytics` hook for custom event tracking: |
| 36 | + |
| 37 | +```tsx |
| 38 | +import { useAnalytics } from '@/hooks/use-analytics'; |
| 39 | + |
| 40 | +const MyComponent = () => { |
| 41 | + const { track, trackDataset, trackUsecase, trackSearch } = useAnalytics(); |
| 42 | + |
| 43 | + const handleClick = () => { |
| 44 | + track('button_click', { button_name: 'download' }); |
| 45 | + }; |
| 46 | + |
| 47 | + const handleDatasetView = (datasetId: string, title?: string) => { |
| 48 | + trackDataset(datasetId, title); |
| 49 | + }; |
| 50 | + |
| 51 | + // ... rest of component |
| 52 | +}; |
| 53 | +``` |
| 54 | + |
| 55 | +### Available Tracking Functions |
| 56 | + |
| 57 | +#### From `lib/gtag.ts`: |
| 58 | +- `pageview(url)` - Track page views |
| 59 | +- `event({ action, category, label, value })` - Generic event tracking |
| 60 | +- `trackEvent(eventName, parameters)` - Custom event tracking |
| 61 | +- `trackPageView(pagePath, pageTitle)` - Page view tracking |
| 62 | +- `trackUserInteraction(action, element)` - User interaction tracking |
| 63 | +- `trackDatasetView(datasetId, datasetTitle)` - Dataset view tracking |
| 64 | +- `trackUsecaseView(usecaseId, usecaseTitle)` - Usecase view tracking |
| 65 | +- `trackSearch(query, resultCount)` - Search query tracking |
| 66 | +- `trackDownload(fileName, fileType)` - File download tracking |
| 67 | + |
| 68 | +#### From `useAnalytics` hook: |
| 69 | +- `track(eventName, parameters)` - Generic event tracking |
| 70 | +- `trackPage(pagePath, pageTitle)` - Page view tracking |
| 71 | +- `trackInteraction(action, element)` - User interaction tracking |
| 72 | +- `trackDataset(datasetId, datasetTitle)` - Dataset view tracking |
| 73 | +- `trackUsecase(usecaseId, usecaseTitle)` - Usecase view tracking |
| 74 | +- `trackSearchQuery(query, resultCount)` - Search query tracking |
| 75 | +- `trackFileDownload(fileName, fileType)` - File download tracking |
| 76 | + |
| 77 | +## Examples |
| 78 | + |
| 79 | +### Track Dataset Views |
| 80 | +```tsx |
| 81 | +useEffect(() => { |
| 82 | + if (datasetId) { |
| 83 | + trackDataset(datasetId, datasetTitle); |
| 84 | + } |
| 85 | +}, [datasetId, datasetTitle, trackDataset]); |
| 86 | +``` |
| 87 | + |
| 88 | +### Track Search Queries |
| 89 | +```tsx |
| 90 | +const handleSearch = (query: string, results: any[]) => { |
| 91 | + trackSearchQuery(query, results.length); |
| 92 | +}; |
| 93 | +``` |
| 94 | + |
| 95 | +### Track File Downloads |
| 96 | +```tsx |
| 97 | +const handleDownload = (fileName: string) => { |
| 98 | + trackFileDownload(fileName, fileName.split('.').pop()); |
| 99 | +}; |
| 100 | +``` |
| 101 | + |
| 102 | +### Track User Interactions |
| 103 | +```tsx |
| 104 | +const handleButtonClick = () => { |
| 105 | + trackInteraction('click', 'export_button'); |
| 106 | +}; |
| 107 | +``` |
| 108 | + |
| 109 | +## Implementation Details |
| 110 | + |
| 111 | +### Automatic Page Tracking |
| 112 | + |
| 113 | +The `GoogleAnalytics` component automatically tracks page views using Next.js router events. It's integrated into the main layout at `app/[locale]/layout.tsx`. |
| 114 | + |
| 115 | +### Privacy Considerations |
| 116 | + |
| 117 | +- Google Analytics only loads when `NEXT_PUBLIC_GA_ID` is provided |
| 118 | +- All tracking functions check for the presence of `window.gtag` before executing |
| 119 | +- The implementation follows Google's recommended practices for GA4 |
| 120 | + |
| 121 | +### TypeScript Support |
| 122 | + |
| 123 | +Full TypeScript support is provided with proper type definitions for the `gtag` function and all tracking utilities. |
| 124 | + |
| 125 | +## Testing |
| 126 | + |
| 127 | +To test the implementation: |
| 128 | + |
| 129 | +1. Set up a Google Analytics 4 property |
| 130 | +2. Add the tracking ID to your `.env.local` file |
| 131 | +3. Run the application in development mode |
| 132 | +4. Open browser developer tools and check the Network tab for GA requests |
| 133 | +5. Use Google Analytics Real-time reports to verify events are being tracked |
| 134 | + |
| 135 | +## Troubleshooting |
| 136 | + |
| 137 | +### GA not loading |
| 138 | +- Verify `NEXT_PUBLIC_GA_ID` is set correctly |
| 139 | +- Check browser console for any JavaScript errors |
| 140 | +- Ensure ad blockers are not interfering |
| 141 | + |
| 142 | +### Events not tracking |
| 143 | +- Verify the `gtag` function is available (`window.gtag`) |
| 144 | +- Check that events are being called after GA initialization |
| 145 | +- Use browser developer tools to inspect network requests to Google Analytics |
| 146 | + |
| 147 | +### Development vs Production |
| 148 | +- GA tracking works in both development and production |
| 149 | +- Use different GA properties for development and production environments |
| 150 | +- Consider using Google Analytics Debug mode for development testing |
0 commit comments