简体   繁体   中英

jquery pass URL value to Google Analytics?

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.

var dimensionValue = location.href.match(/\/city\/([^/]+)/);
if (dimensionValue&&dimensionValue[1])
  ga('set', 'dimension1', dimensionValue[1]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM