gpt4 book ai didi

javascript - JS在跟踪代码推送方法中添加数组

转载 作者:行者123 更新时间:2023-11-28 05:24:45 25 4
gpt4 key购买 nike

从昨天开始,我一直在尝试在推送方法中传递数组。

有一个名为 Criteo 的跟踪代码,他们需要填写以下内容才能使其正常工作。除了 viewBasket 之外,一切都很好。

<script type="text/javascript">
window.criteo_q = window.criteo_q || [];
window.criteo_q.push(
{ event: "setAccount", account: 11111 },
{ event: "setEmail", email: "username@domain.com" },
{ event: "setSiteType", type: "d" },
{ event: "viewBasket", item: [
{ id: "product_id_1", price: price_1, quantity: quantity_1 },
{ id: "product_id_2", price: price_2, quantity: quantity_2 }
/* add a line for each item in the user's basket */
]}
);
</script>

因此,我创建了一个数组,并用 Criteo 所需的数据(即产品 ID、价格和数量)填充它。虽然在控制台中我可以看到正确的结构,但当我在代码中传递它时它不起作用。

在控制台中我可以看到这一点(第一部分是我插入数组的两行,第二部分是整个数组):

{ id:"20020-278", price: 119, quantity: 1},
{ id:"20009-129", price: 927, quantity: 3},

Array[2]
0: "{ id:"20020-278", price: 119, quantity: 1},"
1: "{ id:"20009-129", price: 927, quantity: 3},"
length: 2__proto__: Array[0]

正是我想要的,但由于某种原因它不起作用。我尝试将其转换为 JSON 数组,或者只是传递一个没有变量的普通行,但仍然遇到这个问题。我还转义了该符号 { ..

<script type="text/javascript">
...
...
var full_line = "\{ id:\""+pid+"\", price: "+price+", quantity: "+quantity+"\},";
//var full_line = "\{ id:20020-278, price:119, quantity:1\},";
//var full_lineJson = JSON.stringify(full_line);

console.log(full_line);
allitems.push(full_line);
</script>

我在 Criteo 代码中传递了“allitems”数组

<script type="text/javascript">
window.criteo_q = window.criteo_q || [];
window.criteo_q.push(
{ event: "setAccount", account: 11111 },
{ event: "setEmail", email: "username@domain.com" },
{ event: "setSiteType", type: "d" },
{ event: "viewBasket", item: [
allitems
]}
);
</script>

在 Criteo 调试页面上显示如下:

enter image description here

结果应该是:

Product ID   Price   Quantity
20010-278 69 1

但是正如你所看到的,结构不知何故被破坏了。我尝试了很多不同的方法,但仍然无法解决这个问题。数组中的结构有问题,但我不确定我还能做什么。请问有什么建议吗?

编辑:如果我传递一个对象

Object {product_id: "20020-278", price: "119", quantity: "1"}
Object {product_id: "20009-129", price: "927", quantity: "3"}

Array[2]
0: Object
price: "119"
product_id: "20020-278"
quantity: "1"

__proto__: Object

1: Object
price: "927"
product_id: "20009-129"
quantity: "3"

__proto__: Object

length: 2__proto__: Array[0]

当我使用对象时,Criteo 站点显示此错误:缺少产品 ID 信息:缺少“item”属性

我用于对象的代码:

<script type="text/javascript">
...
var full_line = {};
full_line.product_id = product_id;
full_line.price = price;
full_line.quantity = quantity;
allitems.push(full_line);
...
</script>

然后我只使用了 Criteo“viewBasket”中的 allitems,item 属性。

最佳答案

var firstLine = {
product_id: "20020-278",
price: "119",
quantity: "1"
};
var secondLine = {
product_id: "20009-129",
price: "927",
quantity: "3"
};

var items = [];
items.push(firstLine);
items.push(secondLine);

var myObj = {
event: "viewBasket",
item: items
};

console.log(myObj);

//window.criteo_q = window.criteo_q || [];
//window.criteo_q.push({
// event: "setAccount",
// account: 11111
//}, {
// event: "setEmail",
// email: "username@domain.com"
//}, {
// event: "setSiteType",
// type: "d"
//}, myObj);

关于javascript - JS在跟踪代码推送方法中添加数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40262912/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com