Google Analytics 增强型电子商务负载在结帐时太大

Google Analytics Enhanced Ecommerce payload too big on checkout

好像是:

ga('send', 'pageview');

不知道如何处理大负载(超过 8K),当我们发送包含 100 多种产品的大交易时,页面印象只是尝试在一个信标中发送所有项目post.

products.forEach(product => ga('ec:addProduct', ...) ) // 100 products
ga('ec:setAction', 'purchase', ...)
ga('send', 'pageview');

这导致

raven.js:80 Payload size is too large (11352).  Max allowed is 8192.

我们只是在关注以下文档:enhanced-ecommerce#measuring-transactions

对 Google Analytics 端点的 HTTP 请求限制为 8Kb(或更准确地说是 8192 字节)。
有一篇很棒的博客 here 讨论了如何管理此溢出。
这个想法是,如果数组中的对象(产品)数量大于您定义的数量,比如说 35,并且访问者选择显示 100 种产品,解决方案是在 3 次点击中自动发送数据以避免点击8Kb 限制。

<script>
if (product.length > 0 || promo.length > 0) {
    var maxProducts = 35;                   // Max objects that will be sent with 1 hit.
    var ecomm = product.concat(promo);      // Merge product & promo into 1 array that we use in the add to cart & click tracking.
while(product.length || promo.length) {
    var p1 = product.splice(0,maxProducts); // Split the product array into arrays with max 35 objects
    var p2 = promo.splice(0,maxProducts);   // Split the promo array into arrays with max 35 objects
    dataLayer.push({
        'ecommerce': {
            'promoView': {
                'promotions': p2
                },
            'impressions': p1
        },
        'event': 'impression', // GTM Event for Impression tracking
        'eventCategory':'Ecommerce','eventAction':'Impression'
        });
    };
};
</script>

经过几次测试,我们似乎找到了解决方案,我们将交易分成 20 件商品的批次,最后我们发送交易全局数据(如税收和运费)。每个批次通过发送交易 id 连接到交易。

            //break the transaction of batches of 20 items
            var idArrays = splitArray(Object.keys(cart.lines), 20),
                transaction = { id: order.id };
                angular.forEach(idArrays, function(ids){
                   angular.forEach(ids, function (id) {
                      var analyticsLine = analyticsCart(cart.lines[id]);
                      ga('ec:addProduct', analyticsLine);
                   });

                   // connect the batch to the transaction 
                   ga('ec:setAction', 'purchase', transaction);
                   ga('send', 'event', 'Checkout', 'Purchase', 'items batch');
                });

            //Send the transaction total data
            var fullTransaction = {
                id: order.id,
                tax: cart.tax,
                shipping: cart.deliveryCost
            };
            ga('ec:setAction', 'purchase', fullTransaction);
            ga('send', 'event', 'Checkout', 'Purchase', 'transaction details');