Sub topic 8
Preventing Duplicate Sales with `transaction_id`
Assume you have a GA4 property with a web data stream installed and that you understand basic checkout and order concepts. This chapter focuses on purchase events. It does not cover the full checkout implementation or payment processing.
Why duplicate purchases happen
A purchase event records that an order was completed. A duplicate purchase happens when the same order sends that event more than once. Common causes include:
- A customer refreshes the thank-you page.
- A customer returns to the confirmation URL.
- The browser retries a request after a network delay.
- The site fires the purchase tag on both checkout completion and page load.
- Google Tag Manager has two purchase tags.
- A single-page application runs the purchase code again during navigation.
Without a way to recognize the order, GA4 may treat repeated events as separate sales. Revenue, purchase counts, and key event results then become too high.
How `transaction_id` works
transaction_id is a purchase parameter that identifies the order. Set it to the stable order number from your commerce system, such as T_12345. Send the same value every time the same order is reported.
Use a different value for every legitimate order. Do not create the value from the current time or a random number when the confirmation page loads. A new random value makes every retry look like a new transaction.
| Value | Correct use |
|---|---|
| `T_12345` | Stable identifier for one order |
| `T_12346` | Identifier for a different order |
| Random value on every page load | Incorrect because retries cannot be recognized |
GA4 uses the transaction identifier to help prevent duplicate ecommerce purchases. It is still better to make the confirmation page and tag fire only once. Deduplication reduces the damage from retries; it does not fix incorrect order logic.
// Run after the order is confirmed by the server.
gtag('event', 'purchase', {
transaction_id: 'T_12345',
value: 59.98,
currency: 'USD',
items: [
{
item_id: 'SKU_123',
item_name: 'Trail Shoes',
price: 59.98,
quantity: 1
}
]
});The order system should provide transaction_id. Do not accept an order number typed by the customer, and do not place personal information such as an email address in the parameter.
Preventing duplicates in Google Tag Manager
If you use Google Tag Manager, expose the server-confirmed order data in the data layer. Push the purchase object once on the confirmation state, then configure one GA4 Event tag for the purchase event.
window.dataLayer = window.dataLayer || [];
dataLayer.push({ ecommerce: null });
dataLayer.push({
event: 'purchase',
ecommerce: {
transaction_id: 'T_12345',
value: 59.98,
currency: 'USD',
items: [
{
item_id: 'SKU_123',
item_name: 'Trail Shoes',
price: 59.98,
quantity: 1
}
]
}
});The first push clears an older ecommerce object. This prevents stale items from being combined with the current order. In Tag Manager, check that only the intended purchase tag uses the purchase trigger.
Testing duplicate prevention
Test these cases in DebugView and Realtime:
- Complete one test order.
- Confirm that the purchase event contains the expected
transaction_id. - Refresh or revisit the confirmation page.
- Check whether your implementation sends the event again.
- Confirm that a separate order has a different transaction ID.
Also compare GA4 purchase data with the commerce system. Investigate any mismatch before marking the event as a key event or using it for advertising decisions.
Exercise
A confirmation page sends purchase each time it loads and generates a new random transaction ID. What should you change?
Show answer
Use the stable order ID from the commerce system as transaction_id, and prevent the confirmation logic from firing repeatedly when the same order page is revisited.