Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
710 views
in Technique[技术] by (71.8m points)

javascript - 如何从数据层返回整个数组作为Google跟踪代码管理器中的数组?(How to return an entire array from datalayer as an array in Google Tag Manager?)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Assuming that you are pushing the data:

(假设您要推送数据:)

dataLayer.push({
    'event': 'productListViewed',
    'productData': {
        'productList': [{
            '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'
        }]
    }
})

You would access the array with a GTM dataLayer variable productData.productList: https://support.google.com/tagmanager/answer/7683362#data_layer When I create a dataLayer variable I name it the same as the object reference:

(您将使用GTM dataLayer变量productData.productList访问该数组: https : //support.google.com/tagmanager/answer/7683362#data_layer创建dataLayer变量时,将其命名为与对象引用相同:)

Then in your analytics tag either add as a dimension or within GTM create the script:

(然后在您的分析代码中添加为维度或在GTM中创建脚本:)

<script>
analytics.track('Product List Viewed', {
list_id: 'Products List',
products:
{{productData.productList}}
});
</script>

You can create the stringified variable too if you wish:

(您也可以根据需要创建字符串化变量:)

<script>
analytics.track('Product List Viewed', {
list_id: 'Products List',
products:
JSON.stringify({{productData.productList}})
});
</script>

This may be useful reading: https://www.simoahava.com/gtm-tips/access-array-members-in-the-data-layer/

(阅读以下内容可能会很有用: https : //www.simoahava.com/gtm-tips/access-array-members-in-the-data-layer/)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...