Sub topic 9
Sending GA4 Events with gtag.js or Google Tag Manager
Assume you have a GA4 web data stream, a Google tag installed, and access to the website or Google Tag Manager. You should also know where a successful form submission and a completed order occur in the site flow.
GA4 receives an event, which is a named interaction, together with parameters, which are key-value details about that interaction. Send the event only after the business action succeeds. A button click is not necessarily a lead, and loading a checkout page is not a sale.
Sending events with gtag.js
With gtag.js, call gtag('event', ...) from the site's JavaScript. Use Google's recommended event names when they match your outcome.
For a successful lead form submission, send generate_lead after the server accepts the form:
// Run this after the lead form is successfully submitted.
gtag('event', 'generate_lead', {
currency: 'USD',
value: 25.00
});The value is your estimated value for one lead. Keep the estimate consistent with your business rules. Do not send a value that means something different for different forms unless you document that difference.
For a completed order, send purchase on the confirmation step:
// Run once after the order is confirmed.
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
}
]
});Use the real order identifier for transaction_id. Do not create a random identifier each time the confirmation page loads. A stable identifier helps GA4 recognize repeated submissions of the same order.
Sending events through Google Tag Manager
With Google Tag Manager (GTM), the site pushes an object into the dataLayer, and a GA4 Event tag sends that data to GA4.
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 earlier ecommerce object. This prevents old item data from being combined with the new order.
In GTM, create Data Layer Variables for the values you need. Then create a GA4 Event tag with the following settings:
| GTM setting | Example |
|---|---|
| Event name | `purchase` |
| Parameter `transaction_id` | Data Layer Variable: `ecommerce.transaction_id` |
| Parameter `value` | Data Layer Variable: `ecommerce.value` |
| Parameter `currency` | Data Layer Variable: `ecommerce.currency` |
| Parameter `items` | Data Layer Variable: `ecommerce.items` |
Add a trigger that fires when the event value is purchase. For a lead, use a separate generate_lead event and trigger. Preview the container before publishing it.
Test before publishing
Use GTM Preview or the browser's developer tools to confirm that the action creates exactly one event. Then check GA4 DebugView for the event name and parameters. Realtime can confirm that traffic is arriving, but DebugView is better for inspecting a test event.
Exercise
A purchase event appears twice when a customer refreshes the confirmation page. Which implementation detail should you inspect first?
Show answer
Check whether the event fires on every page load and whether the same stable transaction_id is sent. Fire the purchase only after confirmation and prevent the same order from being submitted repeatedly.