英文:
How to define Content Group in GA4 with Javascript (ie: using gtag instead of Tag Manager)
问题
我正在从Universal Analytics迁移到Google Analytics 4。我们不使用Google Tag Manager,而是直接在我们的网页上注入脚本。
如迁移指南建议的那样,我将GA4脚本与UA脚本集成在一起。这两个跟踪应该可以并行工作。
以下是JavaScript代码。它包括一些在服务器端计算的变量(content_group
和dimensions
)。
在UA中,内容组和自定义维度已正确定义。
在GA4中,自定义维度已正确传递,但内容组标记为**(not set)**。
这是我包含在每个网页中的代码。
<!-- Google tag (gtag.js) - GA4 -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-VPPGG41SM8"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-VPPGG41SM8');
gtag('set', 'content_group', '<%= content_group %>'); // Defined server-side with some business logic
gtag('set', 'user_properties', {
'client_type': "<%= dimensions.client_type %>", // same here
'subscription_type': "<%= dimensions.subscription_type %>", // same here
'mrr': "<%= dimensions.mrr %>", // same here
'lifetime_value': "<%= dimensions.lifetime_value %>", // same here
});
</script>
<!-- Google tag (gtag.js) - Universal Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-45949596-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-45949596-1', {
'optimize_id': 'GTM-WG8W868',
'content_group1': "<%= content_group %>",
'content_group2': "<%= content_group_2 %>",
'custom_map': {
'dimension1': 'client_type',
'dimension2': 'subscription_type',
'dimension3': 'mrr',
'dimension4': 'lifetime_value',
},
});
gtag('set', {
'content_group1': "<%= content_group %>",
'content_group2': "<%= content_group_2 %>",
'dimension1': "<%= dimensions.client_type %>",
'dimension2': "<%= dimensions.subscription_type %>",
'dimension3': "<%= dimensions.mrr %>",
'dimension4': "<%= dimensions.lifetime_value %>",
});
</script>
在GA4上,当我基于内容组显示数字时:
我做错了什么?
英文:
I'm migrating from Universal Analytics to Google Analytics 4. We don't use Google Tag Manager, we directly inject scripts on our web pages.
As suggested in migration guides, I integrated the GA4 script next to the UA script. Both tracking should work in parallel.
Here is the Javascript code. It includes some variables computed server-side (content_group
and dimensions
.
In UA, content group and custom dimensions are correctly defined.
In GA4, custom dimensions are correctly passed but but content group is marked as (not set)
Here is the code I include in every web page.
<!-- Google tag (gtag.js) - GA4 -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-VPPGG41SM8"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-VPPGG41SM8');
gtag('set', 'content_group', '<%= content_group %>'); // Defined server-side with some business logic
gtag('set', 'user_properties', {
'client_type': "<%= dimensions.client_type %>", // same here
'subscription_type': "<%= dimensions.subscription_type %>", // same here
'mrr': "<%= dimensions.mrr %>", // same here
'lifetime_value': "<%= dimensions.lifetime_value %>", // same here
});
</script>
<!-- Google tag (gtag.js) - Universal Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-45949596-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-45949596-1', {
'optimize_id': 'GTM-WG8W868',
'content_group1': "<%= content_group %>",
'content_group2': "<%= content_group_2 %>",
'custom_map': {
'dimension1': 'client_type',
'dimension2': 'subscription_type',
'dimension3': 'mrr',
'dimension4': 'lifetime_value',
},
});
gtag('set', {
'content_group1': "<%= content_group %>",
'content_group2': "<%= content_group_2 %>",
'dimension1': "<%= dimensions.client_type %>",
'dimension2': "<%= dimensions.subscription_type %>",
'dimension3': "<%= dimensions.mrr %>",
'dimension4': "<%= dimensions.lifetime_value %>",
});
</script>
On GA4, when I display numbers based on content group:
What am I doing wrong?!
答案1
得分: 1
我有个坏消息,自从GA4以后,你不能再使用多个content_group
。
而且,content_group
必须使用config
来设置,你应该像这样做:
gtag('config', 'G-KEYVALUE', {
'content_group': 'ONLY_ONE_VALUE'
});
英文:
I have bad news, since GA4 you can't use more than one content_group
.
Also content_group
must be set using config
, you should do you something like this:
gtag('config', 'G-KEYVALUE', {
'content_group': 'ONLY_ONE_VALUE'
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论