Вышел Модуль интеграций 2.0 для MedusaЧитать анонс
G

Google Analytics

Google Analytics 4 для Medusa 2. Покупки и возвраты уходят в GA4 с сервера через Measurement Protocol. Для событий электронной торговли на витрине есть вспомогательные функции.

npm install @topo-js/medusa-plugin-google-analytics
Категория
Аналитика
Создано
Topo-js
Версия
1.0.0
Ежемесячные загрузки
Загрузка данных
Последнее обновление

@topo-js/medusa-plugin-google-analytics

Google Analytics 4 for Medusa 2.23+, server-side. Orders reach GA4 through the Measurement Protocol from the backend, so ad blockers and closed tabs on the thank-you page never lose a sale.

Medusa eventGA4 event
(items, tax, shipping, coupons)
of the whole transaction

is the Medusa order id, so GA4 drops duplicates if a storefront tag sends the same purchase.

For the browsing events (, , ...) the plugin ships a storefront helper that maps Medusa products and carts to the same GA4 items.

Install

No migrations: the plugin has no tables.

Options

OptionDefaultNotes
GA4 > Admin > Data streams > your web stream > Measurement ID ()
Same stream > Measurement Protocol API secrets > Create
Sends to the validation endpoint and logs Google's messages. Nothing is recorded in reports
Sends the Medusa customer id as (cross-device reporting; cover it in your privacy policy)
collects in the EU

Without or the plugin logs a warning at startup and sends nothing, so development and test environments need no keys.

Attribute orders to the shopper's visit

GA4 ties a server-side event to a visit through the browser's GA client id (the cookie) and session id (the cookie). The storefront saves them on the cart before completing it; Medusa copies cart metadata to the order:

Cart metadata keyValue
= →
= →

Next.js storefront example, in the server action that places the order, with the storefront helper:

Without these keys GA4 still records the revenue, under a client id built from the order id, but with no source or campaign.

Storefront helper

builds GA4 ecommerce events from the store API's objects (, , , ) and sends them. It imports no backend code, so it is safe in a browser bundle, and it maps line items with the same function as the backend, so an item looks the same in and in the server's .

BuilderGA4 eventWhere
product page, and on variant change
category, collection, search, related
after the add succeeds
line deleted (whole quantity) or lowered (pass the difference)
cart page
checkout opens
shipping method saved (tier defaults to its name)
payment method chosen

sends with when the page loads gtag.js, and otherwise pushes to the Google Tag Manager . It does nothing on the server. Don't send from the browser: the backend does.

must be in the product query (the starter's field) for prices; a product without a variant uses its first variant.

Load the tag itself as usual, for example with () or Google Tag Manager.

Workflows

Import from :

WorkflowInputSends
a Measurement Protocol payloadany events, for your own workflows

The builders (, , ) are exported too.

Send your own events

Or resolve the module service anywhere: .

Development

License

MIT