Local Analytics Module Provider
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.
Register the Local Analytics Module#
Add the module into the provider
object of the Analytics Module:
Test out the Module#
To test the module out, you'll track in the console when an order is placed.
You'll first create a workflow that tracks the order completion event. Then, you can execute the workflow in a subscriber that listens to the order.placed
event.
For example, create a workflow at src/workflows/track-order-placed.ts
with the following content:
1import { createWorkflow } from "@medusajs/framework/workflows-sdk"2import { createStep } from "@medusajs/framework/workflows-sdk"3import { Modules } from "@medusajs/framework/utils"4import { OrderDTO } from "@medusajs/framework/types"5 6type StepInput = {7 order: OrderDTO8}9 10const trackOrderCreatedStep = createStep(11 "track-order-created-step",12 async ({ order }: StepInput, { container }) => {13 const analyticsModuleService = container.resolve(Modules.ANALYTICS)14 15 await analyticsModuleService.track({16 event: "order_created",17 userId: order.customer_id,18 properties: {19 order_id: order.id,20 total: order.total,21 items: order.items.map((item) => ({22 variant_id: item.variant_id,23 product_id: item.product_id,24 quantity: item.quantity,25 })),26 customer_id: order.customer_id,27 },28 })29 }30)31 32type WorkflowInput = {33 order_id: string34}35 36export const trackOrderCreatedWorkflow = createWorkflow(37 "track-order-created-workflow",38 ({ order_id }: WorkflowInput) => {39 const { data: orders } = useQueryGraphStep({40 entity: "order",41 fields: [42 "*",43 "customer.*",44 "items.*"45 ],46 filters: {47 id: order_id,48 },49 })50 trackOrderCreatedStep({51 order: orders[0],52 })53 }54)
This workflow retrieves the order details using the useQueryGraphStep
and then tracks the order creation event using the trackOrderCreatedStep
.
In the step, you resolve the service of the Analytics Module from the 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.
Next, create a subscriber at src/subscribers/order-placed.ts
with the following content:
1import type {2 SubscriberArgs,3 SubscriberConfig,4} from "@medusajs/framework"5import { trackOrderCreatedWorkflow } from "../workflows/track-order-created"6 7export default async function orderPlacedHandler({8 event: { data },9 container,10}: SubscriberArgs<{ id: string }>) {11 await trackOrderCreatedWorkflow(container).run({12 input: {13 order_id: data.id,14 },15 })16}17 18export const config: SubscriberConfig = {19 event: "order.placed",20}
This subscriber listens to the order.placed
event and executes the trackOrderCreatedWorkflow
workflow, passing the order ID as input.
You'll now track the order creation event whenever an order is placed in your Medusa application. You can test this out by placing an order and checking the console for the tracked event.