GTM Data Layer Generator
Generate dataLayer.push() code for any GTM event
Build correctly formatted dataLayer.push() snippets for Google Tag Manager. Add custom key-value pairs and get production-ready JavaScript code to add to your website.
How It Works
Enter your event name
Type the GTM event name you want to push to the data layer.
Add key-value pairs
Add as many custom properties as your tag configuration requires.
Copy the snippet
Copy the generated dataLayer.push() code and paste it into your website.
Frequently Asked Questions
What Is the GTM Data Layer and Why Does It Matter?
The Google Tag Manager data layer is a JavaScript array — window.dataLayer — that acts as the communication channel between your website and GTM. Instead of hardcoding values like product IDs, order totals, user IDs, or page categories directly into individual tags, you push them into the data layer once, and every GTM tag can then read those values. This separation of data from tag logic is what makes scalable, maintainable tracking possible.
Without a proper data layer implementation, your GTM tags are forced to scrape values from the DOM (page source) — a fragile approach that breaks every time your developers change a class name, move an element, or redesign a page. A well-structured data layer, combined with this GTM data layer generator, produces clean, developer-ready code that works reliably across every page type and device.
The Anatomy of a dataLayer.push() Call
Every data layer push follows the same basic structure: window.dataLayer.push({ event: 'event_name', key: 'value', ... }). The event key is special — it is the trigger name that GTM listens for. When GTM sees a push with a specific event value, it fires any tags that have a matching Custom Event trigger. All other key-value pairs in the push become Data Layer Variables in GTM that your tags can read.
Key principles for correct data layer pushes: always initialise with window.dataLayer = window.dataLayer || [] before the GTM snippet, push the data layer before the GTM container snippet loads when possible (for page-load data), use consistent data types (strings for IDs, numbers for quantities and values), and never push PII (personally identifiable information) without proper data governance approval.
Data Layer Best Practices for GTM Implementations
A well-designed data layer schema is the foundation of every reliable GTM and GA4 implementation. Follow these best practices to build a data layer that scales:
- Document your schema first. Before writing any code, document every event name, every key, and every expected value type. Share this with developers as a specification, not an afterthought.
- Use snake_case consistently. All data layer keys should use lowercase with underscores: product_id, order_value, user_type. This aligns with GA4's parameter naming conventions.
- Keep ecommerce data in the ecommerce object. GA4's ecommerce tracking expects an ecommerce key containing currency, value, and an items array. Push this structure with every ecommerce event.
- Clear ecommerce before each push. To prevent data from the previous push contaminating the next one, always push
{ ecommerce: null }before each new ecommerce event. - Test with GTM Preview mode. After implementing a data layer push, use GTM Preview to inspect every push in the event stream and verify all expected keys and values are present.
Common Data Layer Errors and How to Fix Them
The most common data layer implementation errors are: pushing the event before window.dataLayer is initialised (losing the push silently), sending string values where GTM expects numbers (causing arithmetic in tags to fail), inconsistent key naming between pages (creating gaps in reports), and forgetting to clear the ecommerce object between pushes (causing stale product data to appear in the wrong event). This GTM data layer generator produces correctly formatted code that avoids all of these pitfalls from the start.
Need Expert Tracking Setup?
Our team implements GA4, GTM, and conversion tracking for 200+ clients worldwide. Get a free audit.
Get a Free Consultation →