I have an array in the datalayer I would like to access as a variable in Google Tag Manager.
(我在数据层中有一个数组,希望在Google跟踪代码管理器中作为变量访问。)
I want the entire array though, not just a value from a specific property...(我想要整个数组,而不仅仅是特定属性的值...)
I am trying to fire an eCommerce track like this:
(我正在尝试触发这样的电子商务跟踪:)
<script>
analytics.track(‘Product List Viewed’, {
list_id: ‘Products List’,
products:
‘{{productListArray}}’
});
</script>
The {{productListArray}} variable, a custom javascript type GTM variable, I would like to return the full array from the datalayer variable called {{productList}}, which is a datalayer variable, with variable type of Array in the GTM debugger, that has this value:
({{productListArray}}变量是一个自定义的javascript类型的GTM变量,我想从名为{{productList}}的数据层变量(它是一个数据层变量)返回完整的数组,该变量在GTM调试器中具有Array的变量类型,具有以下值:)
[
{
product_id: ‘132699’,
sku: ‘Brand Name - 132699’,
brand: ‘Brand Name’,
name: ‘Product Name 1’,
price: 1.00,
position: 1,
category: ‘category2’,
},{
product_id: ‘132702’,
sku: ‘Brand Name - 132702’,
brand: ‘Brand Name’,
name: ‘Product Name 2’,
price: 1.00,
position: 2,
category: ‘category1’
}
]
I have been able to pull out a list of specific elements of the array, like product_id, using this method:
(我已经可以使用以下方法提取数组的特定元素列表,例如product_id:)
function() {
var products = {{productList}};
return products.map(function(prod) { return prod.product_id; });
...but I'd like to know the best approach for getting the entire array, with all objects in their entirety, so I can push the entire thing as a single variable in my Product List View track call.
(...但是我想知道获取整个数组以及所有对象完整的最佳方法,因此我可以将整个对象作为单个变量推送到“产品列表视图”跟踪调用中。)
I could probably store the array as text when pushed to the datalayer, but that seems like a bad workaround.(当推送到数据层时,我可能会将数组存储为文本,但这似乎是一个错误的解决方法。)
How do I pull in the entire array of objects instead of just the values for a specific field?(如何提取整个对象数组而不是特定字段的值?)
ask by OldGreg translate from so