Raw
1 import { Table, Prerequisites } from "docs-ui"
2
3 export const metadata = {
4 title: `Local Analytics Module Provider`,
5 }
6
7 # {metadata.title}
8
9 The Local Analytics Module Provider is a simple analytics provider for Medusa that logs analytics events to the console. It's useful for development and debugging purposes.
10
11 <Note>
12
13 The Analytics Module and its providers are available starting [Medusa v2.8.3](https://github.com/medusajs/medusa/releases/tag/v2.8.3).
14
15 </Note>
16
17 ---
18
19 ## Register the Local Analytics Module
20
21 Add the module into the `provider` object of the Analytics Module:
22
23 <Note>
24
25 You can use only one Analytics Module Provider in your Medusa application.
26
27 </Note>
28
29 ```ts title="medusa-config.ts"
30 module.exports = defineConfig({
31 // ...
32 modules: [
33 {
34 resolve: "@medusajs/medusa/analytics",
35 options: {
36 providers: [
37 {
38 resolve: "@medusajs/analytics-local",
39 id: "local",
40 },
41 ],
42 },
43 },
44 ],
45 })
46 ```
47
48 ### Change Log Level
49
50 The Local Analytics Module Provider logs events to the console at the `debug` level, which is below the default `http` level.
51
52 So, to see the tracked events in your logs, you need to change the log level to `debug` or `silly`. You can do so by setting the `LOG_LEVEL` system environment variable:
53
54 ```bash
55 export LOG_LEVEL=debug
56 ```
57
58 <Note title="Important">
59
60 The environment variable must be set as a system environment variable and not in `.env`.
61
62 </Note>
63
64 ---
65
66 ## Test out the Module
67
68 To test the module out, you'll track in the console when an order is placed.
69
70 You'll first create a [workflow](!docs!/learn/fundamentals/workflows) that tracks the order completion event. Then, you can execute the workflow in a [subscriber](!docs!/learn/fundamentals/events-and-subscribers) that listens to the `order.placed` event.
71
72 For example, create a workflow at `src/workflows/track-order-placed.ts` with the following content:
73
74 export const workflowHighlights = [
75 ["14", "resolve", "Resolve the Analytics Module's service"],
76 ["16", "track", "Track the event in the installed Analytics Module Provider"]
77 ]
78
79 ```ts title="src/workflows/track-order-placed.ts" highlights={workflowHighlights}
80 import { createWorkflow } from "@medusajs/framework/workflows-sdk"
81 import { createStep } from "@medusajs/framework/workflows-sdk"
82 import { useQueryGraphStep } from "@medusajs/medusa/core-flows"
83 import { Modules } from "@medusajs/framework/utils"
84 import { OrderDTO } from "@medusajs/framework/types"
85
86 type StepInput = {
87 order: OrderDTO
88 }
89
90 const trackOrderPlacedStep = createStep(
91 "track-order-placed-step",
92 async ({ order }: StepInput, { container }) => {
93 const analyticsModuleService = container.resolve(Modules.ANALYTICS)
94
95 await analyticsModuleService.track({
96 event: "order_placed",
97 actor_id: order.customer_id,
98 properties: {
99 order_id: order.id,
100 total: order.total,
101 items: order.items?.map((item) => ({
102 variant_id: item.variant_id,
103 product_id: item.product_id,
104 quantity: item.quantity,
105 })),
106 customer_id: order.customer_id,
107 },
108 })
109 }
110 )
111
112 type WorkflowInput = {
113 order_id: string
114 }
115
116 export const trackOrderPlacedWorkflow = createWorkflow(
117 "track-order-placed-workflow",
118 ({ order_id }: WorkflowInput) => {
119 const { data: orders } = useQueryGraphStep({
120 entity: "order",
121 fields: [
122 "*",
123 "customer.*",
124 "items.*",
125 ],
126 filters: {
127 id: order_id,
128 },
129 })
130 trackOrderCreatedStep({
131 order: orders[0],
132 } as unknown as StepInput)
133 }
134 )
135 ```
136
137 This workflow retrieves the order details using the `useQueryGraphStep` and then tracks the order placement event using the `trackOrderPlacedStep`.
138
139 In the step, you resolve the service of the Analytics Module from the [Medusa container](!docs!/learn/fundamentals/medusa-container) and use its `track` method to track the event. This method will use the underlying provider configured (which is the Local Analytics Module Provider, in this case) to track the event.
140
141 Next, create a subscriber at `src/subscribers/order-placed.ts` with the following content:
142
143 ```ts title="src/subscribers/order-placed.ts"
144 import type {
145 SubscriberArgs,
146 SubscriberConfig,
147 } from "@medusajs/framework"
148 import { trackOrderPlacedWorkflow } from "../workflows/track-order-placed"
149
150 export default async function orderPlacedHandler({
151 event: { data },
152 container,
153 }: SubscriberArgs<{ id: string }>) {
154 await trackOrderPlacedWorkflow(container).run({
155 input: {
156 order_id: data.id,
157 },
158 })
159 }
160
161 export const config: SubscriberConfig = {
162 event: "order.placed",
163 }
164 ```
165
166 This subscriber listens to the `order.placed` event and executes the `trackOrderPlacedWorkflow` workflow, passing the order ID as input.
167
168 You'll now track the order placement event whenever an order is placed in your Medusa application. You can test this out by placing an order and checking the server's logs for the tracked event.
169
170 ---
171
172 ## Additional Resources
173
174 - [How to Use the Analytics Module](/references/analytics/service)