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.
Signature
Section titled “Signature”tracker.exportContext(input?: ExportContextInput): TrackbridgeContext;ExportContextInput
Section titled “ExportContextInput”type ExportContextInput = { userData?: UserData;};| Field | Required | Notes |
|---|---|---|
userData | no | Pass-through. Stored in the envelope as raw input — not normalized, not hashed. The SDK normalizes/hashes when the envelope reaches trackConversion/trackPurchase server-side. |
Returns
Section titled “Returns”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.
Behavior
Section titled “Behavior”- Reads
getClientId(),getSessionId(),getConsent(), the captured click identifiers, and the most recentidentifyUservalue. - Stamps
createdAt = Date.now()(or the configurednowtest seam). - Includes
userDatafrominput.userDataverbatim. - 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.
Example — persist on order creation
Section titled “Example — persist on order creation”'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.
See also
Section titled “See also”- The context envelope — why and when.
serverTracker.fromContext()— the matching hydration call.TrackbridgeContext— the type.