Sub topic 8
Preventing duplicate sales with transaction_id
This chapter assumes you can already send a purchase event with gtag.js or Google Tag Manager (GTM). It also assumes you know the difference between an event and an event parameter. Here you will learn why sales get counted twice, what transaction_id does about it, and where it does not help.
Why duplicate sales happen
A sale is the one outcome where double counting directly distorts money. If a $59.98 order is recorded twice, revenue reports show $119.96 and the return on ad spend looks better than it is. Key event counts and Google Ads bidding are skewed in the same way.
Most duplicates come from a short list of causes:
- The buyer reloads the order confirmation page.
- The buyer uses the browser's back button, or bookmarks the confirmation URL and returns to it later.
- The confirmation page fires the tag twice, because both a hard-coded
gtagcall and a GTM tag are installed. - A single-page app re-renders the confirmation view and triggers the event again.
- The same order is sent from the browser and from a server (for example through the Measurement Protocol) without coordination.
- A GTM trigger is set to fire on "All Pages" and also on a custom event, so it matches twice.
None of these produces an error. GA4 accepts each event as valid, so the problem only shows up when revenue in GA4 disagrees with the order system.
What transaction_id does
transaction_id is a parameter of the purchase event. It holds a string that uniquely identifies one order, such as an order number from your shop. GA4 uses it as a deduplication key: when it receives more than one purchase event with the same transaction_id, it keeps the first one and discards the later ones from reporting.
Three details matter in practice:
- The ID must be unique per order and stable for that order. Use the order number your back office uses, not a timestamp or a random value generated at page load. A random value defeats the purpose, because every reload produces a new ID.
- It is a string. Send
'T_12345'or'10045', and keep the format consistent between browser and server sends. - The deduplication is applied during processing. Raw events can still appear in DebugView, so seeing two events there does not prove that both will be counted in reports.
The recommended purchase event from the topic plan looks like this:
// Fire once on the order confirmation page
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
}
]
});If transaction_id is missing or empty, GA4 has nothing to compare, so every purchase event counts as a separate sale. In the ecommerce reports the order shows up under (not set) for the Transaction ID dimension, which is a quick way to spot the problem.
Where deduplication happens
There are two layers. The browser-side guard is yours and stops the event from being sent at all. The GA4 processing step is Google's and acts as a safety net. Relying only on the safety net works most of the time, but it is best effort. Treat it as the second line of defense, not the first.
Add a browser-side guard
A guard remembers which order IDs this browser has already reported. The simplest version uses localStorage, which survives reloads and later visits. The function below sends the event only the first time it sees an order ID.
function sendPurchaseOnce(order) {
const key = 'ga4_purchase_sent_' + order.id;
try {
if (window.localStorage.getItem(key)) {
return false; // already reported from this browser
}
} catch (e) {
// Storage can be blocked. Fall through and rely on GA4 deduplication.
}
gtag('event', 'purchase', {
transaction_id: order.id,
value: order.total,
currency: order.currency,
items: order.items
});
try {
window.localStorage.setItem(key, '1');
} catch (e) {
// Ignore: the guard is best effort.
}
return true;
}
sendPurchaseOnce({
id: 'T_12345',
total: 59.98,
currency: 'USD',
items: [
{ item_id: 'SKU_123', item_name: 'Trail Shoes', price: 59.98, quantity: 1 }
]
});This example assumes gtag.js is already loaded on the page. The guard has limits. It works per browser, so a buyer who opens the confirmation link on another device is not blocked. The try blocks keep the page working when storage is disabled, in which case you depend on GA4 deduplication alone.
A server-side alternative is stronger. Your shop can record a flag such as analytics_sent = true on the order and render the tracking snippet only while the flag is false. This works across devices and browsers, and it also handles the case where a customer returns to the confirmation URL a week later.
Do the same in Google Tag Manager
With GTM, the dataLayer push replaces the gtag call. The structure is the same, and transaction_id sits inside the ecommerce object.
window.dataLayer = window.dataLayer || [];
dataLayer.push({ ecommerce: null }); // clear the previous ecommerce object
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 }
]
}
});Check these points in the GTM container:
- Only one GA4 event tag should fire for
purchase. Open Preview mode, complete a test order, and confirm the tag fires once. - The trigger should be the custom event
purchase, not "All Pages" combined with another trigger. - The tag should send the ecommerce data from the dataLayer. In the GA4 event tag, enable "Send Ecommerce data" with the dataLayer as the source.
- If the site is a single-page app, make sure the push happens once per order and not on every route change or re-render.
Do not install the same purchase tracking through both GTM and a hard-coded gtag call. This is the most common source of exact duplicates, and it produces two events with the same ID that GA4 may or may not collapse.
What transaction_id does not cover
The ID is tied to the purchase event. Keep these boundaries in mind:
| Situation | Does transaction_id help? | What to do instead |
|---|---|---|
| Reloaded confirmation page | Yes, GA4 drops later events with the same ID | Add a browser-side or server-side guard as well |
| Browser and server both send the order | Yes, if both use the identical ID | Use the same ID format in both places |
| `generate_lead` events | No, leads have no transaction ID | Guard in code, or use the key event counting method |
| Different IDs for the same order | No | Use one stable order number everywhere |
| Refund of a sale | Used for matching, not removal | Send a `refund` event with the original `transaction_id` |
Leads need separate handling. A form that submits twice produces two generate_lead events, and nothing deduplicates them by default. Disable the submit button after the first click, or fire the event only after the success response. The counting method chosen when you mark the event as a key event is covered in a later chapter.
Check that deduplication works
- Place a test order and open the confirmation page. Note the order ID.
- Reload the page three times and press back and forward once.
- In DebugView, count how many
purchaseevents arrive. With a guard in place you should see one. Without a guard you may see several, which is expected, since DebugView shows raw events. - The next day, open Reports, then Monetization, then Ecommerce purchases (the exact menu names can vary). Search for the test order's ID and confirm it appears once.
- Compare the day's GA4 purchase count with your order system. Small gaps are normal because of consent choices and ad blockers. A GA4 count higher than the order count suggests duplicates.
Common mistakes
- Generating
transaction_idwithDate.now()or a random string at page load. - Sending the order number as a number in one place and a string with a prefix in another, so the two sends do not match.
- Leaving
transaction_idout in testing and shipping that version to production. - Testing with real order IDs and then re-testing the same IDs, so later tests look "missing" because GA4 treats them as duplicates.
- Putting an email address or name inside the ID. IDs must not contain personal data, so use an order number that means nothing outside your own systems.
Exercise
A shop sees GA4 report 480 purchases for a week while its order system shows 400. DebugView shows each test order fires one purchase event. The Transaction ID dimension shows 80 rows labeled (not set). What is the most likely cause, and what is the fix?
Show answer
Some purchase events are being sent without a transactionid, probably from a second tag or a checkout path (such as a payment redirect) that omits it. GA4 cannot deduplicate events that have no ID, so reloads and repeat sends are counted separately. Find the tag or code path that sends purchase without transactionid, add the order number there, and make sure only one tag fires per order.
Exercise
Why is a transaction_id built from Date.now() on the confirmation page a poor choice, even though it looks unique?
Show answer
It changes on every page load, so a reload produces a new ID and GA4 sees a different transaction. The ID must come from the order itself and stay the same each time the order is reported.
Exercise
Your site sends purchases from the browser and also from the server through the Measurement Protocol. What must be true for GA4 to treat them as one sale?
Show answer
Both sends must be purchase events with the same transactionid value in the same format (for example, both the string 'T12345'). If the formats differ, GA4 counts two sales.