Lesson 5 of 8
The DataLayer, The Data Bus
The dataLayer is a JavaScript array your site uses to hand structured information to GTM. Your site (or a plugin) pushes an object onto it, and GTM listens.
dataLayer.push({
event: "purchase",
ecommerce: {
transaction_id: "T-1042",
value: 49.0,
currency: "USD"
}
});A Custom Event trigger listens for the event name (purchase), and Data Layer Variables read the rest (ecommerce.value, ecommerce.transaction_id). That clean separation is why the dataLayer is the backbone of good tracking.
Two kinds of data
In practice these fall into two patterns. Page context is set on load, the page type, a user's plan, whether they're logged in, so it's ready for the very first event. Interaction events are pushed as things happen, a click, an add to cart, a form submit. GA4 ecommerce is entirely dataLayer-driven: the view_item, add_to_cart and purchase events you tag throughout these courses all arrive this way, which is why getting comfortable with it pays off everywhere.
Good habits
- Push page context before the GTM snippet so it's available on the first event.
- Use consistent, GA4-style event and parameter names.
- Clear the
ecommerceobject before each ecommerce push.
Key takeaway
The dataLayer decouples your site from your tags: the site announces what happened, and GTM decides what to do with it.