Question

I'm trying to pass the value of part of the URL to Google Analytics in a custom dimension. The code for custom dimension looks like this

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

The website has URLs with a pattern like /city/san-antonio/places/ etc. What I want to do is have jquery look at the current page URL and see if it contains /city/ and if it does then take the value of the next item and pass it to the Google Analtyics custom dimension. In this example I would want to make the var dimensionValue = 'san-antonio'

Hope that makes sense. Thanks in advance for any assistance.

Was it helpful?

Solution

var dimensionValue = location.href.match(/\/city\/([^/]+)/);
if (dimensionValue&&dimensionValue[1])
  ga('set', 'dimension1', dimensionValue[1]);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top