Sub topic 10
Implementing E-commerce Events with gtag.js
Prerequisites and the gtag.js model
This chapter assumes that you know basic JavaScript objects and arrays, understand GA4 properties and web data streams, and have already chosen the product and checkout data your shop can provide.
gtag.js is Google's JavaScript library for sending commands from a webpage. An e-commerce event is a command with an event name and an object of parameters. The items parameter contains an array of products. Each product is an object with fields such as item_id, item_name, price, and quantity.
Your site should load and configure the GA4 Google tag before sending events. The examples below run after that setup has completed.
// Product detail page
gtag('event', 'view_item', {
currency: 'USD',
value: 24.99,
items: [
{
item_id: 'SKU_1001',
item_name: 'Trail Running Shoe',
item_brand: 'Acme',
item_category: 'Shoes',
item_variant: 'blue',
price: 24.99,
quantity: 1
}
]
});
// Add-to-cart action
gtag('event', 'add_to_cart', {
currency: 'USD',
value: 49.98,
items: [
{
item_id: 'SKU_1001',
item_name: 'Trail Running Shoe',
price: 24.99,
quantity: 2
}
]
});
// Successful order confirmation
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
}
]
});Choose the event trigger carefully
Send an event when the corresponding customer action has actually happened. For example, send add_to_cart after the server or application confirms that the cart changed. Send purchase only on a successful confirmation page or from a trusted backend process.
A purchase needs a stable, unique transaction_id. Keep the same identifier if the confirmation page reloads. GA4 can use it to help prevent repeated transactions from inflating purchase counts.
The value should represent the value of the items in that event. Calculate it from numeric prices and quantities. Send the ISO 4217 currency code, such as USD or EUR, with every event that includes a monetary value. Do not include currency symbols in numeric fields.
Follow the browser-to-GA4 path
The browser creates the event object, gtag.js places it in its command queue, and the Google tag sends it to the configured GA4 data stream.
Validate before publishing
Use GA4 DebugView while testing. Confirm the event name, transaction_id, currency, value, and every item field. Also check that one customer action creates one event. Test a page reload, back button, payment retry, and failed payment so that purchase logic does not fire more than once.
Exercise
A purchase is reported twice after a customer reloads the confirmation page. What should you inspect first?
Show answer
Inspect the purchase trigger and transaction ID handling. Fire the event only after a confirmed order, and reuse one stable transaction_id so the same order cannot create a new client-side transaction on every reload.