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);

}) 

>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);

Leave a Reply