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 […]

Chat with SearchBot

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!)
  • Take the string you want to insert into a custom variable and encode it (using the JavaScript function encodeURIComponent)
  • Truncate the encoded string to 64 characters minus the length of your variable name
  • Decode the string (using the JavaScript function decodeURIComponent)
  • Pass this string to Google Analytics, where it will be URL encoded again on the way in.

Or, you could simply use my function distilledTruncate(myString) assuming you are using 1-character variable names after including this JavaScript file above your analytics customization code:

<script type="text/javascript" src="https://attributiontrackingga.googlecode.com/svn/trunk/distilled.FirstTouch.js"></script>

Note that this JavaScript file also gives you a couple of other functions to help set up first touch tracking in Google Analytics—something I have written about more on our own site. I am undoubtedly going to improve this code, but if you would like to avoid the possibility of me including something malicious in there, you can link to a static version:

<script type="text/javascript" src="https://attributiontrackingga.googlecode.com/svn-history/r3/trunk/distilled.FirstTouch.js"></script>

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.

If you have any suggestions for improvements to my JavaScript, I have released it under a GPL license and you can report issues here. Feel free to let me know any “gotchas” you have found in the comments!


Opinions expressed in this article are those of the guest author and not necessarily Search Engine Land. Staff authors are listed here.


About the author

Will Critchlow
Contributor

Get the must-read newsletter for search marketers.