spyc.ai

Free tool

GTM dataLayer Generator

Write dataLayer.push code for GA4 ecommerce and custom events, with the GTM variables to create.

How it works

What is the GTM dataLayer Generator?

The GTM dataLayer Generator is a free tool that writes dataLayer.push code for Google Tag Manager. Choose a GA4 ecommerce event such as purchase or add_to_cart, or name a custom event, enter the values and items, and copy the snippet. It also lists the Custom Event trigger and the Data Layer Variables to create in GTM.

How to use the GTM dataLayer Generator

  1. Choose an ecommerce event, or choose Custom event and type its name.
  2. Enter the fields as key=value lines, for example value=120 and currency=USD.
  3. For ecommerce events, add one item per line: ID, name, price, quantity.
  4. Copy the push snippet for your developer, and the variable list for your GTM setup.

What is the data layer in Google Tag Manager?

The data layer is a JavaScript array named dataLayer that a page uses to pass information to Google Tag Manager. Your site pushes objects into it, and GTM reads them. A push with an event key can fire a trigger; the other keys become values that tags can use.

It separates two jobs. Developers publish facts about what happened (an order, a sign-up). Whoever runs GTM decides which tags use them, without touching the site's code again.

How do I write a dataLayer.push for a GA4 purchase?

Push an object with event: 'purchase' and an ecommerce object that holds transaction_id, value, currency and an items array. Each item carries at least an item_id or item_name, usually with price and quantity.

The generator also writes dataLayer.push({ ecommerce: null }) before the event. Google recommends this so the previous ecommerce object is cleared and its values do not carry into the next event.

Which Data Layer Variables do I need to create in GTM?

One for each value a tag has to read. In GTM, create a variable of type Data Layer Variable and enter the key as its name, using dot notation for nested keys: ecommerce.value, ecommerce.transaction_id, ecommerce.items. The tool lists every key from your push in that form, with a suggested variable name.

For GA4 ecommerce there is a shortcut. In the GA4 event tag, tick Send Ecommerce data and choose Data Layer as the source, and the tag reads the ecommerce object directly. You then only need variables for values you use elsewhere, such as in a conversion tag for an ad platform.

How do I fire a tag from a dataLayer event?

Create a trigger of type Custom Event and set its event name to the exact value of the event key, for example purchase. The match is case sensitive. Attach the trigger to your GA4 event tag and give the tag the same event name.

Test in GTM's Preview mode: perform the action and check that the event appears in the left column and that each variable shows the value you pushed.

Where should the dataLayer.push go on the page?

Run it when the action happens: on the order confirmation page for a purchase, in the click handler for an add to cart. The snippet starts with window.dataLayer = window.dataLayer || []; so it is safe to run before the GTM container has loaded.

If you install the Google tag directly instead of GTM, use the gtag form from the GA4 Event Generator. If visitors must consent before analytics runs, set the defaults first with the Consent Mode v2 Generator.

Questions

Does a dataLayer.push send data to Google Analytics?
Not by itself. It only adds data to the page's data layer. A tag in GTM has to be triggered by it and forward the values.
Why is my Data Layer Variable undefined?
Usually the key is misspelled, the dot path is wrong, or the tag fires on an earlier event than the push that holds the value. Check the variable on the right event in Preview mode.
Which ecommerce events does the generator cover?
Eleven GA4 ecommerce events, from view_item_list and add_to_cart through begin_checkout, purchase and refund, plus any custom event you name.
Should numbers be strings or numbers?
Send value, price and quantity as numbers. The tool does that, and keeps IDs and names as text.
Is the GTM dataLayer Generator free?
Yes, with a free spyc.ai account. It runs entirely in your browser, so nothing you type or paste is sent to a server.

Analytics Tools

More in Analytics Tools

GA4 Event Generator

Pick a GA4 recommended or custom event, add parameters, and copy the gtag snippet and dataLayer push.

Generate event

GA4 Measurement Protocol Builder

Build a GA4 Measurement Protocol request: JSON body, collect and debug URLs, and a curl command.

Build request

Consent Mode v2 Generator

Set Consent Mode v2 default states, regions and wait time, and copy the gtag default and update snippets.

Generate snippet

Ready when you are

Before your next buyer asks AI about you, ask AI about yourself

One check, four engines, about twenty seconds. We show you the answers before we ask for anything.