Mar 23 11

_setCampValues for Google Analytics


A few months ago I published a post about setting GA campaign tracking using JavaScript, offering a detailed explanation of the problem (the fact Google Analytics does not provide a way for setting campaign tracking information using JavaScript), together with a solution for overcoming this problem (implementing extra JavaScript coding).

Many people have since requested this code. I originally felt slightly uncomfortable publishing the code, as I haven’t performed extensive QA testing of the code. I therefore asked people to contact me individually so that I could be sure I explained the method of implementation personally and specifically. However, now that I know that the code is already implemented on a number of sites, I feel much happier posting it here.

That being said, please keep in mind that you should test the code on your testing environment before putting it live.

Please also feel free to contact me if you have any questions. I would be happy to assist.

So what’s the problem?
The problem is that Google Analytics does not allow you to set your campaign details using JavaScript.

By default, Google Analytics looks for the campaign details in the URL. If you pass utm_source, utm_medium etc. in the URL, Google Analytics automatically takes the values from these parameters.

For example, if your URL is: then Google Analytics will use “mysource” and “mymedium” as the campaign details (source and medium).

If you would like to change the parameter names (instead of utm_source and utm_medium), GA allows you to do so by setting the parameter names using _setCampSourceKey, _setCampMediumKey, etc.

Therefore, If your URL is: and you want GA to look in “src” and “md”, you can call _setCampSourceKey(“src“) and _setCampMediumKey(“md“).

The solution: setCampValues
What I would like to present here is an alternative that I have created, which offers a way to set the campaign values in GA using JavaScript. What you need to do is add this code (or the uncompressed version) to your page before the GA code (putting the javascript code inline, not as an external script), then change your tracking code to:

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XX']);
_gaq.push(function() {

Note that I call extga._setCampValues before calling _trackPageview. You should always call _setCampValues before _trackPageview and _setCustomVar

A few notes:

  1. The code does not currently override GA’s built-in traffic source detection. Therefore, if a user comes through Google and you have set different campaign parameters, the code will not change the campaign but will leave it as organic. (Feel free to change this, although I personally recommend keeping it as is.)
    If, however, the user comes through a referring site or directly, the new campaign parameters will override the current campaign parameters.
  2. A very useful function in this extga object is the _getCampValues, which returns the current values that are stored in the utmz cookie. You can do things such as:
    var ts = extga._getCampValues();
    if (ts.isOrganic()) alert('oragnic traffic, visitor searched for '+ts.term+' in '+ts.source);
    else if (ts.isDirect()) alert('direct traffic');
    if (ts.medium == "referral") alert('referring site: '+ts.source);
  3. If you work with cross subdomains (_setDomainName), you should pass the domain name in the last parameter to _setCampValues:
    If you have _gaq.push([‘_setDomainName’, ‘’]);
    then you should pass the domain name as the last parameter:

  4. As default, if you set only part of the campaign information (for example, just the source and medium), the other campaign fields will stay as they were before:
     // current campaign details:
     //  source: 'mysource', medium: 'mymedium', term: 'myterm'
     _gaq.push(function() { extga._setCampValues('mysource1'); });
     // new campaign details:
     //  source: 'mysource1', medium: 'mymedium', term: 'myterm'

    If you want to reset the campaign details before setting new ones, use the _reset flag:

     // current campaign details:
     //  source: 'mysource', medium: 'mymedium', term: 'myterm'
     _gaq.push(function() { extga._reset = true; });
     _gaq.push(function() { extga._setCampValues('mysource1'); });
     // new campaign details:
     //  source: 'mysource1', medium: '(direct)', term: null
  • Lukas O.

    Wow, this really solved our problem. I can’t believe though that Google Analytics doesn’t provide an easier method to set the Campaign Values.

  • Michael

    Great solution, tested and working… exactly what I was looking for, thanks!

  • Noah

    This is great, thanks very much. We’re tracking a custom Drupal module, and because of the way Drupal’s clean URL system works, we can pass data in via the URL, but it’s not feasible to add an actual query string. This is a perfect workaround.

  • Richter Michael

    Tried it out, worked fine… but somehow, my referers disappear 🙁

    • Shay Sharon

      Sorry to hear. If you want me to take a look please email me with the URL of your site.


      • Richter Michael

        wow, fast reaction..

        You may try this:

        the referers semm to be dropped…

        Thanks in advance

        • Shay Sharon

          When you call _setCampValues with “” as your first parameter and “Konzern Website” as you second parameter you are basically asking to override the source and medium. So if a visitor will come from a referring site and GA will store the site domain as source and the fact it is a referring site as medium, your call will override it with different values.

          • Richter Michael


  • Anonymous

    Thanks Buddy!
    Although the new analytics code is different, the same logic applies. You covered what was missing in the documentation 🙂

    • Anonymous

      True. I guess it is time to create a version for Google Universal 🙂

      • Sergio

        Have you had time to create this? It would be an excellent idea

        • Anonymous

          In Google Universal it is not that simple since the campaign details are not stored on the client side (in cookies) like it used to with the previous version. I will try to find a different way. Will keep you updated!

          • Sergio

            In case it helps, I’ve already tried with:

            ga(‘set’, ‘campaignName’, utmCampaign);
            ga(‘set’, ‘campaignSource’, utmSource);
            ga(‘set’, ‘campaignMedium’, utmMedium);
            ga(‘set’, ‘campaignKeyword’, utmTerm);

            but it doesn’t work in Universal Analytics.

            Also, instead of using your method what I was doing before:

            _gaq.push([‘_set’, ‘campaignParams’, utmString]);

            Where utmString had to be:


            Maybe we can find a method that works similar to that one 🙂

          • Sergio


            Already found the answer, it’s even easier than before and I was on the correct track:

            ga(‘create’, ‘UA-XXXXXX-Y’, ‘DOMINIO’);

            ga(‘set’, {‘campaignName’: ‘UTM_CAMPAIGN’, ‘campaignSource’: ‘UTM_SOURCE’, ‘campaignMedium’: ‘UTM_MEDIUM’, ‘campaignContent’: ‘UTM_TERM’});

            ga(‘send’, ‘pageview’);

          • Anonymous

            Nice one!! Thanks for sharing. I will update the post accordingly.

  • Pingback: Solution: Is it possible to set Google Analytics campaign tracking values manually? #programming #dev #answer | InfoBot()

Subscribe for Our Latest SharePoint Analytics Updates

Signup to get the latest SharePoint Analytics posts straight to your inbox!