5

I searched the web for the solution to send multiple custom dimensions to google analytics.

the old tag ga was easy to configure like so :

ga('create', 'UA-ID', 'auto');

    ga('set', 'dimension1', value1);
    ga('set', 'dimension2', value2);
    ga('set', 'dimension3', value3);
    ga('send', 'pageview');

this would send all of the 3 custom dimensions.

the problem occurs with the new gtag.js I tried lots of ways to configure multiple custom dimensions. the documentation shows how to configure 1 custom dimension as so :

gtag('config', 'UA-ID', {
 'custom_map': {'dimension1': 'value'}
});
gtag('event','eventname', {'valuename':value});

This works [for 1 dimension] but I can't figure out how to send multiple custom dimensions.

[tried sending the object with 2 fields of dimension , tried to duplicate the config custom map with different dimensions - it didn't work]

Any ideas ?

Maxim Kogan
  • 135
  • 1
  • 11

1 Answers1

12

You can still use the Legacy version of the code but you have to send it inside a JSON instead, as the example

--Option I-- 
-- Set Option -- 

gtag( 'set' , {'dimension1' : "yxz"} );                      // Set a Single Element
gtag( 'set' , {'dimension2' : "abc",'dimension3' : "123"} ); // Set multiple Elements
gtag('config', 'UA-1-1');                             // Pageview with 3 cd

--Option II--
-- Map Function--


gtag('config', 'UA-ID', {
 'custom_map': {'dimension1': 'value',
                'dimension2': 'value2',
                'dimension3': 'value3'}
});
gtag('event','eventname', {'value1':"1",'value2':"2",'value3':"3"});
Kemen Paulos Plaza
  • 1,560
  • 9
  • 18
  • 1
    works for me - in my case the custom dimensions were set by variables, so the event looked like gtag('event','eventname', {'value1':variable1 ,'value2':variable2 ,'value3':variable3}); and I was able to set them at the bottom of the document - not necessarily right after the gtag config in the head – jcaponi Jun 01 '18 at 19:01
  • Thank you ! you should post it on google analytics forums as well saw many people with the same problem. – Maxim Kogan Jun 04 '18 at 10:48
  • Do we need to define the `event` (e.g. `'eventname'`) somewhere else first and foremost? If yes, may I know how and where to define it? Thanks! – Antonio Ooi Apr 26 '19 at 08:23
  • 1
    The 'eventName' obey to the event action in GA, the category and label must be inside the JSON more info on: https://developers.google.com/analytics/devguides/collection/gtagjs/events , this is only an example for an event, this can be applied to a pageview. Take into consideration what you want to do, maybe the set option can fit better with your needs. – Kemen Paulos Plaza Apr 26 '19 at 09:14
  • 1
    @KemenPaulosPlaza: Thanks for the quick feedback! So you mean I can actually use `gtag( 'set' , {'dimension1' : "yxz"} );` just to send my custom dimension data to GA without using the event? FYI, for my case, I just want to add the custom dimension with a specific javascript variable, e.g. `accountName`. So I can just simply do this: `gtag( 'set' , {'dimension1' : accountName} );`, right? – Antonio Ooi Apr 26 '19 at 10:44