Leadmetrics
Free tool, no sign-up

GTM dataLayer generator

Build a dataLayer.push for Google Tag Manager, with reserved keys and naming problems caught.

This is the string your GTM trigger listens for. Keep it snake_case and consistent.

Parameter 1

A number or true/false is written unquoted; anything else is a string.

dataLayer push

window.dataLayer.push({
  event: "generate_lead",
});

Before you ship this

  • Note: An event with no parameters can only tell you that something happened. One or two parameters is usually the difference between a number and an answer.
  • Note: This assumes window.dataLayer already exists, which is true once the GTM snippet has run. If this fires earlier, choose the guarded output.
  • Warning: Never push an email address, phone number or name into the dataLayer in the clear. It goes to every tag in the container, including ones you did not intend, and it is a reportable breach in most of Europe.

How to use it

  1. Name the event. This is the exact string your GTM Custom Event trigger listens for, so keep it lowercase snake_case.
  2. Add the parameters you want available as dataLayer variables. Numbers and true/false are written unquoted automatically.
  3. Pick the output shape: a bare push, one with a dataLayer guard for code that runs early, or a named function you can call from a click handler.
  4. Copy it into your page, then add a Custom Event trigger in GTM with the same event name and a Data Layer Variable for each parameter.

The snippet is four lines; the naming is the work

Nothing about a dataLayer.push is hard. What costs time is a container where the same user action is called submit_form on one template, formSubmit on another and lead on a third, because three people added tracking in three different months.

Pick a convention and hold it: lowercase, snake_case, verb first. GA4 uses that convention for its own events, so matching it means your custom events sort alongside the built-in ones instead of in a separate clump.

Keys Tag Manager already owns

GTM writes its own entries into the same array — gtm.start, gtm.element, gtm.uniqueEventId and others. Overwriting one of those from your own push causes failures with no error message and no obvious cause, which is the worst kind of bug to inherit. Keep your keys in your own namespace.

Parameters are the difference between a number and an answer

An event with no parameters tells you that something happened 4,218 times. The same event with form_name and form_location tells you which form, on which page, and therefore what to do next.

Two or three parameters is usually plenty. Twelve is a sign the event is doing more than one job and should be two events.

Types matter more than they look

A value pushed as "49.99" rather than 49.99 arrives in GA4 as a string, and a string cannot be summed. The revenue column reads zero and everything downstream is wrong. This tool writes anything numeric or boolean unquoted for exactly that reason.

GTM dataLayer generator FAQ

Do I need window.dataLayer = window.dataLayer || [] first?

Only if your code might run before the GTM container snippet. Once GTM has loaded, the array exists. The guard costs nothing and prevents a silent failure, so use the guarded output for anything in the head or in a module that loads early.

Why is my GTM trigger not firing?

Almost always the event name. The trigger matches the string exactly, so generate_lead and generateLead are different events, and a trailing space is a different event again. Open GTM Preview, fire the push, and read the event name in the left-hand list.

Can I put an email address in the dataLayer?

You should not. Everything in the dataLayer is readable by every tag in the container, including third-party tags you did not write, and by anyone with the browser console open. Hash it, or send it server-side. An email address pushed in the clear is a reportable breach in most of Europe.

What is the difference between dataLayer.push and gtag?

gtag is Google’s own tagging API and writes into the same array; dataLayer.push is the generic mechanism GTM reads. If you use GTM, push to the dataLayer and let GTM own the gtag calls. Mixing both in one page is how you end up with double-counted events.

Why snake_case for event and parameter names?

Because GA4 expects it, and anything you push is likely to end up there. GA4 event and parameter names are case-sensitive, lowercase by convention, and limited to letters, digits and underscores. Picking the convention it already uses saves renaming everything the first time you connect a tag.

How do I push ecommerce data?

As a single ecommerce object containing the items array, alongside the event name. Push ecommerce as null immediately before the real push: the data layer merges rather than replaces, so without that an earlier purchase leaves stray items attached to the next event.

Why do values from an earlier push keep appearing?

Because the Tag Manager data model is cumulative. Once a key has a value, it keeps it until something overwrites it, so a parameter set on one event is still readable on the next. Push the key with a null or empty value when it should no longer apply.

What is the difference between the data layer and a Data Layer Variable?

The data layer is the array in the page; a Data Layer Variable is the GTM object that reads one key out of it. For nested data use dot notation in the variable name, and leave the version set to 2 unless you are maintaining something very old.

Can I push before the GTM container has loaded?

Yes, as long as the array exists — which is what the guarded output is for. Pushes made before the container loads sit in the array and are processed in order when it initialises, so an early event is not lost. Without the guard, the push throws and the event never happens at all.

How do I see what is actually in the data layer?

Type dataLayer in the browser console for the raw array, and use GTM Preview for the processed view with each event and the variables as they resolved. The two disagreeing is itself informative: it usually means a key was overwritten between the push and the tag firing.