How to get values from dataLayer object
Asked Answered
R

6

12

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

Recept answered 18/8, 2017 at 11:4 Comment(3)
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 objectSandysandye
trying dataLayer[3].LinkerRecept
@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?Recept
L
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

Liverish answered 14/2, 2019 at 9:23 Comment(2)
Do you know where this is documented?Damali
No official docs but I recall this comes from the data-layer-helper: github.com/google/data-layer-helperLiverish
G
6

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.

Goldenrod answered 21/8, 2017 at 5:42 Comment(3)
I am getting right dataLayer[3].Linker with settimout function but still onsubmit sometime i am getting emptyRecept
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')})Goldenrod
It's an old question but is there any way to find the index of the datalayer event with event name = 'some value'?Ochre
M
3

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;    
};
Masefield answered 18/6, 2022 at 6:50 Comment(0)
S
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.

Sensor answered 20/8, 2017 at 5:2 Comment(0)
P
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

Priestess answered 28/9, 2021 at 21:8 Comment(0)
C
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');

Croon answered 30/9, 2022 at 16:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.