Track campaigns with Google Analytics without query string parameters?

_set campaignParams

Your theoretical "_setCampaignData" finally exists, in the form of ["_set","campaignParams",...]

If you have a way to programmatically inject the values you'd like to set (for example, set by a cookie on a redirect, or on the server side and printed onto the page), you can use the _set API to hard-code the campaign params that you'd like to set.

The format for that is just:

_gaq.push(['_set', 'campaignParams', 
'utm_campaign=CAMPAIGN&utm_source=SOURCE&utm_medium=MEDIUM']);

So, using your original example:

 var campaignMedium = <%= ViewData.Model.CampaignMedium %>;
 var campaignSource = <%= ViewData.Model.CampaignSource %>;
 var campaignName = <%= ViewData.Model.CampaignName %>;
 _gaq.push(['_set', 'campaignParams', 
'utm_campaign=' + campaignName +  '&utm_source=' + campaignSource +'&utm_medium=' + campaignMedium]);

Update 2017

This answer details how to accomplish this with the newer Google Analytics library, analytics.js/Universal Analytics.


Török Gábor gave me an idea.

// ...
var campaignMedium = <%= ViewData.Model.CampaignMedium %>;
var campaignSource = <%= ViewData.Model.CampaignSource %>;
var campaignName = <%= ViewData.Model.CampaignName %>;

// save the old hash
var oldHash = document.location.hash;

// add campaign data to the hash
document.location.hash = 'utm_source=' + escape(campaignSource) + ...;
pageTracker._setAllowAnchor(true);
pageTracker._trackPageview();
// restore the old hash:
document.location.hash = oldHash;

This way, you could create the campaign data in the backend, and then, pass it to the hash dynamically, and then restore it without user even noticing it. I.e. the campaign tracking is 100% independent of the real URL.


The solution using push(['_set', 'campaignParams',... seems only to work for the legacy library ga.js.

Using analytics.js you need to specify the campaign param separately. E.g.

ga('set', 'campaignName', 'TheCampaignName...');
ga('set', 'campaignSource', 'someCampaignSource');
ga('set', 'campaignMedium', 'email');

https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#campaignName