Google Tag Manager

⌘K
  1. Home
  2. Google Tag Manager
  3. Advanced Google Tag Manag...
  4. Pulling Values from Nested Data Layers in Google Tag Manager

Pulling Values from Nested Data Layers in Google Tag Manager

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?

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

  1. In GTM, go to Variables
  2. Click New
  3. Name the variable: DLV – user.role
  4. Variable Type: Data Layer Variable
  5. Data Layer Variable Name: user.role
    Use dot notation (object.subKey) to access nested properties.
  6. Save

Step 3: Repeat for Other Nested Fields (Optional)

Create additional variables as needed:

Variable NameData Layer Variable Name
DLV – user.iduser.id
DLV – user.preferences.languageuser.preferences.language

✅ GTM supports up to 5 levels of nesting.

Step 4: Use Nested Variables in a Tag

  1. Create a GA4 Event Tag or any tag of your choice
  2. Add Event Parameters:
Parameter NameValue
user_id{{DLV – user.id}}
user_role{{DLV – user.role}}
language{{DLV – user.preferences.language}}

Step 5: Preview and Test

  1. Click Preview in GTM
  2. Trigger the dataLayer event on your website (e.g., simulate a sign-up)
  3. 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.

How can we help?