1

从昨天开始,我一直在尝试以 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 信息:缺少“项目”属性

我用于对象的代码:

<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 属性。

4

1 回答 1

0

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

于 2016-10-26T13:25:00.970 回答