A website stores a lot of parameters into dataLayer google analytics and inside one of my javascript functions, I am thinking of using one of the parameters and used below code to iterate between the properties
var articleId=0;
var brandId=-1;
$.each( dataLayer, function( key, value ) {
console.log( key + ": " + JSON.stringify(value) );
});
some of the values from JSON.stringify(value) are like below
{"BrandID":0}
{"BrandName":" Group"}
{"Title":" announces electric cranes"}
{"PageType":"Article"}
{"ArticleID":8009988}
{"ArticleTitle":" announces electric cranes"}
{"ArticleLayout":"Standard"}
{"ArticleTypeID":"1"}
{"ArticleTypeName":"Article"}
.......................................
and many more
How can I fetch the BrandID and ArticleID out of this? Is there any way of doing this without iterating through all the items of dataLayer
There's a built in method in the google tag manager api to get values:
google_tag_manager['<container-id>'].dataLayer.get('gtm.start');
The container-id is that of your GTM container, something like GTM-XXXXXXX
Edit: for getting values from multiple containers on one page, you can loop through each container after getting their ID's:
Something like:
const containerIds = Object.keys(google_tag_manager).filter(key => key.includes('GTM-'));
const values = containerIds.map(id => google_tag_manager[id].dataLayer.get('gtm.start'));
console.log(values); // [value1, value2, ...]