Sub topic 9
The Purchase Event and `transaction_id`
Assume you know the GA4 event model, JavaScript objects and arrays, and how your shop creates an order at checkout. This chapter focuses on the final commerce event: purchase.
What the purchase event represents
A purchase event records a completed order. Its top-level parameters describe the order, while the items array describes each product in that order.
The most important identifier is transaction_id. It is the order number that uniquely identifies the purchase in your GA4 property. Use the same value for the same order every time. Do not generate a new random value when the confirmation page reloads.
| Parameter | Meaning | Example |
|---|---|---|
| `transaction_id` | Unique order identifier | `T_12345` |
| `currency` | Three-letter ISO 4217 currency code | `USD` |
| `value` | Sum of item price multiplied by quantity | `49.98` |
| `tax` | Tax charged for the order | `4.00` |
| `shipping` | Shipping charge | `5.99` |
| `coupon` | Order-level coupon code | `SUMMER_SALE` |
| `items` | Products included in the order | Array of objects |
The value should normally equal the total of the item values. Send tax and shipping separately when you collect those amounts. This keeps revenue components easier to interpret.
Build a reliable purchase event
Create the event only after the payment system confirms the order. Read the transaction ID and amounts from your server or checkout system rather than trusting values supplied by the browser.
gtag('event', 'purchase', {
transaction_id: 'T_12345',
currency: 'USD',
value: 49.98,
tax: 4.00,
shipping: 5.99,
coupon: 'SUMMER_SALE',
items: [
{
item_id: 'SKU_1001',
item_name: 'Trail Running Shoe',
price: 24.99,
quantity: 2,
index: 0
}
]
});For two units at 24.99, the item value is 49.98. If the order contains several products, include one item object for each product and calculate value from all item rows.
Avoid duplicate and incorrect purchases
A confirmation page can reload, a user can revisit it, or a tag can fire twice. Store a sent-status for each transaction, and make the browser implementation check it before sending. The server should also be the source of truth for whether an order exists.
Common errors include:
- Sending a session ID instead of the order ID.
- Reusing one
transaction_idfor multiple orders. - Sending a string such as
"49.98 USD"instead of a number. - Omitting
currencywhen sendingvalue. - Sending tax and shipping inside
valuewhile also sending them separately. - Sending the purchase event before payment succeeds.
A repeated request with the same transaction ID can help GA4 avoid counting the same transaction twice, but reliable deduplication still requires careful application logic.
Exercise
An order contains two products worth $30 and $20. Shipping is $6 and tax is $5. What should value be if shipping and tax are sent separately?
Show answer
value should be 50, with shipping: 6 and tax: 5.