11

I want to get some values from dataLayer object of google tag manager. In chrome tag assistance i am getting values like this

[
  {
    "gtm.start": 1503053374849,
    "event": "gtm.js",
    "gtm.uniqueEventId": 0
  },
  {
    "event": "gtm.dom",
    "gtm.uniqueEventId": 1
  },
  {
    "event": "gtm.load",
    "gtm.uniqueEventId": 2
  },
  {
    "Linker": "_ga=53655374"
  }
]

I need to get the "Linker" value. i tried dataLayer[3].Linker but it gives me "undefined" or blank also same for dataLayer[1].event (it's blank not return value = "gtm.dom") When i try dataLayer[0].event it's return correct 'gtm.js'

Please help me how to get "Linker" value

Rakesh Sharma
  • 13,680
  • 5
  • 37
  • 44
  • 2
    The methods you're using to access the object is correct - assuming the object is available when you run that code, and isn't being retrieved via an async method. To help you we need to see your code that retrieves the object – Rory McCrossan Aug 18 '17 at 11:06
  • trying `dataLayer[3].Linker` – Rakesh Sharma Aug 18 '17 at 11:07
  • @RoryMcCrossan i am trying to print this and save value in a var at my page footer. any way to grab this value on form submit? – Rakesh Sharma Aug 18 '17 at 11:19

6 Answers6

12

You can access the pushed data via the google tag manager javascript-api. The variable part will be the container-id of your GTM container. Make shure you adress the correct one.

google_tag_manager['<container-id>'].dataLayer.get('gtm.start');
//result e.g.: 1210115541132

The result will be the last value of the datalayers state

Moritz
  • 334
  • 5
  • 17
4

Print the Data Layer in a table console.table(dataLayer); and note the index value it will show for Linker.

enter image description here

Then you may use dataLayer[XXX] as XXX being the index value for Linker.

  • I am getting right `dataLayer[3].Linker` with settimout function but still onsubmit sometime i am getting empty – Rakesh Sharma Aug 21 '17 at 05:44
  • Note that some forms in certain circumstances might not trigger a `submit` event, such as the Angular's `click-ng`. First make sure that the `submit` event is triggered by checking `$('#form').on('submit', function(){console.log})`, if not, then you might need to change the button to `type=submit` or trigger `submit` manually `$('#button').on('click', function(){$('#form').trigger('submit')})` – Husain Al Faraj Aug 21 '17 at 05:51
  • It's an old question but is there any way to find the index of the datalayer event with event name = 'some value'? – Si8 Aug 17 '18 at 13:04
2

You can actually get the dataLayer from below code, just call getFromDataLayer('your_object_key_from_dataLayer'); then you should get value from the dataLayer. You could use google_tag_manager['container_id'].dataLayer.get to get the same result but then your code is depend on the GTM Container ID.

function getFromDataLayer(key) { 
    let result = null; 
    dataLayer.push(function() { 
        let value = this.get(key);
        if (value) {
            result = value;
        }
    });
    return result;    
};
user2650480
  • 429
  • 2
  • 5
  • 17
0

The best way is to use Google Tag Manager itself, that's what it's designed for. To do this, go to the GTM interface (tagmanager.google.com) and create a variable of type dataLayer. You can then use this variable in a tag of your choosing to pass its value to a 3rd party system.

faridghar
  • 1,445
  • 2
  • 13
  • 25
0

I've been just looking for an answer to a similar question and see you haven't found a definitive solution.

I've found this article by Simo Ahava which highlights the difference between vanilla JS and GTM when accessing object variables from GTM.

So according to that article, you should be able to use this and get the value:

dataLayer.3.Linker

paulcik
  • 136
  • 1
  • 8
0

Getting the GTM container ID can be an issue depending on environment and the number of containers loaded. This is a quick and easy way to get a value.

google_tag_manager[Object.getOwnPropertyNames(window.google_tag_manager).filter(name => name.includes('GTM'))[0]].dataLayer.get('gtm.start');