@robojs/analytics
Track usage patterns in your Robo with Google Analytics, Plausible, or custom providers.
Track usage patterns in your Robo with services like Google Analytics, Plausible, or a custom provider. Know how often commands are used, how many servers your bot joins, and more.
The plugin provides a unified Analytics API that works the same regardless of which analytics service you use. Switch providers or use multiple at once without changing your tracking code.
Features
Google Analytics
Track events with GA4 Measurement Protocol
Plausible
Privacy-focused, GDPR-compliant analytics
Multi-Provider
Send events to multiple services at once
Event Tracking
Track custom events with session and user context
Page Views
Record screen and page view analytics
Auto-Detection
Detects providers from environment variables
Seed Files
Pre-built middleware and tracking commands
Custom Engines
Build your own analytics provider
Installation
Add the plugin to your Robo:
npx robo add @robojs/analytics@nextOr create a new project with the plugin pre-installed:
npx create-robo@next my-project -p @robojs/analytics@nextQuick start
The plugin detects which analytics service to use based on environment variables in your .env file.
Add your Measurement ID and API Secret:
GOOGLE_ANALYTICS_MEASURE_ID="G-123456789"
GOOGLE_ANALYTICS_SECRET="your-api-secret"Find these values in your Google Analytics Admin under Data Streams > Measurement Protocol API secrets.
Add your site domain:
PLAUSIBLE_DOMAIN="example.com"Plausible is a privacy-focused alternative that does not use cookies and is GDPR-compliant by default.
Start tracking events anywhere in your project:
import { Analytics } from '@robojs/analytics'
Analytics.event('button_click')
Analytics.view('Dashboard')import { Analytics } from '@robojs/analytics'
Analytics.event('button_click')
Analytics.view('Dashboard')That's it. The plugin handles initialization, service detection, and API calls automatically.
Auto-detection
The plugin detects which engine to use at startup:
| Environment variables set | Engine created |
|---|---|
Both GOOGLE_ANALYTICS_MEASURE_ID and PLAUSIBLE_DOMAIN | ManyEngines (sends to both) |
Only GOOGLE_ANALYTICS_MEASURE_ID | GoogleAnalytics |
Only PLAUSIBLE_DOMAIN | PlausibleAnalytics |
| Neither | Warning logged, analytics inactive |
Set both to send events to multiple services automatically. No config file needed.
Which service to choose?
| Service | Best for |
|---|---|
| Plausible | Privacy-focused, simple dashboards, GDPR compliance |
| Google Analytics | Detailed funnels, audience segmentation, free tier |
| Custom | Proprietary systems, niche providers |
Tracking events
Use Analytics.event() to track any action. Include sessionId and userId when possible for better accuracy.
import { Analytics } from '@robojs/analytics'
import type { ChatInputCommandInteraction } from 'discord.js'
export default (interaction: ChatInputCommandInteraction) => {
const sessionId = interaction.channelId ?? interaction.guildId
// Track with session and user context
Analytics.event('feature_used', {
sessionId,
userId: interaction.user.id,
data: { feature: 'leaderboard' }
})
return 'Tracked.'
}import { Analytics } from '@robojs/analytics'
export default (interaction) => {
const sessionId = interaction.channelId ?? interaction.guildId
// Track with session and user context
Analytics.event('feature_used', {
sessionId,
userId: interaction.user.id,
data: { feature: 'leaderboard' }
})
return 'Tracked.'
}Use Analytics.view() for page or screen views:
Analytics.view('Settings Page', {
sessionId: interaction.channelId,
userId: interaction.user.id
})Analytics.view('Settings Page', {
sessionId: interaction.channelId,
userId: interaction.user.id
})Check if the plugin has been initialized before calling:
if (Analytics.isReady()) {
Analytics.event('safe_event')
}if (Analytics.isReady()) {
Analytics.event('safe_event')
}Seed files
When you install the plugin with npx robo add, it can seed your project with starter tracking code:
| File | What it does |
|---|---|
track-commands.ts | Middleware that automatically tracks every slash command execution |
guildCreate.ts | Tracks server_join events when your bot joins a new server |
test-analytics.ts | Test command to verify your analytics integration works |
The track-commands middleware is the most useful seed file. It intercepts every slash command, normalizes the name (replacing / and - with _), and sends it as an event with session and user context. No per-command tracking code needed.
The guildCreate event requires the Guilds intent. Add it to your Robo config if you want server join tracking.
Delete test-analytics.ts after verifying your setup works.
