npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@variablevic/google-analytics-medusa

v0.1.1

Published

A Medusa plugin for sending ecommerce spec events to Google Analytics using the Measurement Protocol.

Readme

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:

  • add_to_cart - When items are added to a cart
  • remove_from_cart - When items are removed from a cart
  • add_shipping_info - When shipping information is added to a cart
  • add_payment_info - When payment information is added
  • purchase - When an order is placed

Prerequisites

Installation

yarn add @variablevic/google-analytics-medusa

Configuration

Add the plugin to your medusa-config.ts:

import { defineConfig } from "@medusajs/utils";

// ... other imports and environment variables

export default defineConfig({
  // ... other configurations
  plugins: [
    // ... other plugins
    {
      resolve: "@variablevic/google-analytics-medusa",
      options: {
        measurementId: "G-XXXXXXXX", // Your GA4 Measurement ID
        apiSecret: "XXXXXXXXXX", // Your GA4 API Secret
        debug: 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:

  • view_item - Product views
  • begin_checkout - Checkout initiation
  • sign_up - User registration
  • 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:

  1. Get the GA client ID from the cookie:
export 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;
};
  1. Set the client ID as cart metadata during cart creation:
const 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

  1. Clone this repository
  2. Install dependencies: yarn install
  3. Build the project: yarn build
  4. Test the plugin: yarn test

For local development and testing:

npx medusa plugin:develop

Contributing

Contributions are welcome! Please read our contributing guidelines before submitting a pull request.

License

MIT