Back

Technologies:

javascriptjavascript
avatar
Tolerim
13 days ago

How can I retrieve the array parameter from the GTM API?

The issue I am facing is that I need to extract the value from the parameter array in which the key is eventName and the value is become_member. Currently, my logic produces output data as shown below. However, it does not retrieve the desired value from the parameter array.

//Output

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



To achieve the desired output, which should look like the example below, I need to extract the value of parameter with key eventName and value become_member.

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

}) 

'
Answers(2)
avatar
Vogi
12 days ago
Verified Answer
To extract the value of a property from an object in JavaScript, you can use the following code:
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 eventParam = tags.parameter.find(param => param.key === 'eventName');
const finalOutput = [tags.name, tags.workspaceId, eventParam.value, tags.containerId, tags.tagId, tags.accountId];
console.log(finalOutput);
This will output the following:
["GA4 Tag",12,"become_member","1234564","89","1898989"]
I hope this helps! Let me know if you have any other questions.

Sources

Extracting nested values in a Javascript object - Stack OverflowHow might I extract the property values of a JavaScript object into an ...Extract property value as array from array of objects in JavaScriptJavaScript Extract Elements From Array - Tuts MakeHow to Extract Part of an Array with JavaScript?How to extract value from an Array in JavascriptHow to extract values from an array of arrays in Javascript?How to Access Object's Keys, Values, and Entries in JavaScriptGet values from an object in JavaScript [duplicate]How to get key and value from JSON object in javascriptJavaScript Program to Extract Given Property Values from Objects as Arrayjavascript - How to get value in an object's key using a variable ...How to get the key of a key/value JavaScript objectObject.fromEntries () - JavaScript | MDN
avatar
Tolerim
13 days ago
To retrieve the value from the parameter array where the key is "eventName" and the value is "become_member", you can access the parameter array in the tags object and use the find method to find the object that matches the key value pair and then retrieve the value property of that object. Here's how you can modify your code to achieve the expected final output:
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 output = [
  tags.name,
  tags.workspaceId,
  tags.parameter.find(param => param.key === "eventName").value,
  tags.containerId,
  tags.tagId,
  tags.accountId
];

console.log("Final Output Required:", output);
console.log("Final Output Fields:", ["Tag", "WorkspaceId", "Event Name", "Container ID", "Tag ID", "Account ID"]);
This should output the following:
Final Output Fields: [ 'Tag', 'WorkspaceId', 'Event Name', 'Container ID', 'Tag ID', 'Account ID' ]
;