How to Use Analytics Module

In this document, you’ll learn about the different methods in the Analytics Module's service and how to use them.

Note: The Analytics Module is available starting Medusa v2.8.3.

Configure Analytics Module Provider#

To use the Analytics Module, you need to configure it along with an Analytics Module Provider.

Medusa provides two Analytics Module Providers: Local and PostHog module providers.

To configure the Analytics Module and its provider, add it to the list of modules in your medusa-config.ts file. For example:

medusa-config.ts
1module.exports = defineConfig({2  // ...3  modules: [4    {5      resolve: "@medusajs/medusa/analytics",6      options: {7        providers: [8          {9            resolve: "@medusajs/medusa/analytics-local",10            id: "local",11          },12        ],13      },14    },15  ],16})

Refer to the documentation of each provider for specific configuration options.


Resolve Analytics Module's Service#

In your workflow's step, you can resolve the Analytics Module's service from the Medusa container:

Code
1import { Modules } from "@medusajs/framework/utils"2import { createStep } from "@medusajs/framework/workflows-sdk"3
4const step1 = createStep(5  "step-1",6  async ({}, { container }) => {7    const analyticsModuleService = container.resolve(8      Modules.Analytics9    )10    11    // TODO use analyticsModuleService12  } 13)

You can then use the Analytics Module's service's methods in the step, which would use the underlying provider's logic. The rest of this guide details these methods.


getProvider#

This method returns the service of the configured Analytics Module Provider in medusa-config.ts. This is useful if you want to execute custom methods defined in the provider's service or you need direct access to it.

Example#

Code
1const postHogProviderService = analyticsModuleService.getProvider()2// TODO: perform custom actions with the provider

Returns#

track(data: TrackAnalyticsEventDTO) => Promise<void>
identify(data: IdentifyAnalyticsEventDTO) => Promise<void>
shutdown() => Promise<void>Optional

identify#

This method identifies an actor or group in the analytics provider. The Analytics Module will use the identify method of the underlying provider configured in medusa-config.ts to identify the actor or group.

Example#

Code
1await analyticsModuleService.identify({2  actor_id: "123",3  properties: {4    name: "John Doe"5  }6})

Parameters#

The details of the actor or group.

Returns#

PromisePromise<void>
Resolves when the actor or group is identified successfully.

track#

This method tracks an event in the analytics provider. The Analytics Module will use the track method of the underlying provider configured in medusa-config.ts to track the event.

Example#

Code
1await analyticsModuleService.track({2  event: "order_placed",3  properties: {4    order_id: "order_123",5    customer_id: "customer_456",6    total: 100,7  }8})

Parameters#

The event's details.

Returns#

PromisePromise<void>
Resolves when the event is tracked successfully.
Was this page helpful?
Ask Anything
FAQ
What is Medusa?
How can I create a module?
How can I create a data model?
How do I create a workflow?
How can I extend a data model in the Product Module?
Recipes
How do I build a marketplace with Medusa?
How do I build digital products with Medusa?
How do I build subscription-based purchases with Medusa?
What other recipes are available in the Medusa documentation?
Chat is cleared on refresh
Line break