PostHog Analytics
PostHog is an open-source product analytics platform that helps you understand user behavior. CommandKit provides a simple way to integrate PostHog into your Discord bot.
Setup
- First, install the analytics package:
npm install @commandkit/analytics
- Configure PostHog in your CommandKit config:
import { posthog } from '@commandkit/analytics/posthog';
export default defineConfig({
plugins: [
posthog({
posthogOptions: {
apiKey: 'YOUR_POSTHOG_API_KEY',
},
}),
],
});
Usage
Once configured, you can track anonymous events using the track
function:
import { track } from 'commandkit/analytics';
// Track a custom event with anonymous data
await track({
name: 'command_executed',
data: {
commandName: 'ban',
// Track performance metrics
executionTime: 150, // ms
// Track anonymous usage patterns
timeOfDay: new Date().getHours(),
dayOfWeek: new Date().getDay(),
},
});
Identifying Anonymous Sessions
You can identify anonymous sessions in PostHog using the identify
function:
import { useAnalytics } from 'commandkit/analytics';
const analytics = useAnalytics();
await analytics.identify({
// Use a hashed or anonymous identifier
distinctId: 'session_' + Math.random().toString(36).substring(7),
properties: {
// Track anonymous session properties
sessionStart: Date.now(),
environment: process.env.NODE_ENV,
version: '1.0.0',
},
});
Automatic Events
CommandKit automatically tracks the following anonymous events in PostHog:
command_execution
: Command execution eventscache_hit
: Cache hit eventscache_miss
: Cache miss eventscache_revalidated
: Cache revalidation eventsfeature_flag_metrics
: Feature flag metricsfeature_flag_decision
: Feature flag decision events
Best Practices
- Use consistent event names across your bot
- Only track anonymous, aggregated data
- Avoid storing personal information
- Use hashed identifiers when necessary
- Be transparent about what data you collect
- Respect user privacy and Discord's Terms of Service
Next Steps
- Learn about Umami analytics
- Learn how to create custom providers