Audience and prerequisites
Create Data Layer Variables for the exact event keys GTM receives, then prove each value resolves in Preview before a tag uses it. You need the actual data-layer key names, a web container, and a test page that can emit one known event. Skill level: intermediate. Reading time: seven minutes.
1. Inspect the event payload before creating variables
Connect GTM Preview and trigger the application action. In Tag Assistant, select the event and inspect the data layer values. Record the exact key names, nesting, types, and when they become available. A variable path must reflect the payload that is really pushed; guessing from a developer's field label often creates an empty value.
2. Create a Data Layer Variable for each needed field
In the GTM container, create a Data Layer Variable and enter the key path from the observed payload, such as form_id. For nested objects, use the path supported by the data-layer structure and the selected variable version. Give the GTM variable a clear name, then test its value on the event that should use it. Do not create variables for every field by default.
Generic data-layer example
The application may emit a controlled value after successful submission:
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
event: 'generate_lead',
form_id: 'contact_sales'
});This is a generic message to the shared queue. It is not a GA4 API call or another vendor's event. Keep the existing dataLayer; don't overwrite it after GTM loads.
3. Use the variable in a configured tag
For GA4, add the intended event parameter to a GA4 Event tag and select the Data Layer Variable as its value. For a different destination, map only fields supported by that tag and provider. Variables read values; they do not send events. A tag and matching trigger are still required.
4. Check expected and failing cases in Preview
Repeat the action and confirm the variable resolves to the expected value on the correct event. Also test a failure, a page where the key is absent, and a repeat action. If the value is undefined, check exact spelling/case, nesting, push timing, and whether code recreated a separate data layer. If it is stale, inspect whether a later event updated the key and whether the tag fired on the intended message.
Publish only after the positive and negative cases pass. A Preview value proves the variable resolved in that test session; it does not prove a destination received or processed it.
Privacy and data minimization
Only expose fields that have an approved measurement purpose. Never map emails, names, phone numbers, credentials, or free-text form content into tags. Keep consent requirements on each destination tag and check the relevant state during Preview.
Related work
For a matching Custom Event trigger, see Connect a data-layer event to a GTM trigger. For the GA4 tag that uses mapped values, continue with Send a data-layer event to GA4 through GTM.
Official references
- The data layer Checked November 10, 2026
- Set up event parameters Checked November 10, 2026
- Preview and debug containers Checked November 10, 2026
What changed
- v1.0.0Initial bilingual guide drafted and checked against current official Google documentation.