从昨天开始,我就一直试图在推送方法中传递一个数组。
有一个名为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代码中传递“所有项”数组
<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“属性中的所有条目。
发布于 2016-10-26 13:25:00
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);
https://stackoverflow.com/questions/40262912
复制相似问题