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.
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:
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:
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#
Returns#
track
(data: TrackAnalyticsEventDTO) => Promise<void>identify
(data: IdentifyAnalyticsEventDTO) => Promise<void>shutdown
() => Promise<void>Optionalidentify#
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#
Parameters#
The details of the actor or group.
Returns#
Promise
Promise<void>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#
Parameters#
The event's details.
Returns#
Promise
Promise<void>