javascript - 具有 UA 属性和 GA4 属性的 Google Analytics GTag.js,只有 UA 属性接收添加到购物车事件
问题描述
我无法编辑服务器源代码的网站有问题。在标题中,我正在使用 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>
解决方案
推荐阅读
- c# - CS1503 参数 1:无法从“Poc.Core.Player”转换为“Poc.Interfaces.IScheduleable”
- java - 如何在 JAVA 中将字节数组从 BGR24 转换为 JPG?
- javascript - 在javascript中多次执行自定义打字功能时出现问题
- c++ - C++有没有办法丢弃多行std::cin输入
- macos - MAC 上 .net 核心应用程序的核心签名 + 强化运行时
- python-3.x - 将 kv 文件中的标签文本绑定到 python 文件中的方法
- java - 我怎么能说一个字符串匹配我的模式
- python - python中的正弦平方
- pgadmin - 哪个版本的 pgAdmin 开始使用主密码?
- vba - 显示随机值的 MS Access 组合框