Google Analytics
Track ecommerce events
Medusa GA4 Plugin
A Google Analytics 4 plugin for Medusa that automatically tracks ecommerce events on your backend using Measurement Protocol. This plugin implements server-side tracking for key ecommerce events in your Medusa store.
Features
The plugin automatically tracks the following GA4 ecommerce events:
- Copy to clipboard
add_to_cart
- When items are added to a cart - Copy to clipboard
remove_from_cart
- When items are removed from a cart - Copy to clipboard
add_shipping_info
- When shipping information is added to a cart - Copy to clipboard
add_payment_info
- When payment information is added - Copy to clipboard
purchase
- When an order is placed
Prerequisites
- Medusa backend
- Google Analytics 4 property
- GA4 Measurement ID
- GA4 API Secret
Installation
1yarn add @variablevic/google-analytics-medusa
Configuration
Add the plugin to your Copy to clipboardmedusa-config.ts
:
123456789101112131415161718import { defineConfig } from "@medusajs/utils";// ... other imports and environment variablesexport default defineConfig({// ... other configurationsplugins: [// ... other plugins{resolve: "@variablevic/google-analytics-medusa",options: {measurementId: "G-XXXXXXXX", // Your GA4 Measurement IDapiSecret: "XXXXXXXXXX", // Your GA4 API Secretdebug: false, // Optional, enables debug mode - no events will be sent to your property when debug is active!},},],});
Client-Side Setup
This plugin handles server-side events, but some GA4 ecommerce events need to be implemented on the client side due to their nature:
- Copy to clipboard
view_item
- Product views - Copy to clipboard
begin_checkout
- Checkout initiation - Copy to clipboard
sign_up
- User registration - Copy to clipboard
login
- User login
Additionally, to properly associate events with users, you need to set the GA client ID as metadata when creating a cart. Here's how to do it in the Next.js Starter:
- Get the GA client ID from the cookie:
1234567891011export const getGaClientId = async (): Promise<string | null> => {const cookies = await nextCookies();const gaClientIdCookie = cookies.get("_ga")?.value;const gaClientId = (gaClientIdCookie as string).split(".").slice(-2).join(".");return gaClientId;};
- Set the client ID as cart metadata during cart creation:
12345678910111213const gaClientId = await getGaClientId();const body = {region_id: region.id,} as Record<string, any>;if (gaClientId) {body.metadata = {ga_client_id: gaClientId,};}const cartResp = await sdk.store.cart.create(body, {}, headers);
Events Data Format
The plugin automatically formats cart and order data according to GA4's ecommerce event specifications. Each event includes:
- Currency code
- Transaction value
- Item-level details (name, variant, quantity, price)
- Customer information when available
Development
- Clone this repository
- Install dependencies: Copy to clipboard
yarn install
- Build the project: Copy to clipboard
yarn build
- Test the plugin: Copy to clipboard
yarn test
For local development and testing:
1npx medusa plugin:develop
Contributing
Contributions are welcome! Please read our contributing guidelines before submitting a pull request.
License
MIT