Skip to content

tracker.exportContext()

Returns a TrackbridgeContext envelope: a JSON-serializable snapshot of the tracker’s identity, attribution, and consent state. Pair with serverTracker.fromContext() for delayed conversions where the conversion fires from a different process than the user’s session.

For the why and the end-to-end pattern, see The context envelope.

tracker.exportContext(input?: ExportContextInput): TrackbridgeContext;
type ExportContextInput = {
userData?: UserData;
};
FieldRequiredNotes
userDatanoPass-through. Stored in the envelope as raw input — not normalized, not hashed. The SDK normalizes/hashes when the envelope reaches trackConversion/trackPurchase server-side.
type TrackbridgeContext = {
v: 1;
createdAt: number;
clientId?: string;
sessionId?: string;
userId?: string;
clickIds: { gclid?: string; gbraid?: string; wbraid?: string };
consent: ConsentState;
userData?: UserData;
};

v: 1 is the wire-format version. clickIds and consent are always present (possibly empty / 'unknown'). The other fields are present only when the underlying state exists — clientId is omitted when GA4 hasn’t initialized, userId is omitted when identifyUser wasn’t called, and so on.

  1. Reads getClientId(), getSessionId(), getConsent(), the captured click identifiers, and the most recent identifyUser value.
  2. Stamps createdAt = Date.now() (or the configured now test seam).
  3. Includes userData from input.userData verbatim.
  4. Returns a fresh object — mutating the result does not affect tracker state.

exportContext is synchronous and never throws. Calling it on a fresh page that has not yet fired any GA4 hit may return an envelope with no clientId because the _ga cookie hasn’t been set yet.

'use client';
import { tracker } from '@/lib/tracker.client';
async function placeOrder(cart: Cart, customer: Customer) {
const trackbridgeContext = tracker.exportContext({
userData: {
email: customer.email,
phone: customer.phone,
},
});
await fetch('/api/orders', {
method: 'POST',
body: JSON.stringify({ cart, customer, trackbridgeContext }),
});
}

Persist trackbridgeContext as a JSON column on the order row. When the Stripe webhook (or any later server-side process) needs to fire the conversion, hydrate it via serverTracker.fromContext(order.trackbridgeContext) — see the Stripe webhook recipe.