Objective
To extract (pull) values from nested objects inside the Data Layer using Data Layer Variables in GTM — essential when working with complex data structures, especially for ecommerce, login, or dynamic form events.
What is a Nested Data Layer?
A nested data layer is when values are structured inside objects within the dataLayer. Example:
window.dataLayer.push({
event: “purchase_complete”,
ecommerce: {
transaction_id: “123ABC”,
value: 89.99,
items: [
{
item_name: “Sneakers”,
price: 59.99
}
]
}
});
This structure contains nested keys like:
- ecommerce.transaction_id
- ecommerce.items[0].item_name
Step 1: Simulate or Capture the Data Layer Push
In your website’s code (or via console), ensure a nested dataLayer push happens like this:
window.dataLayer.push({
event: “signup_success”,
user: {
id: “abc123”,
role: “marketing”,
preferences: {
language: “en”,
timezone: “Asia/Ho_Chi_Minh”
}
}
});
This creates deeply nested keys like:
- user.id
- user.role
- user.preferences.language
Step 2: Create a Data Layer Variable for a Nested Value
- In GTM, go to Variables
- Click New
- Name the variable: DLV – user.role
- Variable Type: Data Layer Variable
- Data Layer Variable Name: user.role
Use dot notation (object.subKey) to access nested properties. - Save
Step 3: Repeat for Other Nested Fields (Optional)
Create additional variables as needed:
| Variable Name | Data Layer Variable Name |
| DLV – user.id | user.id |
| DLV – user.preferences.language | user.preferences.language |
✅ GTM supports up to 5 levels of nesting.
Step 4: Use Nested Variables in a Tag
- Create a GA4 Event Tag or any tag of your choice
- Add Event Parameters:
| Parameter Name | Value |
| user_id | {{DLV – user.id}} |
| user_role | {{DLV – user.role}} |
| language | {{DLV – user.preferences.language}} |
Step 5: Preview and Test
- Click Preview in GTM
- Trigger the dataLayer event on your website (e.g., simulate a sign-up)
- In Tag Assistant, confirm:
- The event is received
- The nested values appear correctly under Variables
- Your tag fires and uses the correct values
Note
If a value is not showing, double-check:
- The event name matches your trigger
- The dot notation is correct (object.key1.key2)
For arrays (like products), GTM can’t pull items like items[0].item_name directly in a variable — use a Custom JavaScript Variable instead.