|
1
|
{"slug":"/references/analytics/service","title":"How to Use Analytics Module","frontmatter":{"slug":"/references/analytics/service","tags":["analytics","server","how to"],"sidebar_label":"Use Analytics Module"},"toc":[{"title":"Configure Analytics Module Provider","id":"configure-analytics-module-provider","level":2},{"title":"Resolve Analytics Module's Service","id":"resolve-analytics-module-s-service","level":2},{"title":"getProvider","id":"getprovider","level":2},{"title":"Example","id":"example","level":3},{"title":"Returns","id":"getprovider-returns","level":3},{"title":"identify","id":"identify","level":2},{"title":"Example","id":"example-1","level":3},{"title":"Parameters","id":"identify-parameters","level":3},{"title":"Returns","id":"identify-returns","level":3},{"title":"track","id":"track","level":2},{"title":"Example","id":"example-2","level":3},{"title":"Parameters","id":"track-parameters","level":3},{"title":"Returns","id":"track-returns","level":3}],"blocks":[{"kind":"markdown","html":"In this document, you’ll learn about the different methods in the Analytics Module's service and how to use them."},{"kind":"note","variant":"note","html":"The Analytics Module is available starting [Medusa v2.8.3](https://github.com/medusajs/medusa/releases/tag/v2.8.3)."},{"kind":"markdown","html":"---"},{"kind":"heading","level":2,"text":"Configure Analytics Module Provider","id":"configure-analytics-module-provider"},{"kind":"markdown","html":"To use the Analytics Module, you need to configure it along with an Analytics Module Provider.\n\nMedusa provides two Analytics Module Providers: [Local](/infrastructure-modules/analytics/local) and [PostHog](/infrastructure-modules/analytics/posthog) module providers.\n\nTo configure the Analytics Module and its provider, add it to the list of modules in your `medusa-config.ts` file. For example:\n\n```ts title=\"medusa-config.ts\"\nmodule.exports = defineConfig({\n // ...\n modules: [\n {\n resolve: \"@medusajs/medusa/analytics\",\n options: {\n providers: [\n {\n resolve: \"@medusajs/medusa/analytics-local\",\n id: \"local\",\n },\n ],\n },\n },\n ],\n})\n```\n\nRefer to the documentation of each provider for specific configuration options.\n\n\n---"},{"kind":"heading","level":2,"text":"Resolve Analytics Module's Service","id":"resolve-analytics-module-s-service"},{"kind":"markdown","html":"In your workflow's step, you can resolve the Analytics Module's service from the Medusa container:\n\n```ts\nimport { Modules } from \"@medusajs/framework/utils\"\nimport { createStep } from \"@medusajs/framework/workflows-sdk\"\n\nconst step1 = createStep(\n \"step-1\",\n async ({}, { container }) => {\n const analyticsModuleService = container.resolve(\n Modules.Analytics\n )\n \n // TODO use analyticsModuleService\n } \n)\n```\n\nYou 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.\n\n---"},{"kind":"heading","level":2,"text":"getProvider","id":"getprovider"},{"kind":"markdown","html":"This method returns the service of the configured Analytics Module Provider in `medusa-config.ts`. This is useful\nif you want to execute custom methods defined in the provider's service or you need direct access to it."},{"kind":"heading","level":3,"text":"Example","id":"example"},{"kind":"markdown","html":"```ts\nconst postHogProviderService = analyticsModuleService.getProvider()\n// TODO: perform custom actions with the provider\n```"},{"kind":"heading","level":3,"text":"Returns","id":"getprovider-returns"},{"kind":"typeList","sectionTitle":"getProvider","types":[{"name":"track","type":"(`data`: [TrackAnalyticsEventDTO](/references/types/interfaces/types.TrackAnalyticsEventDTO)) => Promise<void>","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"identify","type":"(`data`: [IdentifyAnalyticsEventDTO](/references/types/types/types.IdentifyAnalyticsEventDTO)) => Promise<void>","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"shutdown","type":"() => Promise<void>","description":"","optional":true,"defaultValue":"","expandable":false,"children":[]}]},{"kind":"heading","level":2,"text":"identify","id":"identify"},{"kind":"markdown","html":"This method identifies an actor or group in the analytics provider. The Analytics Module\nwill use the `identify` method of the underlying provider configured in `medusa-config.ts` to identify the actor or group."},{"kind":"heading","level":3,"text":"Example","id":"example-1"},{"kind":"markdown","html":"```ts\nawait analyticsModuleService.identify({\n actor_id: \"123\",\n properties: {\n name: \"John Doe\"\n }\n})\n```"},{"kind":"heading","level":3,"text":"Parameters","id":"identify-parameters"},{"kind":"typeList","sectionTitle":"identify","types":[{"name":"data","type":"[IdentifyAnalyticsEventDTO](/references/types/types/types.IdentifyAnalyticsEventDTO)","description":"The details of the actor or group.","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"actor_id","type":"`string`","description":"The actor of the event, if there is any.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"properties","type":"`Record<string, any>`","description":"The properties of the actor. The format and content is dynamic and depends on the integrated provider.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"group","type":"`object`","description":"The group that the event is for, such as an organization or team.","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"type","type":"`string`","description":"The name of the group.","optional":false,"defaultValue":"","example":"\"organization\"","expandable":false,"children":[]},{"name":"id","type":"`string`","description":"The group's ID.","optional":false,"defaultValue":"","expandable":false,"children":[]}]}]}]},{"kind":"heading","level":3,"text":"Returns","id":"identify-returns"},{"kind":"typeList","sectionTitle":"identify","types":[{"name":"Promise","type":"Promise<void>","optional":false,"defaultValue":"","description":"Resolves when the actor or group is identified successfully.","expandable":false,"children":[]}]},{"kind":"heading","level":2,"text":"track","id":"track"},{"kind":"markdown","html":"This method tracks an event in the analytics provider. The Analytics Module\nwill use the `track` method of the underlying provider configured in `medusa-config.ts` to track the event."},{"kind":"heading","level":3,"text":"Example","id":"example-2"},{"kind":"markdown","html":"```ts\nawait analyticsModuleService.track({\n event: \"order_placed\",\n properties: {\n order_id: \"order_123\",\n customer_id: \"customer_456\",\n total: 100,\n }\n})\n```"},{"kind":"heading","level":3,"text":"Parameters","id":"track-parameters"},{"kind":"typeList","sectionTitle":"track","types":[{"name":"data","type":"[TrackAnalyticsEventDTO](/references/types/interfaces/types.TrackAnalyticsEventDTO)","description":"The event's details.","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"event","type":"`string`","description":"The event name","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"actor_id","type":"`string`","description":"The actor of the event, if there is any","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"group","type":"`object`","description":"The group that the event is for, such as an organization or team.","optional":true,"defaultValue":"","expandable":false,"children":[{"name":"type","type":"`string`","description":"The name of the group.","optional":true,"defaultValue":"","example":"\"organization\"","expandable":false,"children":[]},{"name":"id","type":"`string`","description":"The group's ID.","optional":true,"defaultValue":"","expandable":false,"children":[]}]},{"name":"properties","type":"`Record<string, any>`","description":"The properties of the event. The format and content is dynamic and depends on the integrated provider.","optional":true,"defaultValue":"","expandable":false,"children":[]}]}]},{"kind":"heading","level":3,"text":"Returns","id":"track-returns"},{"kind":"typeList","sectionTitle":"track","types":[{"name":"Promise","type":"Promise<void>","optional":false,"defaultValue":"","description":"Resolves when the event is tracked successfully.","expandable":false,"children":[]}]}]} |