问题描述
我无法使用gtag发送到自定义维度。我目前正在遵循
现在我正在使用下面的代码初始化我的gtag:
%script {: async => ,:src => https://www.googletagmanager.com/gtag/js?id=#{APP_CONFIG[:ga_tracking_code]}}
:javascript
window.dataLayer = window.dataLayer || [];
函数gtag(){dataLayer.push(参数);}
gtag('js',new Date());
gtag('config','#{APP_CONFIG [:ga_tracking_code]}',{
'custom_map':{$ b $'dimension1':'user_type'
'dimension2': 'organization_id'
}
});
事件现在以这种方式记录下来
gtag('event','test_event',{
'event_category':'test_category',
'organization_id':'test_org',
'user_type ':'test_user_type'
});
期待回应,因为过去两天我没有取得进展。因此,在经历了这么多次之后,我意识到了这个问题的原因。
我们的应用程序是SPA与服务器端渲染页面的混合。在我们的前端路由器中,我是这样做的
let path = SomeRouter.currentPath
gtag('config' ,gaTrackingCode,{page_path:path})
问题在于我没有传入<$ c
每当你调用 gtag('config')时, ,gaTrackingCode,configParameters)
您需要重新发送 configParamters
中的 custom_map
设置。
因此,我改变了代码看起来像这样
let path = SomeRouter.currentPath
gtag(' config',gaTrackingCode,
{
page_path:path,
custom_map:{$ b $'dimension1':'user_type'
'dimension2':'organization_id'
}
现在当我发送一个事件时,无论路由是否改变,自定义维度会发送到Google分析。
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不为事件发送自定义维度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!