我从昨天开始一直在尝试用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”中的allitems,item属性。
答案 0 :(得分: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);