Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
360 views
in Technique[技术] by (71.8m points)

javascript - gtag not sending custom dimensions for events

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 enter image description here

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...