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!)
  • 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=""></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=""></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 the article are those of the guest author and not necessarily Search Engine Land.

Related Topics: Channel: Analytics | Search & Conversion


About The Author: is a co-founder of Distilled, a London SEO company. He speaks regularly at industry conferences on analytics, data-driven optimization and data visualization.

Connect with the author via: Email


Get all the top search stories emailed daily!  


Other ways to share:

Read before commenting! We welcome constructive comments and allow any that meet our common sense criteria. This means being respectful and polite to others. It means providing helpful information that contributes to a story or discussion. It means leaving links only that substantially add further to a discussion. Comments using foul language, being disrespectful to others or otherwise violating what we believe are common sense standards of discussion will be deleted. Comments may also be removed if they are posted from anonymous accounts. You can read more about our comments policy here.

Comments are closed.


Get Our News, Everywhere!

Daily Email:

Follow Search Engine Land on Twitter @sengineland Like Search Engine Land on Facebook Follow Search Engine Land on Google+ Get the Search Engine Land Feed Connect with Search Engine Land on LinkedIn Check out our Tumblr! See us on Pinterest


Click to watch SMX conference video

Join us at one of our SMX or MarTech events:

United States


Australia & China

Learn more about: SMX | MarTech

Free Daily Search News Recap!

SearchCap is a once-per-day newsletter update - sign up below and get the news delivered to you!



Search Engine Land Periodic Table of SEO Success Factors

Get Your Copy
Read The Full SEO Guide