Google Analytics GTag.js with UA Property and GA4 Property, only the UA property receives Add to Cart Event

Viewed 208

I have a problem with a site I can't edit server source code to. In the header, I am implementing standard Google Analytics install with gtag

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

When I send the below add to cart event, the event and the product info gets collected by the UA property, but neither is collected by the GA4 property. When I use the JSON.Stringify variable, I get the add to cart event in the GA 4 property but no products.

I am using the GA4 structure and it said it was backwards compatible.

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>
0 Answers
Related