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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@commercengine/analytics

v0.3.0

Published

Zero-dependency mappers that transform canonical Commerce Engine entities into canonical e-commerce spec analytics events

Readme

@commercengine/analytics

Zero-dependency, fully-typed mappers that turn canonical Commerce Engine entities into events conforming to the Segment / RudderStack E-Commerce spec.

Every mapper is a pure function: canonical CE shape in → a Segment HTTP Tracking API envelope out ({ type: "track", event, properties, … }). Modify it, POST it unchanged to a CDP, or dispatch it through an analytics SDK.

With the browser SDKs (Segment analytics.js / RudderStack JS), identity is ambient. Initialize the mapping context once, then map and send without passing context at individual event call sites:

import { createAnalytics } from "@commercengine/analytics";

// Configure shared context once. All event mappers below inherit it.
const a = createAnalytics({ productUrl: ({ slug }) => `/products/${slug}` });

const { data: product } = await sdk.catalog.getProduct(slug);
analytics.track(...a.trackArgs(a.toProductViewed(product)));

// Mutations Commerce Engine also reports server-side return `events`:
// pass them through and the two copies deduplicate.
const { data, events } = await sdk.cart.addDeleteCartItem(
  { id: cartId },
  { product_id, variant_id, quantity: 1 },
);
const line = data.cart.cart_items.find(
  (i) => i.product_id === product_id && i.variant_id === variant_id,
)!;
analytics.track(...a.trackArgs(a.toProductAdded(line, { cart: data.cart, events })));

const { data: placed } = await sdk.order.getOrderDetails({ order_number });
analytics.track(...a.trackArgs(a.toOrderCompleted(placed.order)));

Currency is derived from each entity. Put mapping hooks (productUrl, brand), SSR cookie access, consent controls, and envelope identity in the initializer as needed. For every mutation that has a server-side counterpart, pass the response's events to the mapper; see Deduplicating against server-side events.

Sending events

The returned object is the Segment/RudderStack HTTP Tracking API payload, not the argument list of the browser SDKs. Dispatch it the way your target expects:

// 1. Browser SDK (Segment analytics.js / RudderStack JS): method takes
//    (name, properties, options). Identity is ambient (set via identify()); the
//    third argument carries automatically collected destination identifiers.
analytics.track(...a.trackArgs(a.toProductViewed(product)));

// 2. Server SDK (@segment/analytics-node, @rudderstack/rudder-sdk-node):
//    no ambient session — pass identity via ctx so it lands on the envelope.
const server = createAnalytics({ userId });
const event = server.toProductViewed(product);
analytics.track({ userId: event.userId, event: event.event, properties: event.properties });

// 3. Raw HTTP ingestion: POST the envelope unchanged.
await fetch("https://api.segment.io/v1/track", { method: "POST", body: JSON.stringify(event) });

analytics.track(event) (passing the whole envelope to a browser SDK) is wrong — it would be read as the event-name string. Use trackArgs or destructure event.event / event.properties / event.context.

Upgrading from 0.2.x

No event id is derived any more. Commerce Engine returns, in each storefront mutation response, a top-level events array with the id its own server-side copy uses; the SDK exposes it next to data. Pass it to the mapper:

// Before (0.2): id derived from storeId + cart/product/variant
analytics.track(...a.trackArgs(a.toProductAdded(line, { cart })));

// Now: id taken from the response
const { data, events } = await sdk.cart.addDeleteCartItem({ id: cartId }, body);
analytics.track(...a.trackArgs(a.toProductAdded(line, { cart: data.cart, events })));

Without events these mappers still build the event, just without an event_id — ad platforms then count the browser and server copies twice.

Upgrade in step with the server. Order Completed's id is now the order number itself, and Commerce Engine's server-side copy switches to the order number in the same release. A page still on 0.2.x keeps sending {store}.order_completed.{order}, so from that release on its Order Completed no longer deduplicates against the server's copy — the one event that still did. Upgrade to 0.3.0 when the server change ships.

  • toOrderCompleted's event_id is the order number exactly as the API returns it ("ORD-10023"), no longer {store}.order_completed.{order}.
  • storeId is removed from MapContext and createAnalytics; nothing needs it any more. Drop it from your initializer.
  • toProductAdded, toProductRemoved, toCheckoutStarted and toOrderCancelled no longer derive an id; they read it from events. Product Removed now gets one too.
  • toProductAdded / toProductRemoved report the matching entry's quantity — the units this call added or removed — and compute value from it, instead of the line's total quantity.
  • toOrderRefunded and OrderRefundedOptions are removed: refunds are reported server-side only. The OrderRefundedProperties type remains.
  • Payment Info Entered has no server counterpart, so it no longer gets an id unless you pass eventId.
  • The id derivation API is removed: deriveEventId, buildEventId, eventSlug, normalizePart, EVENT_ID_KEY_SPEC, EVENT_ID_OPTIONAL_KEYS and EVENT_ID_ABSENT_PART.
  • withEventId(properties, explicitId?, knownId?) replaces withEventId(eventName, properties, ctx, explicitId?).
  • Cart Created, Cart Deleted and Order Created are now typed entries of EcommerceEventProperties. A declaration merge that typed any of them differently must be removed.

New: toCartCreated, toCartDeleted, toCouponApplied, toCouponRemoved, toOrderCreated, toProductAddedToWishlist, toProductRemovedFromWishlist, findServerEvent, and toIdentify(data) / userTraits(data) straight from a verifyOtp or loginWithPassword result.

Upgrading from 0.1.2

The recommended API initializes shared context once:

const a = createAnalytics({ productUrl, brand });
analytics.track(...a.trackArgs(a.toOrderCompleted(order)));

The standalone mappers remain available for compatibility, but the initializer means consumers never repeat mapping hooks or cookie wiring at individual call sites.

trackArgs now returns three elements: [event, properties, options]. Existing spread calls therefore need no dispatch change. Code that destructures the tuple or forwards it through a fixed two-argument facade must accept and pass the third argument:

// Before
const [name, properties] = trackArgs(event);
analytics.track(name, properties);

// Now
const [name, properties, options] = trackArgs(event);
analytics.track(name, properties, options);

Other usage changes:

  • Event ids now come from the response's events, and Order Completed's from the order number — see Upgrading from 0.2.x.
  • Destination identifiers are collected automatically. Use enrich: false to disable collection, or per-platform toggles such as enrich: { tiktok: false }.
  • For SSR, pass a Cookie header/getter through cookies; browser usage needs no cookie wiring.
  • For Hosted Checkout or another envelope this package did not build, use ambientContext() as the browser SDK's third argument.
  • trackArgsWithContext is removed; use trackArgs.
  • readFbCookies and withFbCookies are replaced by readAmbientIdentifiers and ambientContext.
  • userTraits now normalizes phone numbers and omits a national number when no country code is available.

Deduplicating against server-side events

Commerce Engine sends the funnel events server-side as well. Meta and GA4 will count the browser event and its server counterpart as two conversions unless both carry the same event_id for the same event name.

For every storefront mutation the server reports, the response carries the id it used: a top-level events array the SDK returns next to data ([] on error, or when the call changed nothing worth reporting). Pass it as the mapper's events option and the mapper picks out its own entry (bound-client calls shown; the standalone mappers take the same options third):

| Event | SDK call | Mapper | | --- | --- | --- | | Cart Created | sdk.cart.createCart (see below) | toCartCreated(data.cart, { events }) | | Cart Deleted | sdk.cart.deleteCart / deleteUserCart | toCartDeleted(cart, { events })¹ | | Product Added / Product Removed | sdk.cart.addDeleteCartItem, and createCart (see below) | toProductAdded(line, { cart: data.cart, events })² | | Checkout Started | sdk.cart.updateCartAddress³ | toCheckoutStarted(data.cart, { events }) | | Coupon Applied / Coupon Removed | sdk.cart.applyCoupon / removeCoupon | toCouponApplied(data.cart, { events })⁴ | | Product Added to Wishlist / … Removed from Wishlist | sdk.cart.addToWishlist / removeFromWishlist | toProductAddedToWishlist(item, { events })⁵ | | Order Created | sdk.order.createOrder | toOrderCreated(data.order, { events }) | | Order Cancelled | sdk.order.cancelOrder | toOrderCancelled(data.order, { events }) | | Order Completed | — (payment callback) | toOrderCompleted(order)⁶ |

Send the browser copy only when its entry is in events. The server reports what a call actually changed, so events is also the record of whether there is anything to send: a createCart that reuses the shopper's active cart creates none, a cancel that leaves the status unchanged cancels nothing, removing a coupon when none is applied removes nothing, and a quantity change that nets to zero adds nothing. In each case there is no entry — and no server copy — so a browser copy would be a conversion that never happened. Check with findServerEvent before tracking:

import { findServerEvent } from "@commercengine/analytics";

const { data, events } = await sdk.cart.applyCoupon({ id: cartId }, { coupon_code });
if (findServerEvent(events, "Coupon Applied")) {
  analytics.track(...a.trackArgs(a.toCouponApplied(data.cart, { events })));
  // properties.event_id === the server copy's id, e.g. "01JQZM3…"
}

The shorter examples elsewhere in this README leave the check out.

createCart({ items }) reports more than one event: Cart Created only when it actually creates a cart, and a Product Added for each line it writes — also when it adds to the active cart it reused. Walk the entries and find each line in the returned cart by its product and variant:

const { data, events } = await sdk.cart.createCart({ items });

if (findServerEvent(events, "Cart Created")) {
  analytics.track(...a.trackArgs(a.toCartCreated(data.cart, { events })));
}
for (const entry of events) {
  if (entry.event !== "Product Added") continue;
  const line = data.cart.cart_items.find(
    (i) => i.product_id === entry.product_id && (i.variant_id ?? null) === entry.variant_id,
  );
  if (line) analytics.track(...a.trackArgs(a.toProductAdded(line, { cart: data.cart, events })));
}

¹ Deleting returns no cart: pass the cart as it was before the call.

² addDeleteCartItem sets a line's quantity, so one call either adds or removes units. The entry says which — a Product Added or a Product Removed entry — and its quantity is the difference, which the mapper reports (and computes value from) in place of the line's total. A line set to 0 is gone from the returned cart, so map the line as it was:

const isLine = (i: CartItem) => i.product_id === product_id && i.variant_id === variant_id;
const before = cart.cart_items.find(isLine);
const { data, events } = await sdk.cart.addDeleteCartItem({ id: cart.id }, { product_id, variant_id, quantity });
const after = data.cart.cart_items.find(isLine);

if (findServerEvent(events, "Product Added")) {
  analytics.track(...a.trackArgs(a.toProductAdded((after ?? before)!, { cart: data.cart, events })));
}
if (findServerEvent(events, "Product Removed")) {
  analytics.track(...a.trackArgs(a.toProductRemoved((before ?? after)!, { cart: data.cart, events })));
}

³ The server reports Checkout Started each time an address is set on the cart, so send the browser copy from the same place.

⁴ After a removal the cart no longer carries the code; toCouponRemoved reads it from the entry (or a couponCode option).

⁵ A wishlist request names a product and a variant (variant_id is null only for a product without variants), and the server's entry repeats both. Map a wishlist Item (from the response's products) or the catalog Product on screen: an Item is matched on its own product and variant, and a Product is matched on its product id and then described as the variant the entry names. Pass variant only to override that.

⁶ Order Completed fires from the payment callback, where no storefront response exists to carry an id. Its id is the order number, exactly as the API returns it — the same value the server's copy carries, and unique because an order completes once:

analytics.track(...a.trackArgs(a.toOrderCompleted(order)));
// properties.event_id === "ORD-10023"

Two events sit outside the handshake. Order Refunded is reported server-side only, so there is no browser mapper (the OrderRefundedProperties type describes the server's copy). Payment Info Entered is browser-only — payment details are submitted as part of order creation — so it carries no id.

When no id is known — no events passed, or no matching entry — the field is simply omitted and the event goes undeduplicated: nothing breaks, but Meta double-counts. It is never replaced by a placeholder, which every event hitting the same gap would share, collapsing them into a single conversion. No deduplication is strictly better than false deduplication.

eventId on the mapper options overrides both sources if you obtained the server's id some other way. findServerEvent(events, name, match?) exposes the lookup itself, for events you build with trackEvent.

Why properties, not the envelope? The envelope's messageId does not survive dispatch through a browser SDK — trackArgs omits identity and messageId deliberately. properties.event_id is the channel that survives on both the browser and the server.

Destination identifiers — collected automatically

Every ad platform matches a server-side conversion back to the click that caused it using an identifier its own tag wrote into a first-party cookie, or a click id it appended to the landing URL. Each reads a different key from a different place, and a value in the wrong place is silently ignored.

You wire none of it. Identifiers are collected inside every track mapper and delivered by trackArgs (identify envelopes do not need destination click identifiers):

analytics.track(...a.trackArgs(a.toOrderCompleted(order)));
// third argument carries { context: { fbc, fbp, gclid, ttclid, … } }

| Platform | Collected | | --- | --- | | Meta | fbc, fbp (_fbc / _fbp cookies) | | Google | gclid, gbraid, wbraid, ga_client_id (_gcl_aw / _ga) | | TikTok | ttclid, ttp (_ttp) | | Snapchat | sccid, scid (_scid) | | Reddit | rdt_cid, rdt_uuid (_rdt_uuid) | | Microsoft | msclkid | | LinkedIn | li_fat_id | | X / Twitter | twclid | | Pinterest | epik (_epik) |

Anything absent is simply not collected — no placeholders, no empty strings. Adding a platform is a package change, not a change in your app.

Verification status. Meta's context.fbc / context.fbp are confirmed against RudderStack's Facebook Conversions destination. The rest are collected under each platform's conventional key name; confirm against your destination's field mapping before relying on them. An unread field is harmless, but it is not attribution.

Consent is yours to enforce. Collection is unconditional: click ids are read from the landing URL even when a platform's own tag never ran, so they are available before any consent dialog resolves. If you operate under GDPR, CCPA or similar, gate it with enrich: false until consent is granted — this package has no notion of consent state and will not infer one.

Google cookie names are the defaults. Only _ga and _gcl_aw are read. A property configured with a custom cookie prefix, or a _ga_<CONTAINER> session cookie, is not collected.

Opting out

createAnalytics({ enrich: false });
createAnalytics({ enrich: { tiktok: false } });
createAnalytics({ context: { fbp: "…" } });

For a one-event override, derive a temporary bound client without changing the defaults:

const withoutEnrichment = a.withContext({ enrich: false });
analytics.track(...withoutEnrichment.trackArgs(
  withoutEnrichment.toOrderCompleted(order),
));

Events this package did not build

Commerce Engine Hosted Checkout runs in a cross-origin iframe, so it cannot read the parent's first-party cookies — and shouldn't. The parent attaches them when forwarding:

import { ambientContext } from "@commercengine/analytics";

export function forwardCheckoutAnalytics(event) {
  if (event.type !== "track") return;
  analytics.track(event.event, event.properties, ambientContext());
}

Server-side dispatch

Collection reads document.cookie and the current URL, so it yields nothing during SSR — safely, never throwing. To enrich from a request instead, pass a Cookie header or a getter (a Commerce Engine CookieAdapter's get fits):

{ cookies: request.headers.get("cookie") ?? undefined }
{ cookies: (name) => adapter.get(name) }

Design

  • Zero-dependency — no network, no auth, no storage; nothing is ever written.
  • Pure transforms, with one deliberate exception — mappers are pure functions of their inputs, except that they read ambient destination identifiers (cookies, URL) so no application has to wire each platform by hand. Disable it with enrich: false, or make it pure again by passing cookies explicitly.
  • Tree-shakeable — import only the mappers you use.
  • Canonical types by construction — input types are the generated CE types, imported type-only from @commercengine/storefront-sdk. The imports are erased at build (no runtime dependency — the bundle has zero imports), but the public types reference them, so @commercengine/storefront-sdk is a required peer dependency for TypeScript consumers. It's satisfied automatically if you use @commercengine/storefront (which depends on, and re-exports, the SDK); install @commercengine/storefront-sdk directly only if you use this package without the umbrella.
  • Spec types are the public contract — the Segment shapes in spec/segment-ecommerce.ts are hand-authored because they describe an external spec, not the CE API.

Mapping context

createAnalytics accepts persistent AnalyticsDefaults; standalone mappers continue to accept the wider MapContext for backward compatibility.

Mapping hooks — shape the event properties; useful in every setup:

| Field | Purpose | | ------------ | ---------------------------------------------------------- | | productUrl | (ref) => string to build a product page URL (url) | | brand | (ref) => string to resolve a brand (brand) | | currency | Last-resort currency fallback (normally derived — see below) |

Deduplication needs no context: event ids come from the response's events, and Order Completed's is the order number. See Deduplicating against server-side events.

Persistent envelope identity — userId and anonymousId. These matter only when you send envelopes yourself (server SDK object form or raw HTTP). Browser SDK identity is ambient, and trackArgs omits it regardless.

Per-event metadata — timestamp and messageId are intentionally rejected by createAnalytics and withContext. Reusing a messageId would make a CDP treat distinct events as duplicates. Attach either value to one completed envelope instead:

const event = a.withEventMetadata(
  a.toOrderCompleted(order),
  { messageId: crypto.randomUUID(), timestamp: new Date().toISOString() },
);

context is different: it is where destinations read their identifiers, so it is delivered by trackArgs as the third argument, and it is populated automatically — see Destination identifiers. withContext recursively merges ordinary nested context objects; explicit values win, while undefined leaves the initializer value unchanged.

messageId does not survive trackArgs — it is deliberately dropped along with identity. Anything a destination must see from the browser travels in properties (as event_id does) or in context.

Currency is derived, not passed. Mappers read it from the entity — Product.pricing.currency, Cart.currency, Order.currency. The only exception is Product Added/Product Removed, which take a bare CartItem (no currency of its own): pass the parent cart via { cart } and both cart_id and currency are derived from it. ctx.currency exists solely as a last-resort fallback and is rarely needed.

Identifying users

toIdentify maps either canonical user shape — the full API User (e.g. verifyOtp(...).data.user or the profile endpoint) or the trimmed JWT UserInfo (e.g. sdk.getUserInfo()) — to a Segment identify envelope. It also takes a login or signup result's content as-is: anything with a user, such as verifyOtp's or loginWithPassword's { user, access_token, refresh_token }. userTraits is the reusable traits bag; identifyArgs adapts to the browser SDK analytics.identify(userId, traits) call.

const { data } = await sdk.auth.verifyOtp({ otp, otp_token, otp_action });

// Browser SDK — the auth result (or its `User`)…
analytics.identify(...a.identifyArgs(a.toIdentify(data)));
// …or the trimmed JWT `UserInfo` (its anonymousId is used automatically)
const info = await sdk.getUserInfo();
analytics.identify(...a.identifyArgs(a.toIdentify(info)));

// Server SDK / HTTP — the envelope is the identify payload
const event = a.toIdentify(data);
// { type: "identify", userId, traits: {…} }

Standard traits (email, firstName, lastName, name, phone, avatar, createdAt, company, …) are mapped from the User, plus CE-specific additive traits: customer_id, customer_group_id, customer_group, country_code, email_verified, phone_verified. Tokens are never traits.

email and phone are normalized. Destinations hash these verbatim to build match keys, so email is lowercased and trimmed, and phone is reduced to digits including country code (919897622832). CE stores the subscriber number and country code separately, so a value starting with + is treated as already international and anything else gets country_code prepended.

A phone with no resolvable country code is dropped, not guessed. A national number without its prefix hashes to something no ad platform will ever match, which dilutes match quality rather than merely failing. The server applies the identical rule, so both sides produce the same hash for the same user.

Anonymous vs identified. CE, Segment, and RudderStack all distinguish a known userId from a pseudonymous anonymousId. toIdentify decides purely on is_anonymous/isAnonymous — not login state, since a user can be logged out yet still known. When not anonymous (even if logged out) it sets userId (and also anonymousId, so Segment links prior anonymous activity); when anonymous it leaves userId unset and sets only anonymousId — the user's CE id is never used as userId. An explicit ctx.userId overrides.

Entity mappers

Convenience functions that derive an event directly from a Commerce Engine shape. Those marked † take the response's events for the server's event_id.

| Mapper | Event | Input | | -------------------------------- | ------------------------------- | ---------------------- | | toProductsSearched | Products Searched | string | | toProductViewed | Product Viewed | Product | | toProductClicked | Product Clicked | Product | | toProductListViewed | Product List Viewed | Product[] | | toProductListFiltered | Product List Filtered | Product[] | | toProductAdded † | Product Added | CartItem | | toProductRemoved † | Product Removed | CartItem | | toProductAddedToWishlist † | Product Added to Wishlist | Product / Item | | toProductRemovedFromWishlist † | Product Removed from Wishlist | Product / Item | | toCartViewed | Cart Viewed | Cart | | toCartCreated † | Cart Created | Cart | | toCartDeleted † | Cart Deleted | Cart | | toCheckoutStarted † | Checkout Started | Cart | | toCheckoutStepViewed | Checkout Step Viewed | Cart | | toCheckoutStepCompleted | Checkout Step Completed | Cart | | toPaymentInfoEntered | Payment Info Entered | Cart | | toCouponApplied † | Coupon Applied | Cart | | toCouponRemoved † | Coupon Removed | Cart | | toOrderCreated † | Order Created | Order | | toOrderCompleted | Order Completed | Order | | toOrderUpdated | Order Updated | Order | | toOrderCancelled † | Order Cancelled | Order / OrderDetail |

Cart Created, Cart Deleted and Order Created are not Segment spec events; they are the names Commerce Engine's server-side copies use, built from the standard cart and order bags. Coupon Applied / Coupon Removed carry the spec's cart_id, coupon_id (the CE coupon id, on Coupon Applied), coupon_name and discount, plus the CE coupon_code and currency.

Product sources & variant fields

The product mappers (toProductViewed, toProductClicked, toProductListViewed, toProductListFiltered, and the wishlist mappers) and productProperties accept any catalog shape — Product, ProductDetail, or Item — and normalize the field-name differences for you. Every emitted product carries the additive variant_id and variant_slug (beyond the spec's variant name) whenever available, plus the canonical CE identifiers that are useful for analytics joins: product_slug, variant_name, product_type, category ids/slugs, tags, stock/promotion/subscription flags, line-level price/tax/discount fields, and compact product attributes keyed by CE attribute key/name. Empty slug fields are omitted rather than emitted as empty strings.

Item, CartItem, and OrderItem already reference a single variant inline. A Product/ProductDetail describes the parent product (its variants live in variants[]), so pass the selected one to report variant info:

// Explicit variant (object or id) — 3rd arg; ctx (2nd) is empty here
toProductViewed(product, {}, { variant: selectedVariantId });
toProductClicked(product, {}, { variant: selectedVariant });

When no variant is passed, the product's default variant (is_default) is used if present — including in list events. A resolved variant overrides variant/variant_id/variant_slug and sku, price, and image_url.

The mappers intentionally do not dump every nested CE object into analytics payloads. Large operational objects like addresses, shipments, raw promotion objects, inventory lots, and seller details are left out by default; add them in your app only when a destination actually needs them.

trackEvent — the rest of the spec, fully typed

Events with no natural source entity (promotions, sharing, reviews, coupons entered or denied) are covered by a single builder. For spec events the properties argument is type-checked against the event name; for custom event names it accepts an open object:

import { trackEvent, trackArgs } from "@commercengine/analytics";

analytics.track(...trackArgs(trackEvent("Promotion Viewed", { promotion_id: "promo_1", name: "Sale" })));
analytics.track(...trackArgs(trackEvent("Coupon Denied", { cart_id, coupon_id, reason: "Expired" })));

An event built this way carries no event_id. For one Commerce Engine also sends server-side, prefer its mapper, or attach the id yourself with findServerEvent(events, name)?.event_id.

Reusable builders & custom events

The property bags are the reusable primitives — a.productProperties (product), a.cartProperties (cart), a.orderProperties (order). The bound versions inherit the initializer's currency, URL, and brand hooks. Compose them into events that aren't in the Segment spec but that you track internally (e.g. Cart Updated, Payment Successful):

cartProperties and orderProperties include the core CE monetary and status fields: totals/subtotals, tax, shipping, coupon/promotion discount and savings breakdowns, loyalty/credit amounts, item counts, and cart/order status. They exclude addresses, metadata, shipments, and raw applied-promotion/coupon objects by default.

checkoutStepProperties and paymentInfoEnteredProperties derive checkout-step linking fields from the canonical Cart: checkout_id is the cart id, and cart_id is emitted separately. shipping_method is derived from cart.fulfillment_preference (delivery provider/courier, collect-in-store pickup location, or both for partial fulfillment). Commerce Engine has no separate checkout id, so order mappers emit the order's cart_id as both cart_id and the checkout_id proxy.

analytics.track(...a.trackArgs(a.trackEvent("Cart Updated", a.cartProperties(cart))));
analytics.track(...a.trackArgs(a.trackEvent("Payment Successful", {
  ...a.orderProperties(order),
  payment_method: "Visa",
})));

To make a custom event strongly typed, augment the spec map via declaration merging:

import type { CartProperties, OrderProperties } from "@commercengine/analytics";

declare module "@commercengine/analytics" {
  interface EcommerceEventProperties {
    "Cart Updated": CartProperties;
    "Payment Successful": OrderProperties & { payment_method?: string };
  }
}

// Now `trackEvent("Payment Successful", …)` is type-checked.

This means every V2 spec event is representable — entity mappers for the derivable ones, trackEvent for the rest — and your own non-spec events reuse the exact same builders.