在 Google 跟踪代码管理器中,如何从 dataLayer 数组的所有成员中获取数据?

In Google Tag Manager, how do I get data from all members of dataLayer array?

我正在为电子商务购买做一个数据层推送。 Following the GTM guide, the product items are in an array. The issue is pulling the data: all the guides I've seen mention using an array index——例如transactionProduct.1.price 以获得第二项的 价格 。但这不是一个可靠的解决方案:如果用户购买了 100 多件商品怎么办?由于这个原因,我不能对数组索引进行硬编码。那么,在这种情况下如何动态捕获属性呢?我想我需要使用自定义 JavaScript 用户定义的变量,但那会是什么样子(举个例子会有帮助)?

我相信我已经弄明白了:

如果我们的数据层推送是这样的:

window.dataLayer.push({
    'event' : 'purchase',
    'ecommerce': {
        'transaction': {
            'actionField': {
                 // ...
             },
            'products': // an array of objects...
         }
     }        
});

然后,此 GTM 自定义 JavaScript 变量将 return 列表(每个项目的 价格 ):

function(){
    var productList = {{NAME_OF_DATA_VARIABLE_ON_GTM_OF_ENTIRE_PUSHED_OBJECT}}.transaction.products;

    return productList.map(function (obj) {
        return obj.price;
    });
}