@okutanihayato/analytics
v0.1.0
Published
Analytics SDK for Emulsia Platform - Track user behavior and measure KPIs
Maintainers
Readme
@emulsia/analytics
Analytics SDK for Emulsia Platform - Track user behavior and measure KPIs automatically.
Features
- 🚀 Zero-config tracking for Next.js + Firebase apps
- 📊 Automatic collection of 7 event types
- 🔒 Privacy-first design (no sensitive data collection)
- 🎯 Built for manufacturing industry DX
Installation
This package is part of the Emulsia Platform monorepo. To use it in the main application:
# From the root of emulsia-platform
cd packages/analytics-sdk
npm install
npm run buildThen in the main app's package.json, add:
{
"dependencies": {
"@emulsia/analytics": "file:./packages/analytics-sdk"
}
}Usage
1. Initialize in your Next.js app
// src/app/layout.tsx or appropriate component
'use client';
import { useEffect } from 'react';
import { useAuth } from '@/hooks/useAuth';
import { initAnalytics } from '@emulsia/analytics';
export default function AnalyticsProvider({ children }) {
const { user } = useAuth();
useEffect(() => {
if (user) {
initAnalytics({
projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID!,
userId: user.uid,
enabled: process.env.NEXT_PUBLIC_ANALYTICS_ENABLED === 'true',
});
}
}, [user]);
return children;
}2. Add environment variable
# .env.local
NEXT_PUBLIC_ANALYTICS_ENABLED=true3. Update Firestore rules
// firestore.rules
match /analytics_events/{eventId} {
allow create: if request.auth != null
&& request.resource.data.userId == request.auth.uid;
allow read: if false;
}Tracked Events
| Event Type | Auto-collected | Description | |------------|----------------|-------------| | screen_view | ✅ | Page navigation | | click | ✅ | All clicks | | scroll | ✅ | Scroll depth (10% intervals) | | time_spent | ✅ | Time on page | | focus | ✅ | Input focus duration | | keypress | ✅ | Keypress count (aggregated per 10s) | | error | ✅ | JavaScript errors |
Privacy
- No sensitive data: We don't collect keypress content, only counts
- User consent: Built-in consent management (auto-consent in Phase 1)
- Anonymization: Error messages are truncated to 500 chars
Requirements
- Next.js 14+
- Firebase 10+
- TypeScript 5+
License
MIT
Author
Hayato Okutani (Emulsia)
