Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Extract Parameter array from the GTM API

I am trying to retrieve the parameter array values where the key is eventName and the value is become_member. So far I am able to achieve the following output. I am unable to retrieve the value from the parameter array.

//Output

["GA4 Tag", "12", [{
  key: "sendEcommerceData",
  type: "boolean",
  value: "false"
}, {
  key: "eventName",
  type: "template",
  value: "become_vip_member"
}, {
  key: "measurementId",
  type: "tagReference",
  value: "GA4 - Config - SS - Dummy"
}], "1234564", "89", "1898989"]



The actual output should look something like this:

Final Output Required: [GA4 Tag,12,become_member,1234564,89,1898989]
Final Output Fields:   [Tag, WorkspaceId, Event Name, Container ID, Tag ID, Account ID]

//Data from the API

var tags =  {
"name":"GA4 Tag", 
"workspaceId": "12", 
"parameter": [{
  "type": "boolean",
  "key": "sendEcommerceData",
  "value": "false"
}, {
  "key": "eventName",
  "type": "template",
  "value": "become_member"
}, {
  "key": "measurementId",
  "type": "tagReference",
  "value": "GA4 - Config - SS - Dummy"
}], 
"containerId": "1234564", 
"tagId": "89", 
"accountId": "1898989"

}


//Code

const keys = Object.keys(tags);
console.log(keys);
const rows = []


keys.forEach(function(key) {
console.log(tags[key]);
rows.push(tags[key]);
console.log(rows);

}) 

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

parameter is an array of objects. You can use find to find the object where the key is eventName, and return its value.

const tags={name:"GA4 Tag",workspaceId:"12",parameter:[{type:"boolean",key:"sendEcommerceData",value:"false"},{key:"eventName",type:"template",value:"become_member"},{key:"measurementId",type:"tagReference",value:"GA4 - Config - SS - Dummy"}],containerId:"1234564",tagId:"89",accountId:"1898989"};

const out = [
  tags.name,
  tags.workspaceId,
  tags.parameter.find(p => p.key === 'eventName').value,
  tags.containerId,
  tags.tagId,
  tags.accountId
];

console.log(out);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading