Search code examples
indexinggoogle-analyticsdimensions

Google Analytics: Can I use custom dimension name instead of index when sending data?


According to Google's documentation, when sending data for custom dimensions or metrics, I have to use their index to identify them, like this:

ga('send', 'pageview', {
  'dimension15':  'My Custom Dimension'
});

or

ga('set', 'dimension5', 'custom data');

I need to know if it's possible to send the data by using the dimension's name (i.e.: "MyDimension") instead of the index. I need to do this in a code that is used by multiple customers, and some already have other custom dimensions defined, while other customers don't. If I were able to target the custom dimension names, I wouldn't have to complicate myself with using different dimension indexes for each customer.


Solution

  • Custom dimension and metric values are sent to Google Analytics at collection time as a pair of index and value parameters. The index parameter corresponds to the index of the custom dimension or metric defined in the Configuration phase.

    // Set value for custom dimension at index 1.
    ga('set', 'dimension1', 'Level 1');
    

    That's directly from the documentation you need to use the index number.

    Update from comment. You cant just send a Json The tracking code uses the Measurement protocol directly the measurement protocol requires that you send the dimension index. you cant hack this. Measurement protocol documentation