@medusajs/analytics
Documentation | Website
Building blocks for digital commerce
Analytics Module for Medusa: tracks events and identifies users by forwarding them to third-party analytics services such as PostHog. It uses a provider pattern, so the service you send data to is a pluggable Analytics Module Provider. It ships with @medusajs/medusa and is registered from @medusajs/medusa/analytics.
At a glance
| Package | @medusajs/analytics |
| Type | Infrastructure Module |
| Module key | Modules.ANALYTICS ("analytics") |
| Registered by default | No |
| Import from | @medusajs/medusa/analytics |
Features
- Track events (for example, order placed) and identify users from workflows, subscribers, or API routes.
- Pluggable providers. Official providers:
@medusajs/analytics-local: logs events with the Medusa logger, for development.@medusajs/analytics-posthog: sends events to PostHog.
Installation
No installation needed. The module ships with @medusajs/medusa, but you must register it in medusa-config.ts.
Usage
import { defineConfig } from "@medusajs/framework/utils"
module.exports = defineConfig({
// ...
modules: [
{
resolve: "@medusajs/medusa/analytics",
options: {
providers: [
{
resolve: "@medusajs/medusa/analytics-local",
id: "local",
},
],
},
},
],
})
Then resolve the service with container.resolve(Modules.ANALYTICS) and call its track and identify methods.
Configuration
| Option | Type | Description | Default |
|---|---|---|---|
providers |
{ resolve: string, id: string, options?: Record<string, unknown> }[] |
Analytics Module Providers to register. options is passed to the provider. |
[] |