如何使用Javascript在GA4中定义内容组(即:使用gtag而非标签管理器)

huangapple go评论71阅读模式
英文:

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_groupdimensions)。

在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上,当我基于内容组显示数字时:

如何使用Javascript在GA4中定义内容组(即:使用gtag而非标签管理器)

我做错了什么?

英文:

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.

&lt;!-- Google tag (gtag.js) - GA4 --&gt;
&lt;script async src=&quot;https://www.googletagmanager.com/gtag/js?id=G-VPPGG41SM8&quot;&gt;&lt;/script&gt;
&lt;script&gt;
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag(&#39;js&#39;, new Date());
gtag(&#39;config&#39;, &#39;G-VPPGG41SM8&#39;);
gtag(&#39;set&#39;, &#39;content_group&#39;, &#39;&lt;%= content_group %&gt;&#39;); // Defined server-side with some business logic
gtag(&#39;set&#39;, &#39;user_properties&#39;, {
&#39;client_type&#39;: &quot;&lt;%= dimensions.client_type %&gt;&quot;, // same here
&#39;subscription_type&#39;: &quot;&lt;%= dimensions.subscription_type %&gt;&quot;, // same here
&#39;mrr&#39;: &quot;&lt;%= dimensions.mrr %&gt;&quot;, // same here
&#39;lifetime_value&#39;: &quot;&lt;%= dimensions.lifetime_value %&gt;&quot;, // same here
});
&lt;/script&gt;
&lt;!-- Google tag (gtag.js) - Universal Analytics --&gt;
&lt;script async src=&quot;https://www.googletagmanager.com/gtag/js?id=UA-45949596-1&quot;&gt;&lt;/script&gt;
&lt;script&gt;
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag(&#39;js&#39;, new Date());
gtag(&#39;config&#39;, &#39;UA-45949596-1&#39;, {
&#39;optimize_id&#39;: &#39;GTM-WG8W868&#39;,
&#39;content_group1&#39;:  &quot;&lt;%= content_group %&gt;&quot;,
&#39;content_group2&#39;:  &quot;&lt;%= content_group_2 %&gt;&quot;,
&#39;custom_map&#39;: {
&#39;dimension1&#39;: &#39;client_type&#39;,
&#39;dimension2&#39;: &#39;subscription_type&#39;,
&#39;dimension3&#39;: &#39;mrr&#39;,
&#39;dimension4&#39;: &#39;lifetime_value&#39;,
},
});
gtag(&#39;set&#39;, {
&#39;content_group1&#39;: &quot;&lt;%= content_group %&gt;&quot;,
&#39;content_group2&#39;: &quot;&lt;%= content_group_2 %&gt;&quot;,
&#39;dimension1&#39;: &quot;&lt;%= dimensions.client_type %&gt;&quot;,
&#39;dimension2&#39;: &quot;&lt;%= dimensions.subscription_type %&gt;&quot;,
&#39;dimension3&#39;: &quot;&lt;%= dimensions.mrr %&gt;&quot;,
&#39;dimension4&#39;: &quot;&lt;%= dimensions.lifetime_value %&gt;&quot;,
});
&lt;/script&gt;

On GA4, when I display numbers based on content group:

如何使用Javascript在GA4中定义内容组(即:使用gtag而非标签管理器)

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(&#39;config&#39;, &#39;G-KEYVALUE&#39;, {
&#39;content_group&#39;: &#39;ONLY_ONE_VALUE&#39;
});

huangapple
  • 本文由 发表于 2023年6月22日 20:15:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76531786.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定