PostHog Analytics Module Provider
The PostHog Analytics Module Provider allows you to integrate PostHog with Medusa.
PostHog is an open-source product analytics platform that helps you track user interactions and analyze user behavior in your commerce application.
By integrating PostHog with Medusa, you can track events such as cart additions, order completions, and user sign-ups, enabling you to gain insights into user behavior and optimize your application accordingly.
Register the PostHog Analytics Module#
Add the module into the provider
object of the Analytics Module:
1module.exports = defineConfig({2 // ...3 modules: [4 {5 resolve: "@medusajs/medusa/analytics",6 options: {7 provider: {8 resolve: "@medusajs/analytics-posthog",9 id: "posthog",10 options: {11 posthogEventsKey: process.env.POSTHOG_EVENTS_API_KEY,12 posthogHost: process.env.POSTHOG_HOST,13 },14 },15 },16 },17 ],18})
Environment Variables#
Make sure to add the following environment variables:
PostHog Analytics Module Options#
Option | Description | Default |
---|---|---|
| The PostHog API key for tracking events. This is required to authenticate your requests to the PostHog API. | - |
| The PostHog API host URL. |
|
Test out the Module#
To test the module out, you'll track in PostHog 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 PostHog 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 your PostHog dashboard for the tracked event.