2

我无法编辑服务器源代码的网站有问题。在标题中,我正在使用 gtag 实现标准的 Google Analytics 安装

<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());
    gtag('config', 'G-XXXXXXXXXX');
    gtag('config', 'UA-XXXXXXXX-1');
</script>

当我发送以下添加到购物车事件时,UA 属性会收集该事件和产品信息,但 GA4 属性不会收集该事件和产品信息。当我使用 JSON.Stringify 变量时,我在 GA 4 属性中获得了添加到购物车事件,但没有产品。

我使用的是 GA4 结构,它说它是向后兼容的。

jQuery(document).ready(function () {
    // Search transaction products. Add each project to an Object. Then add each Object in to an array

    const cmbaordertable = document.querySelector("[id*='ShoppingCart_ItemGrid'] tbody");
    const cmbaordertablelength = document.querySelector("[id*='ShoppingCart_ItemGrid'] tbody").rows.length;

    const cartProducts = []

    for (i = 0; i < cmbaordertablelength; i++) {
        const productSku = String(cmbaordertable.rows[i].cells[6].innerHTML.split("-")[1]);
        const productCat = String(cmbaordertable.rows[i].cells[6].innerHTML.split("-")[0]);
        const productNameHTML = cmbaordertable.rows[i].cells[0].innerHTML;
        const productName = String(productNameHTML.replace(/(<([^>]+)>)/ig,""));
        const productPrice = Number(cmbaordertable.rows[i].cells[2].innerHTML);
//const productQuantity = Number(cmbaordertable.rows[i].cells[1].children[0].value);        
const productQuantity = Number(cmbaordertable.rows[i].cells[1].innerHTML);
const index = i+1;

        const cartProduct = {
            "id": productSku,
            "name": productName,
            "category": "Event",
            "index": index,
            "quantity": productQuantity,
            "price": productPrice
        };
        cartProducts.push(cartProduct);
    }

 const cartProductsFinal = JSON.stringify(cartProducts)

console.log(cartProductsFinal);


  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());
gtag('config', 'G-XXXXXXXXXX', { 'send_page_view': false });
  gtag('config', 'UA-XXXXXXXXXX-1', { 'send_page_view': false });
    gtag('event', 'add_to_cart', {
        'currency': 'USD',
        'items' : [
            cartProducts
        ]
    });
});


</script>
4

0 回答 0