gtag 未发送事件的自定义维度 [英] gtag not sending custom dimensions for events

查看:19
本文介绍了gtag 未发送事件的自定义维度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 gtag 发送到自定义维度时遇到问题.我目前正在关注

现在我使用以下代码在头部初始化我的 gtag:

%script{:async =>"", :src =>"https://www.googletagmanager.com/gtag/js?id=#{APP_CONFIG[:ga_tracking_code]}"}:javascriptwindow.dataLayer = window.dataLayer ||[];函数 gtag(){dataLayer.push(arguments);}gtag('js', new Date());gtag('config', '#{APP_CONFIG[:ga_tracking_code]}', {'custom_map':{维度1":用户类型"'dimension2': 'organization_id'}});

当前记录的事件是这样的

gtag('event', 'test_event', {'event_category': 'test_category','organization_id': 'test_org','user_type': 'test_user_type'});

期待回复,因为过去两天我在解决这个问题上没有取得进展.

解决方案

所以在反复经历了很多次之后,我意识到了问题的原因.

我们的应用程序是 SPA 与服务器端呈现页面的混合体.在我们的前端路由器中,我正在这样做

let path = SomeRouter.currentPathgtag('config', gaTrackingCode, {page_path: path})

问题是我在发送页面视图时没有再次将 custom_map 传入配置

每次调用 gtag('config', gaTrackingCode, configParameters) 时都需要重新发送 configParamters 中的 custom_map设置自定义维度和指标.

所以我把代码改成这样

let path = SomeRouter.currentPathgtag('config', gaTrackingCode,{page_path:路径,自定义地图:{维度1":用户类型"'dimension2': 'organization_id'}})

现在,当我发送事件时,无论路线是否发生变化,自定义维度都会发送到谷歌分析.

I'm having trouble using gtag to send to custom dimensions. I'm currently following their gtag documentation.

Screenshot of the custom dimensions created for my google analytics property

Right now I currently initialize my gtag in the head with the following code:

%script{:async => "", :src => "https://www.googletagmanager.com/gtag/js?id=#{APP_CONFIG[:ga_tracking_code]}"}
:javascript
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());
  gtag('config', '#{APP_CONFIG[:ga_tracking_code]}', {
   'custom_map': {
                   'dimension1': 'user_type'
                   'dimension2': 'organization_id'
                 }
  });

Events are currently logged like this

gtag('event', 'test_event', {
                             'event_category': 'test_category', 
                             'organization_id': 'test_org',
                             'user_type': 'test_user_type'
                            });

Looking forward to responses as I have not made progress figuring this out for the past two days.

解决方案

So after going through this over and over a bunch of times I realized cause of the issue.

Our application is a mix of an SPA with server side rendered pages. In our router for the front end I was doing this

let path = SomeRouter.currentPath
gtag('config', gaTrackingCode, {page_path: path})

The issue was that I was not passing in custom_map into the configuration again when sending the page view

Every time you call gtag('config', gaTrackingCode, configParameters) you need to resend the custom_map in the configParamters if you are setting custom dimensions and metrics.

Therefore the I changed the code to look like this

let path = SomeRouter.currentPath
gtag('config', gaTrackingCode,
  {
     page_path: path,
     custom_map: {
               'dimension1': 'user_type'
               'dimension2': 'organization_id'
               }
   })

Now when I send an event, regardless if the route has changed, the custom dimensions are sent to google analytics.

这篇关于gtag 未发送事件的自定义维度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆