The GTM Data Layer: The Backbone of Reliable Tracking
If you track conversions by looking for a "Thank You" message on a page, you are doing it wrong.
Modern analytics requires a Data Layer. This is a dedicated communication channel between your website's backend and Google Tag Manager. Instead of scraping the visual page (which changes constantly), the Data Layer delivers clean, structured JSON data directly to your tags.
This tool helps you generate the standard code snippets required for GA4 E-commerce and Lead generation tracking.
Why "window.dataLayer.push()"?
You will notice the code generated below uses `.push()`. This is critical.
- Don't overwrite: If you define `window.dataLayer = [...]` later in the page, you might mistakenly delete previous events. `.push()` adds your event to the queue safely.
- Asynchronous Safety: The `window.dataLayer = window.dataLayer || []` check ensures that the code doesn't crash even if GTM hasn't loaded yet.
GA4 E-commerce Schema
Google Analytics 4 is strict about how it receives product data. You cannot just send parameters named whatever you want.
You MUST nest your products inside an `items` array within an `ecommerce` object. If you misspell `item_id` as `productId`, GA4 will simply ignore it, and your reports will be empty.
Pro Tip: Always ask your developers to declare variable types (String vs Number). Sending `price: "99.99"` (string) instead of `price: 99.99` (number) can break some ad platform tags.