Are utm_source and utm_medium always required for link tagging in Google Analytics?

joelmdev picture joelmdev · Apr 3, 2012 · Viewed 9.1k times · Source

According to every source I've found, in order to manually tag links for campaign tracking in Google Analytics 3 fields are required - utm_campaign, utm_source, and utm_medium. However, I get mixed results when I don't use the latter two.

If I set utm_campaign and utm_source, the campaign is tracked in GA with a medium value of (not set). This seems to work dependably.

If I set utm_campaign in the href of an anchor tag on another site and leave out utm_source, the campaign source shows up as the referring domain when using the GA debugging script but no campaign name is listed nor does not seem to track properly when I look at the reports in Google Analytics. For example, if on www.referringsite.com i have an anchor that looks like
<a href="http://www.mysite.com?utm_campaign=test">click me</a>
The debug tool will show Campaign Source: referringsite and the parameter Campaign Name is not listed.

What is going on here? When do I need to use these parameters and when can I leave them out? My ultimate goal is to allow the referrer to be used as the campaign source when utm_source is not explicitly set, and to leave utm_medium out altogether (which it appears I can do without issue). Can someone explain concretely what has to be used when and where, as well as how to achieve the desired effect of using httpReferrer as the implicit campaign source?

Answer

greg picture greg · Apr 4, 2012

utm_source is typically required if Google Analytics is to recognize any other utm parameters.

To set __utmz cookie (the one that contains source and campaign informations), analytics javascript first checks if a utm_source parameter exist. If not, the script passes through "standard" source setting and doesn't check other parameters like utm_campaign.

But you can achieve the desired effect with some javascript.

In your page, detect there is a utm_campaign parameter without utm_source.
You can use something like :

function isParameterInUrl(param){
    var url = document.location.href;
    var reg = new RegExp('(\\?|&|^)'+param+'=(.*?)(&|$)');
    matches = url.match(reg);
    if (matches[2] != undefined) {
        return true;
    } else {
        return false;
    }
}

Then set utm_source with :

var re_hostname = new RegExp('^(?:f|ht)tp(?:s)?\://([^/]+)', 'im');
var referrer = document.refferer;
_gaq.push(['_setCampSourceKey', referrer.match(re)[1].toString();]);

Be sure to insert your code before the _tackPageView call.