gtag not sending custom dimensions for events

2019-03-30 12:43发布

问题:

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.

回答1:

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.



回答2:

Would it be the same format if you had 4 different custom dimensions? such as...

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