_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: http://www.yourdomain.com/?utm_source=mysource&utm_medium=mymedium 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: http://www.yourdomain.com/?src=mysource&md=mymedium 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:

 

[javascript]
var _gaq = _gaq || [];
_gaq.push([‘_setAccount’, ‘UA-XX’]);
_gaq.push(function() {
extga._setCampValues(
‘<source>’,
‘<medium>’,
‘<name>’,
‘<term>’,
‘<content>’
);
});
_gaq.push([‘_trackPageview’]);
[/javascript]

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:

    [javascript]
    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);
    [/javascript]

  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’, ‘.mydomain.com’]);
    then you should pass the domain name as the last parameter:

    [javascript]
    extga._setCampValues(
    ‘<source>’,
    ‘<medium>’,
    ‘<name>’,
    ‘<term>’,
    ‘<content>’,
    ‘.mydomain.com’
    );
    [/javascript]

  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:

    [javascript]
    // current campaign details:
    // source: ‘mysource’, medium: ‘mymedium’, term: ‘myterm’
    _gaq.push(function() { extga._setCampValues(‘mysource1’); });

    // new campaign details:
    // source: ‘mysource1’, medium: ‘mymedium’, term: ‘myterm’
    [/javascript]

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

    [javascript]
    // 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
    [/javascript]