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:
Change Log Level#
The Local Analytics Module Provider logs events to the console at the debug
level, which is below the default http
level.
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:
.env
.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 { useQueryGraphStep } from "@medusajs/medusa/core-flows"4import { Modules } from "@medusajs/framework/utils"5import { OrderDTO } from "@medusajs/framework/types"6 7type StepInput = {8 order: OrderDTO9}10 11const trackOrderPlacedStep = createStep(12 "track-order-placed-step",13 async ({ order }: StepInput, { container }) => {14 const analyticsModuleService = container.resolve(Modules.ANALYTICS)15 16 await analyticsModuleService.track({17 event: "order_placed",18 actor_id: order.customer_id,19 properties: {20 order_id: order.id,21 total: order.total,22 items: order.items?.map((item) => ({23 variant_id: item.variant_id,24 product_id: item.product_id,25 quantity: item.quantity,26 })),27 customer_id: order.customer_id,28 },29 })30 }31)32 33type WorkflowInput = {34 order_id: string35}36 37export const trackOrderPlacedWorkflow = createWorkflow(38 "track-order-placed-workflow",39 ({ order_id }: WorkflowInput) => {40 const { data: orders } = useQueryGraphStep({41 entity: "order",42 fields: [43 "*",44 "customer.*",45 "items.*",46 ],47 filters: {48 id: order_id,49 },50 })51 trackOrderCreatedStep({52 order: orders[0],53 } as unknown as StepInput)54 }55)
This workflow retrieves the order details using the useQueryGraphStep
and then tracks the order placement event using the trackOrderPlacedStep
.
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 { trackOrderPlacedWorkflow } from "../workflows/track-order-placed"6 7export default async function orderPlacedHandler({8 event: { data },9 container,10}: SubscriberArgs<{ id: string }>) {11 await trackOrderPlacedWorkflow(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 trackOrderPlacedWorkflow
workflow, passing the order ID as input.
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.