Customized Google Analytics: Migrating From setVar To setCustomVar
Many tactics for improving conversion rates rely on accurate data about what is currently happening with visitors to your site. As a result, customizing analytics tracking is a core part of any optimizer’s job. At the end of last year, Google announced a bunch of new features including multiple custom variables accessed via the _setCustomVar function. This replaces the (now deprecated) _setVar.
Custom variables are useful for all kinds of segmentation tasks as well as for adding information to your Google Analytics profile not normally captured by the tracking code. If you have customized your analytics install, there is a fair chance that you are using _setVar. It would be tempting, from reading the announcement to think that _setCustomVar is simply an improvement to _setVar that allows you to set the scope and manage more variables.
It is important, however, to realize that there is a crucial limitation to _setCustomVar that was not there for _setVar:
_setCustomVar(index, name, value, opt_scope)
From Google’s documentation on setCustomVar:
“There is a 64-byte character limit for the name and value combined…”
This throws up a number of a challenges for migrating from _setVar to _setCustomVar. There is the obvious one of not being able to store as much data which is annoying, but subject to persuading the Google team to reverse a design decision there’s nothing you can do about it (what’s the problem guys? Not enough server space to store a few more bytes in that string?). What’s more tiresome is that it’s non-trivial to tell how long your string is because Google URL encodes the string on the way into the system, changing its length! (Note that this is not in the documentation…).
Don’t panic, though! I have a work-around that will hopefully save you from some of the heartache I have been experiencing over the last few days.
The best way I have found to solve the problem is to:
- Give your variable names that are as short as possible—preferably only one character (comment the code to remember what you meant by variable “p” later!)
- Truncate the encoded string to 64 characters minus the length of your variable name
- Pass this string to Google Analytics, where it will be URL encoded again on the way in.
Or, you could simply use my function
It is important that you perform this kind of truncation to avoid passing a string that ends up longer than the limit as _setCustomVar itself will not truncate the input for you—it will simply fail to record a value and return false. It is very likely that this will simply result in missing data in your analytics profile. This kind of relatively silent failure is quite dangerous on an upgrade path like this in my opinion.
Opinions expressed in the article are those of the guest author and not necessarily Search Engine Land.
Analytics news and expert advice every Thursday.