javascript 从GTM API提取参数数组

bnl4lu3b  于 2023-05-05  发布在  Java
关注(0)|答案(1)|浏览(130)

我尝试检索参数数组值,其中键为eventName,值为become_member。到目前为止,我能够实现以下输出。我无法从参数数组中检索值。

//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"]

实际的输出应该类似于以下内容:

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

})
dy1byipe

dy1byipe1#

parameter是对象数组。可以使用find查找键为eventName的对象,并返回其值。

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

相关问题