我正在尝试使用JavaScript通过“set”命令向GA4中发送的每个事件添加数据:
https://developers.google.com/tag-platform/gtagjs/reference#set
从这些文档中看来,它似乎类似于Serilog Enrichment,但它似乎无法工作,我也没有看到这些数据。我正在使用localhost + Google Analytics Debugger Chrome扩展程序。然后在Analytics > Configure > DebugView中,我可以看到自定义事件“hello-world”和属性“test”,但我没有看到通过“set”命令添加的数据。 GA DebugView 我使用set命令进行了2个调用 - 第一个是“user_id”属性,它确实有效。那一定是一个特殊情况,因为GA对此有不同的处理方式。第二个是用于自定义对象的,但它并不起作用。
控制台显示了两个set命令调用的一些输出,但没有任何提示告诉我是否成功或失败。
<!DOCTYPE html>
<html>
<head>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-SOMECODE"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() {
window.dataLayer.push(arguments);
}
gtag('js', new Date());
// Set enrichment properties for every event "on this page"
// https://developers.google.com/tag-platform/gtagjs/reference#set
gtag('set', {
'foo': 'bar',
});
// Set the measurement id
// https://developers.google.com/analytics/devguides/collection/gtagjs/setting-values
gtag('config', 'G-SOMECODE');
//Set the GA4 user_id and keep it set for all events
gtag('set', {
'user_id': 'a24b935c-03cd-47f0-af68-c60a68b31303'
});
gtag('event', 'hello-world', {
'test': true
});
</script>
<title>Html Delivery</title>
<script type="text/javascript">
function myFunction() {
// Event with nested data test. It doesnt seem to display nicely
gtag('event', 'button-click-nested', {
'data': {
'type': 'nextButton'
}
});
gtag('event', 'button-click', {
'type': 'nextButton'
});
}
function LinkFunction() {
console.log("link click");
gtag('event', 'link click', {
'type': 'link'
});
}
</script>
</head>
<body>
<button onclick="myFunction()">Next</button>
<a id="myLink" href="#" title="Click to do something" onclick="LinkFunction()">link text</a>
</body>
</html>
有一个类似的问题,但我的声誉不允许我评论它以查看它是否仍然存在。 接受的答案对我没有帮助,因为我想能够以这种方式添加任意数据。
我已经尝试在GA中设置自定义指标(我没有使用GTM),但仍然没有数据传递。
这只是不起作用吗?
set
方法出现了问题。 - Nenad Vracar