dfos_g
dfos_g

Reputation: 3

What is the correct syntax to set a custom dimension in Google Analytics?

I'd like to send some custom dimensions back to GA with every blog post that is read on my website - author and category.

When I set up the custom dimensions in GA I got this code to use:

var dimensionValue = 'SOME_DIMENSION_VALUE'; ga('set', 'dimension2', dimensionValue);

However, in the GA docs it specifies a different syntax using "send" rather than "set". https://developers.google.com/analytics/devguides/collection/analyticsjs/custom-dims-mets ga('send', 'pageview', { 'dimension4': '<?=$categories?>'});

We are using universal google analytics, but this conflicting information means I'm not sure which syntax to use.

Thanks in advance

Upvotes: 0

Views: 535

Answers (1)

nyuen
nyuen

Reputation: 8907

The first code block is how you set the custom dimension (CD) that can be sent with any hit: event, pageview, transaction, etc.

The second code block is how you set the CD and send it with a pageview. So it's a specific example of the first method.

Both are valid, it's just that the second example is more complete.

When sending data to CDs, don't forget to create and define them in the GA configuration as well.

Upvotes: 1

Related Questions