|
| 1 | +# Analytics Integration Guide |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This document provides comprehensive guidance on the analytics integration implemented in the Devlander Software documentation system. The integration includes Google Ads, Google Analytics, and cookie consent management with GDPR compliance. |
| 6 | + |
| 7 | +## Architecture |
| 8 | + |
| 9 | +### Components |
| 10 | + |
| 11 | +1. **AnalyticsProvider** (`src/components/AnalyticsProvider.tsx`) |
| 12 | + - Main component for initializing Google Ads and Analytics |
| 13 | + - Handles script loading and configuration |
| 14 | + - Manages cookie consent integration |
| 15 | + |
| 16 | +2. **useAnalytics Hook** (`src/hooks/useAnalytics.ts`) |
| 17 | + - Primary hook for tracking user interactions |
| 18 | + - Provides memoized tracking functions |
| 19 | + - Automatically tracks page views on route changes |
| 20 | + |
| 21 | +3. **Analytics Library** (`src/lib/analytics.ts`) |
| 22 | + - Core tracking functions and utilities |
| 23 | + - Cookie consent validation |
| 24 | + - Event and conversion tracking |
| 25 | + |
| 26 | +4. **CookieConsent Component** (`src/components/CookieConsent.tsx`) |
| 27 | + - GDPR-compliant cookie consent management |
| 28 | + - Local storage integration |
| 29 | + - User preference management |
| 30 | + |
| 31 | +## Features |
| 32 | + |
| 33 | +### Core Tracking Functions |
| 34 | + |
| 35 | +- **Page Views**: Automatic tracking on route changes |
| 36 | +- **Custom Events**: Flexible event tracking with parameters |
| 37 | +- **Conversions**: Google Ads conversion tracking |
| 38 | +- **Form Submissions**: Form interaction tracking |
| 39 | +- **Button Clicks**: Button interaction tracking |
| 40 | +- **Downloads**: File download tracking |
| 41 | +- **Search Queries**: Search interaction tracking |
| 42 | +- **Outbound Links**: External link tracking |
| 43 | +- **Video Engagement**: Video interaction tracking |
| 44 | +- **E-commerce**: Transaction tracking |
| 45 | + |
| 46 | +### Specialized Hooks |
| 47 | + |
| 48 | +- `useFormTracking(formName, formId)`: Form-specific tracking |
| 49 | +- `useButtonTracking(buttonName, buttonId)`: Button-specific tracking |
| 50 | +- `useDownloadTracking()`: Download tracking |
| 51 | +- `useSearchTracking()`: Search tracking |
| 52 | + |
| 53 | +## Usage Examples |
| 54 | + |
| 55 | +### Basic Analytics Usage |
| 56 | + |
| 57 | +```tsx |
| 58 | +import { useAnalytics } from '../hooks/useAnalytics' |
| 59 | + |
| 60 | +function MyComponent() { |
| 61 | + const { trackEvent, trackConversion, isEnabled } = useAnalytics() |
| 62 | + |
| 63 | + const handleClick = () => { |
| 64 | + trackEvent('button_click', { |
| 65 | + component: 'MyComponent', |
| 66 | + action: 'primary_button' |
| 67 | + }, 'engagement', 'my-component') |
| 68 | + } |
| 69 | + |
| 70 | + const handlePurchase = () => { |
| 71 | + trackConversion(undefined, undefined, 99.99, 'USD') |
| 72 | + } |
| 73 | + |
| 74 | + return ( |
| 75 | + <div> |
| 76 | + {isEnabled && ( |
| 77 | + <button onClick={handleClick}> |
| 78 | + Track Event |
| 79 | + </button> |
| 80 | + )} |
| 81 | + </div> |
| 82 | + ) |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +### Form Tracking |
| 87 | + |
| 88 | +```tsx |
| 89 | +import { useFormTracking } from '../hooks/useAnalytics' |
| 90 | + |
| 91 | +function ContactForm() { |
| 92 | + const { handleFormSubmit, handleButtonClick } = useFormTracking('contact-form', 'contact-form-1') |
| 93 | + |
| 94 | + const onSubmit = (formData) => { |
| 95 | + handleFormSubmit({ |
| 96 | + form_type: 'contact', |
| 97 | + user_agent: navigator.userAgent, |
| 98 | + timestamp: Date.now() |
| 99 | + }) |
| 100 | + } |
| 101 | + |
| 102 | + const onButtonClick = (buttonName) => { |
| 103 | + handleButtonClick(buttonName, 'submit-button') |
| 104 | + } |
| 105 | + |
| 106 | + return ( |
| 107 | + <form onSubmit={onSubmit}> |
| 108 | + <button onClick={() => onButtonClick('submit')}> |
| 109 | + Submit |
| 110 | + </button> |
| 111 | + </form> |
| 112 | + ) |
| 113 | +} |
| 114 | +``` |
| 115 | + |
| 116 | +### Download Tracking |
| 117 | + |
| 118 | +```tsx |
| 119 | +import { useDownloadTracking } from '../hooks/useAnalytics' |
| 120 | + |
| 121 | +function DownloadButton() { |
| 122 | + const { handleDownload } = useDownloadTracking() |
| 123 | + |
| 124 | + const onDownload = () => { |
| 125 | + handleDownload('document.pdf', 'pdf', { |
| 126 | + file_size: '2.5MB', |
| 127 | + download_source: 'homepage' |
| 128 | + }) |
| 129 | + } |
| 130 | + |
| 131 | + return ( |
| 132 | + <button onClick={onDownload}> |
| 133 | + Download PDF |
| 134 | + </button> |
| 135 | + ) |
| 136 | +} |
| 137 | +``` |
| 138 | + |
| 139 | +## Configuration |
| 140 | + |
| 141 | +### Environment Variables |
| 142 | + |
| 143 | +Create a `.env.local` file with the following variables: |
| 144 | + |
| 145 | +```env |
| 146 | +# Google Ads Configuration |
| 147 | +NEXT_PUBLIC_GOOGLE_ADS_ID=AW-XXXXXXXXX |
| 148 | +NEXT_PUBLIC_GOOGLE_ADS_CONVERSION_ID=AW-XXXXXXXXX |
| 149 | +NEXT_PUBLIC_GOOGLE_ADS_CONVERSION_LABEL=XXXXXXXXX |
| 150 | +
|
| 151 | +# Google Analytics Configuration |
| 152 | +NEXT_PUBLIC_GOOGLE_ANALYTICS_ID=G-XXXXXXXXXX |
| 153 | +
|
| 154 | +# Google AdSense Configuration |
| 155 | +NEXT_PUBLIC_GOOGLE_ADSENSE_CLIENT=ca-pub-XXXXXXXXXXXXXXXX |
| 156 | +
|
| 157 | +# Site Configuration |
| 158 | +NEXT_PUBLIC_SITE_URL=https://your-domain.com |
| 159 | +NEXT_PUBLIC_SITE_NAME=Your Site Name |
| 160 | +``` |
| 161 | + |
| 162 | +### Layout Integration |
| 163 | + |
| 164 | +The analytics integration is automatically initialized in the root layout: |
| 165 | + |
| 166 | +```tsx |
| 167 | +// src/app/layout.tsx |
| 168 | +import AnalyticsProvider from '../components/AnalyticsProvider' |
| 169 | + |
| 170 | +export default function RootLayout({ children }) { |
| 171 | + return ( |
| 172 | + <html> |
| 173 | + <body> |
| 174 | + <AnalyticsProvider |
| 175 | + googleAdsId={process.env.NEXT_PUBLIC_GOOGLE_ADS_ID} |
| 176 | + googleAnalyticsId={process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS_ID} |
| 177 | + conversionId={process.env.NEXT_PUBLIC_GOOGLE_ADS_CONVERSION_ID} |
| 178 | + conversionLabel={process.env.NEXT_PUBLIC_GOOGLE_ADS_CONVERSION_LABEL} |
| 179 | + enableRemarketing={true} |
| 180 | + enableConversionTracking={true} |
| 181 | + enableAnalytics={true} |
| 182 | + /> |
| 183 | + {children} |
| 184 | + </body> |
| 185 | + </html> |
| 186 | + ) |
| 187 | +} |
| 188 | +``` |
| 189 | + |
| 190 | +## Cookie Consent |
| 191 | + |
| 192 | +### GDPR Compliance |
| 193 | + |
| 194 | +The integration includes full GDPR compliance with: |
| 195 | + |
| 196 | +- Cookie consent banner |
| 197 | +- User preference management |
| 198 | +- Local storage integration |
| 199 | +- Consent validation before tracking |
| 200 | + |
| 201 | +### Consent States |
| 202 | + |
| 203 | +- **Not Given**: No tracking occurs |
| 204 | +- **Accepted**: Full tracking enabled |
| 205 | +- **Declined**: No tracking occurs |
| 206 | + |
| 207 | +### Managing Consent |
| 208 | + |
| 209 | +```tsx |
| 210 | +import { hasCookieConsent, clearCookieConsent } from '../components/CookieConsent' |
| 211 | + |
| 212 | +// Check if consent is given |
| 213 | +const consentGiven = hasCookieConsent() |
| 214 | + |
| 215 | +// Clear consent (for testing) |
| 216 | +clearCookieConsent() |
| 217 | +``` |
| 218 | + |
| 219 | +## Development |
| 220 | + |
| 221 | +### Debug Mode |
| 222 | + |
| 223 | +Analytics debug mode is enabled in development: |
| 224 | + |
| 225 | +```env |
| 226 | +NODE_ENV=development |
| 227 | +``` |
| 228 | + |
| 229 | +Debug messages will appear in the browser console. |
| 230 | + |
| 231 | +### Testing |
| 232 | + |
| 233 | +1. **Local Testing**: Use development environment variables |
| 234 | +2. **Console Logs**: Check browser console for debug messages |
| 235 | +3. **Network Tab**: Verify Google Analytics/Ads requests |
| 236 | +4. **Cookie Consent**: Test with and without consent |
| 237 | + |
| 238 | +### Example Component |
| 239 | + |
| 240 | +See `src/components/AnalyticsExample.tsx` for a complete working example of all tracking functions. |
| 241 | + |
| 242 | +## Best Practices |
| 243 | + |
| 244 | +### Performance |
| 245 | + |
| 246 | +- Use memoized tracking functions |
| 247 | +- Lazy load analytics scripts |
| 248 | +- Respect user consent preferences |
| 249 | + |
| 250 | +### Privacy |
| 251 | + |
| 252 | +- Always check cookie consent before tracking |
| 253 | +- Provide clear consent options |
| 254 | +- Allow users to manage preferences |
| 255 | + |
| 256 | +### Error Handling |
| 257 | + |
| 258 | +- Gracefully handle missing environment variables |
| 259 | +- Validate tracking parameters |
| 260 | +- Provide fallbacks for failed tracking |
| 261 | + |
| 262 | +### Code Organization |
| 263 | + |
| 264 | +- Use specialized hooks for specific tracking needs |
| 265 | +- Keep tracking logic separate from business logic |
| 266 | +- Document tracking events clearly |
| 267 | + |
| 268 | +## Troubleshooting |
| 269 | + |
| 270 | +### Common Issues |
| 271 | + |
| 272 | +1. **Analytics Not Working** |
| 273 | + - Check environment variables |
| 274 | + - Verify cookie consent |
| 275 | + - Check browser console for errors |
| 276 | + |
| 277 | +2. **Scripts Not Loading** |
| 278 | + - Verify network connectivity |
| 279 | + - Check ad blockers |
| 280 | + - Validate script URLs |
| 281 | + |
| 282 | +3. **Events Not Tracking** |
| 283 | + - Check consent status |
| 284 | + - Verify event parameters |
| 285 | + - Check Google Analytics/Ads dashboards |
| 286 | + |
| 287 | +### Debug Steps |
| 288 | + |
| 289 | +1. Enable debug mode |
| 290 | +2. Check browser console |
| 291 | +3. Verify network requests |
| 292 | +4. Test with different consent states |
| 293 | +5. Check Google Analytics/Ads real-time reports |
| 294 | + |
| 295 | +## Security Considerations |
| 296 | + |
| 297 | +- Never expose sensitive data in tracking events |
| 298 | +- Validate all user inputs |
| 299 | +- Use HTTPS in production |
| 300 | +- Regularly update tracking scripts |
| 301 | +- Monitor for suspicious activity |
| 302 | + |
| 303 | +## Maintenance |
| 304 | + |
| 305 | +### Regular Tasks |
| 306 | + |
| 307 | +- Update tracking scripts |
| 308 | +- Review consent implementation |
| 309 | +- Monitor analytics data |
| 310 | +- Update documentation |
| 311 | +- Test integration regularly |
| 312 | + |
| 313 | +### Version Updates |
| 314 | + |
| 315 | +- Test with new versions of Google Analytics/Ads |
| 316 | +- Update TypeScript types |
| 317 | +- Review breaking changes |
| 318 | +- Update documentation |
| 319 | + |
| 320 | +## Support |
| 321 | + |
| 322 | +For issues or questions regarding the analytics integration: |
| 323 | + |
| 324 | +1. Check this documentation |
| 325 | +2. Review the example component |
| 326 | +3. Check browser console for errors |
| 327 | +4. Verify environment configuration |
| 328 | +5. Test with different consent states |
| 329 | + |
| 330 | +## Changelog |
| 331 | + |
| 332 | +- **2024-12-19**: Initial analytics integration implementation |
| 333 | +- Added Google Ads and Analytics support |
| 334 | +- Implemented GDPR-compliant cookie consent |
| 335 | +- Created comprehensive tracking hooks |
| 336 | +- Added example component and documentation |
0 commit comments