Skip to content

tracker.trackPurchase()

Fires a GA4 purchase event from the browser. When conversionLabels.purchase is configured on createBrowserTracker, also fires the matching Ads conversion in the same call, sharing the transactionId with the eventual server-side counterpart for dedup.

tracker.trackPurchase(input: BrowserPurchaseInput): Promise<void>;
type BrowserPurchaseInput = {
transactionId: string;
value: number;
currency: string;
items: TrackbridgeItem[];
affiliation?: string;
coupon?: string;
shipping?: number;
tax?: number;
userData?: UserData;
};
FieldRequiredNotes
transactionIdyesThe dedup key. No auto-generation — purchase is the canonical dual-fire conversion, so a missing ID is treated as a programming error.
valueyesConversion value in currency.
currencyyesISO 4217 (e.g. 'USD').
itemsyesTyped TrackbridgeItem[]. Empty arrays are sent as [] (GA4 distinguishes empty from absent). See TrackbridgeItem.
affiliationnoForwarded as GA4 affiliation param.
couponnoForwarded as GA4 coupon param.
shippingnoForwarded as GA4 shipping param.
taxnoForwarded as GA4 tax param.
userDatanoPass raw — the SDK normalizes and SHA-256 hashes per enhanced conversions.

Promise<void>. Resolves after the gtag pushes have been queued. Like trackConversion, gtag failures are caught and logged via console.warn only when debug: true.

  1. If conversionLabels.purchase is configured and userData is supplied, calls gtag('set', 'user_data', { ... }) with hashed PII.
  2. If conversionLabels.purchase is configured, calls gtag('event', 'conversion', { send_to: '${adsConversionId}/${conversionLabels.purchase}', transaction_id, value, currency }).
  3. Always calls gtag('event', 'purchase', { transaction_id, value, currency, items, affiliation?, coupon?, shipping?, tax? }) for GA4.
  4. items is mapped from camelCase (itemId) to snake_case (item_id) by the same routine used on the server, byte-identically.

Throws synchronously if transactionId is missing or empty. No auto-generation for purchases.

app/checkout/success/page.tsx
'use client';
import { useEffect } from 'react';
import { tracker } from '@/lib/tracker.client';
export function FirePurchase({ order }: { order: Order }) {
useEffect(() => {
tracker.trackPurchase({
transactionId: order.id,
value: order.total,
currency: order.currency,
items: order.items.map((line) => ({
itemId: line.sku,
itemName: line.name,
price: line.unitPrice,
quantity: line.quantity,
})),
coupon: order.couponCode,
userData: {
email: order.customer.email,
},
});
}, [order]);
return null;
}