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

JS add array in tracking code push method

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

有一个名为 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调试页面显示如下:

结果应该是:

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" 中的所有项目,项目 属性。

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