Using JavaScript to set your GA campaign variables

Posted 12 years ago by Internetrix

3 Minute(s) to read

One of the most powerful features of Google Analytics (GA) is in its campaign tracking. Within GA's traffic sources reporting section, you can track traffic to your website, including conversion rates, by looking at campaign values.


Aside from AdWords campaigns, which can be automatically linked to your Google Analytics account, you can also track campaigns that deliver traffic to your site from other sources. The secret is through Link Tagging, where you craft a special link to your web page containing some special values which the Google Analytics tracking script knows to look for. When it sees those values, it sets your campaign cookie - the __utmz cookie for the technical ones amongst you - storing things like the campaign name, keywords (known as the terms), the medium (eg, CPC, banner, affiliate) and the content (useful if someone could click on a top banner image and an in-content banner image).


All of this is great, however, you end up with some really ugly URL, looking at bit like:


While this is fine for paid links - banner or CPC - where you can dictate the URL someone will click through on, this just isn't going to work for the increasing amount of traffic that comes from blogs, twitter posts and social network links, not to mention offline traffic where someone comes to your website because they heard your ad on the radio or saw your URL in the paper!


This limitation has been known for a while, and webmasters have had to do a few different things to get around it, none of which are particularly nice for the user.


Solution 1 - META Redirect

The first solution, which is generally easier for webmasters without knowledge or access to configure their web server is to make the call to action page - mostly for things like offline media advertising - a redirect page. By putting almost nothing in the page, except for a meta redirect statement, they can send the visitor off to a page link full of all of those ugly tagged extensions.


In this case, a user would go to and they'd then get redirected immediately to an ugly link like that above.



Solution 2 - URL Redirection or Rewriting

The other solution which is a little less of a hack is to tell your web server that when a user asks for a specific URL, they should instead get a redirect - known as a 302 redirect to the techies out there - which then sends them off to the page full of extra stuff on the URL.


To do this properly, you'll want to be familiar with the finer points of web server directives and configuration, but again, you end up with the same problem - users getting "teleported" to an ugly address, admittedly in this case without them having to load a page first.


A Real Solution

As you can probably see, we're not huge fans of either approach here at Internetrix. The main reason is related to user trust. With the increasing media coverage of phishing scams, where people "think" they're going to their bank's website, for example, only to end up accidentally giving organised criminals their bank logon details, means that things which teleport or push a user around complicated and confusing looking links aren't the greatest idea ever.

Continue Reading: How to set campaign values in Google Analytics using JavaScript