Skip to main content

@robojs/analytics

Instant analytics for your Robo. This plugin automatically integrates your preferred analytics service into your project, such as Google Analytics, Plausible, and more.

GitHub
license npm install
size Discord All
Contributors

Use cases:

  • Know how many times a Slash Command is used.
  • Track how many times your Discord Activity is opened.
  • Monitor number of users over time.
  • Anything else you can think of. ✨

📚 Documentation: Getting started

🚀 Community: Join our Discord server

Installation

To add this plugin to your Robo.js project:

Terminal
npx robo add @robojs/analytics

New to Robo.js? Start your project with this plugin pre-installed:

Terminal
npx create-robo <project-name> -p @robojs/analytics

Getting Started

How does @robojs/analytics know which service to use? Simple! Just add your analytics service key to your .env file.

For Google Analytics, it's a tracking ID and secret:

GOOGLE_ANALYTICS_MEASURE_ID="G-123456789"
GOOGLE_ANALYTICS_SECRET="123456789"

For Plausible, it's your domain:

PLAUSIBLE_DOMAIN="example.com"

Not sure which service to use?

We recommend Plausible for its simplicity and privacy-focused analytics. If you're looking for more tracking, Google Analytics may be a better fit. You can also use both or implement your own!

Seed

This plugin can seed your project with a basic tracking setup. This includes tracking Slash Commands and Events.

JavaScript API

An Analytics object is available globally in your project.

import { Analytics } from '@robojs/analytics'
import type { ChatInputCommandInteraction } from 'discord.js'

export default (interaction: ChatInputCommandInteraction) => {
// An event can be anything you want to track.
Analytics.event('testing_event')

// A view is a page view or screen view. (not really for discord bots)
Analytics.view('Test Page')

// Use a unique identifier per session if you can.
// This may be more difficult for discord bots, but you can use the user id, guild id, etc.
const sessionId = interaction.channelId ?? interaction.guildId
Analytics.event('something_happened', { sessionId })

// Also include a user id as well whenever possible for greater accuracy.
Analytics.event('test_event', {
sessionId: sessionId,
userId: interaction.user.id
})

return 'I just tracked something special!'
}

Feel free to use the Analytics object anywhere in your project! It's just JavaScript, after all!

Other Services

Want to use a different analytics service? You can add your own service by extending the BaseEngine class and passing it in your config.

// config/plugins/robojs/analytics.mjs
import { BaseEngine } from '@robojs/analytics'

class MyCustomEngine extends BaseEngine {
// Implement your custom tracking logic here
}

export default {
engine: new MyCustomEngine()
}

We also offer a ManyEngine class that allows you to use multiple analytics services at once.

// config/plugins/robojs/analytics.mjs
import { GoogleAnalytics, Plausible, ManyEngine } from '@robojs/analytics'

export default {
engine: new ManyEngine(new GoogleAnalytics(), new Plausible())
}

Mix and match services to your heart's content, or switch between them without needing to update code. Usage remains the same.

More on GitHub

Robo.js Logo

MIT © 2024 Robo.js By WavePlay