1

We have a website that uses GA tracking to track traffic and was able to use embed GA components to our CMS.

However we have an instance where its one site but with difference locations in it, so customers can chose a location and only show relevant data. This is all handled by cookies values on the browser eg. cookie name = "LocationId" cookie value = "Melbourne"

Is it possible in GA (also in embed GA )to show data dependent on a cookie value of a website?

1 Answer 1

2

Not by default, but with a tiny bit of programming and configuration.

There is no data field "cookieValue" in GA, so you have to make your own. Go the the property settings, custom definitions, custom dimensions, and click "new custom dimension".

You need to decide on the "scope" of the custom dimension. Since a clients location does probably not change during a session you would probably selecting "session scope", which means the last selected value is applied to all hits in the session.

Your new dimension will have a name that is used in the interface, and a numeric index that you need to send data to this data field. If this is your first custom dimension index will be 1.

Next you need to get the cookie value. There are tons of cookie getter functions for javascript out there, possibly the simplest is

window.getCookie = function(name) {
  match = document.cookie.match(new RegExp(name + '=([^;]+)'));
  if (match) return match[1];
}

Make sure this function definition is included in the page before you call it. it only works really well when the cookie stores but a single value.

You can then send the value to Google Analytics. Custom dimensions need to be sent along with an interaction hit (pageview, event, timinng etc.).

You can either send them along with each hit:

... (code that loads analytics.js)....
ga('create', 'UA-XXXXX-Y', 'auto');
// event will be only applied to the pageview, not the event
ga('send', 'pageview', {
  'dimension1':  getCookie('LocationId');
});
ga('send','event','eventCategory','eventAction');
... (other page code) ...

If you look at the send call you see that the third argument is wrapped in curly brackets - that is called a configuration object, it allows you to pass in one or multiple configuration values for your hit; in this case it's used to set the value for the custom dimension, which is addressed by the "dimension" keyword followed by the numeric index of the dimension you want to send data to.

An alternative syntax would be to use a "set" call. That would send the value once, and have it send along with all subsequent interaction calls:

... (code that loads analytics.js)....
ga('create', 'UA-XXXXX-Y', 'auto');
ga('set','dimension1', getCookie('LocationId'));
// dimension will be applied to both pageview and event
ga('send', 'pageview');
ga('send','event','eventCategory','eventAction');
... (other page code) ...

However for a session scoped custom dimension this does not matter that much, since it is enough to set the value once per session.

Custom dimensions are not part of the standard reports (they are custom. after all), but you can selected them (by the name you gave during configuration) as secondary dimensions, use them for segmentation, create custom reports or Google Data Studio reports or use them in API integrations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.