Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 1m14s
Build & Deploy / 🧪 QA (push) Successful in 3m20s
Build & Deploy / 🧪 Smoke Test (push) Failing after 49s
Build & Deploy / ⚡ Lighthouse (push) Successful in 3m24s
Build & Deploy / 🏗️ Build (push) Successful in 3m2s
Build & Deploy / 🚀 Deploy (push) Successful in 26s
Build & Deploy / 🔔 Notify (push) Successful in 2s
7.9 KiB
7.9 KiB
Umami Analytics Implementation Summary
✅ Implementation Status: COMPLETE
Your project now has a modern, clean, and comprehensive Umami analytics implementation.
What Was Already Implemented
The project already had a solid foundation:
UmamiScript.tsx- Next.js Script component for loading analyticsAnalyticsProvider.tsx- Automatic pageview tracking on route changesUmamiAnalyticsService.ts- Service layer for event tracking- Environment variables in
docker-compose.yml
What Was Enhanced
1. Enhanced UmamiScript (components/analytics/UmamiScript.tsx)
- ✅ Added TypeScript props interface for customization
- ✅ Added JSDoc documentation with usage examples
- ✅ Added error handling for script loading failures
- ✅ Added development mode warnings
- ✅ Improved type safety and comments
2. Enhanced AnalyticsProvider (components/analytics/AnalyticsProvider.tsx)
- ✅ Added comprehensive JSDoc documentation
- ✅ Added development mode logging
- ✅ Improved code comments
3. New Custom Hook (components/analytics/useAnalytics.ts)
- ✅ Type-safe
useAnalyticshook for easy event tracking - ✅
trackEvent()method for custom events - ✅
trackPageview()method for manual pageview tracking - ✅
useCallbackoptimization for performance - ✅ Development mode logging
4. Event Definitions (components/analytics/analytics-events.ts)
- ✅ Centralized event constants for consistency
- ✅ Type-safe event names
- ✅ Helper functions for common event properties
- ✅ 30+ predefined events for various use cases
5. Comprehensive Documentation
- ✅ README.md - Full documentation with setup, usage, and best practices
- ✅ EXAMPLES.md - 20+ practical examples for different scenarios
- ✅ QUICK_REFERENCE.md - Quick start guide and troubleshooting
- ✅ SUMMARY.md - This file
File Structure
components/analytics/
├── UmamiScript.tsx # Script loader component
├── AnalyticsProvider.tsx # Route change tracker
├── useAnalytics.ts # Custom hook for event tracking
├── analytics-events.ts # Event definitions and helpers
├── README.md # Full documentation
├── EXAMPLES.md # Practical examples
├── QUICK_REFERENCE.md # Quick start guide
└── SUMMARY.md # This summary
Key Features
🚀 Modern Implementation
- Uses Next.js
Scriptcomponent (not old-school<script>tags) - TypeScript for type safety
- React hooks for clean API
- Environment variable configuration
📊 Comprehensive Tracking
- Automatic pageview tracking on route changes
- Custom event tracking with properties
- E-commerce events (products, cart, purchases)
- User authentication events
- Search and filter tracking
- Error and performance tracking
🎯 Developer Experience
- Type-safe event tracking
- Centralized event definitions
- Development mode logging
- Comprehensive documentation
- 20+ practical examples
🔒 Privacy & Performance
- No PII tracking by default
- Script loads after page is interactive
- Minimal performance impact
- Easy to disable in development
Environment Variables
The project is already configured in docker-compose.yml:
environment:
- UMAMI_WEBSITE_ID=${UMAMI_WEBSITE_ID}
- NEXT_PUBLIC_UMAMI_SCRIPT_URL=${NEXT_PUBLIC_UMAMI_SCRIPT_URL:-https://analytics.infra.mintel.me/script.js}
Required Setup
Add to your .env file:
UMAMI_WEBSITE_ID=59a7db94-0100-4c7e-98ef-99f45b17f9c3
Usage Examples
Basic Event Tracking
'use client';
import { useAnalytics } from '@/components/analytics/useAnalytics';
import { AnalyticsEvents } from '@/components/analytics/analytics-events';
function MyComponent() {
const { trackEvent } = useAnalytics();
const handleClick = () => {
trackEvent(AnalyticsEvents.BUTTON_CLICK, {
button_id: 'my-button',
page: 'homepage',
});
};
return <button onClick={handleClick}>Click Me</button>;
}
E-commerce Tracking
'use client';
import { useAnalytics } from '@/components/analytics/useAnalytics';
import { AnalyticsEvents } from '@/components/analytics/analytics-events';
function ProductCard({ product }) {
const { trackEvent } = useAnalytics();
const addToCart = () => {
trackEvent(AnalyticsEvents.PRODUCT_ADD_TO_CART, {
product_id: product.id,
product_name: product.name,
price: product.price,
});
};
return <button onClick={addToCart}>Add to Cart</button>;
}
Custom Pageview Tracking
'use client';
import { useAnalytics } from '@/components/analytics/useAnalytics';
function CustomNavigation() {
const { trackPageview } = useAnalytics();
const navigate = () => {
trackPageview('/custom-path');
// Navigate...
};
return <button onClick={navigate}>Go to Page</button>;
}
Testing & Development
Development Mode
In development, you'll see console logs:
[Umami] Tracked event: button_click { button_id: 'my-button' }
[Umami] Tracked pageview: /products/123
Disable Analytics (Development)
# .env.local
# UMAMI_WEBSITE_ID=
Troubleshooting
Analytics Not Working?
-
Check environment variables:
echo $UMAMI_WEBSITE_ID -
Verify script is loading:
- Open DevTools → Network tab
- Look for
script.jsrequest - Check Console for errors
-
Check Umami dashboard:
- Log into Umami
- Verify website ID matches
- Check if data is being received
Common Issues
| Issue | Solution |
|---|---|
| No data in Umami | Check website ID and script URL |
| Events not tracking | Verify useAnalytics hook is used |
| Script not loading | Check network connection, CORS |
| Wrong data | Verify event properties are correct |
Performance Tips
- Use
useCallback- The hook is already optimized - Debounce high-frequency events - See EXAMPLES.md
- Don't track every interaction - Focus on meaningful events
- Use environment variables - Disable in development
Privacy & Compliance
- ✅ Don't track PII (personally identifiable information)
- ✅ Don't track sensitive data (passwords, credit cards)
- ✅ Follow GDPR and other privacy regulations
- ✅ Use anonymized IDs where possible
- ✅ Provide cookie consent if required
Next Steps
- ✅ Setup complete - All files are in place
- ✅ Documentation complete - README, EXAMPLES, QUICK_REFERENCE
- ✅ Code enhanced - Better TypeScript, error handling, docs
- 📝 Add to .env - Set
UMAMI_WEBSITE_ID - 🧪 Test in development - Verify events are tracked
- 🚀 Deploy - Analytics will work in production
Quick Start Checklist
- Add
UMAMI_WEBSITE_IDto.envfile - Verify
UmamiScriptis inapp/[locale]/layout.tsx - Verify
AnalyticsProvideris inapp/[locale]/layout.tsx - Test in development mode (check console logs)
- Check Umami dashboard for data
- Review EXAMPLES.md for specific use cases
- Start tracking custom events with
useAnalytics
Summary
Your Umami analytics implementation is now production-ready with:
- ✅ Modern Next.js approach (Script component, not old-school tags)
- ✅ Type-safe API (TypeScript throughout)
- ✅ Comprehensive tracking (pageviews, events, e-commerce, errors)
- ✅ Excellent documentation (README, examples, quick reference)
- ✅ Developer-friendly (hooks, helpers, development mode)
- ✅ Performance optimized (async loading, minimal impact)
- ✅ Privacy conscious (no PII, easy to disable)
The implementation is clean, maintainable, and follows Next.js best practices. You can now track any user interaction or business event with just a few lines of code.