Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Track campaigns with Google Analytics without query string parameters?

Is there a supported way in Google Analytics to track a campaign without having to use query string parameters.

In Analytics you can tag a link to your site with query string parameters such as utm_campaign and utm_medium which carry information about the campaign so that they can be tracked.

Google actually has an online tool to help in the creation of such links.

For instance if StackOverflow was advertising on Experts Exchange they may have a link like this :

http://www.stackoverflow.com/?utm_source=expertexchange&utm_medium=banner&utm_campaign=a-better-expert-exchange

For many reasons I don't want these clumsy looking parameters appearing in my URLS :

  • I want to encourage twittering, and long links discourage this
  • I dont want people bookmarking them with campaign IDs in
  • I want people to see a clean URL
  • I dont want search engines indexing these links.
  • I want full control about what parameters are sent to google analytics - and not leave it up to my partners to mess up the URLs they access my site with

I looked a while ago to try to find a way wherein you could set these parameters. Google has a page which at first glance looks like the solution, but actually isn't. That page describes how you can change the names of the query string parameters to something else - for instance to use src instead of utm_source you would run :

 pageTracker._setCampSourceKey("src");      

I really cannot seem to figure out why they don't make it easy to actually explicitly set the value of the utm_source key - and not just set up an alternative parameter name for it.

I remember a while back finding someone who had a kind of nasty hack, but I cant even seem to find that now. I seem to recall though that whoever it was took a copy of the analytics code and essentially branched it off and hacked at it. This is not a good solution for me!

is there an officially supported way of doing this at all, without some kind of nasty redirects.

In a nutshell I want to do something like this (ASP.NET MVC site). Give a partnet a link to my site with a URL like this :

 http://www.example.com/?cid=2dae88a8-66b1-475d-8a35-2978bd1a158c 

In the controller for my MVC page I would find out what campaign this GUID related to, and set the model state. Note: this gives me the advantage that i can change the campaign parameters without having to reissue the URL.

In the page itself I would then do this:

var campaignMedium = <%= ViewData.Model.CampaignMedium %>; var campaignSource = <%= ViewData.Model.CampaignSource %>; var campaignName = <%= ViewData.Model.CampaignName %>;  pageTracker._setCampaignData({     utm_source: campaignSource,     utm_medium: campaignMedium,     utm_campaignName: campaignName }); pageTracker._trackPageview(); 

IMPORTANT: This _setCampaignData method DOES NOT ACTUALLY EXIST. This is just 'pseudo code' for what I'd ideally like to be able to do.

Has anyone successfully managed to do anything like this?

like image 618
Simon_Weaver Avatar asked Jun 01 '09 09:06

Simon_Weaver


People also ask

How do I exclude a query parameter in Google Analytics?

One way to remove query parameters from pages is through the View Settings. Under Admin > View Settings > Exclude Query Parameters, list the query parameters that you want to exclude from your page paths.

How do I track my Google Analytics campaign?

To track your marketing campaigns, you need to build a custom campaign URL with UTM codes. Navigate to Insights » Tools and then go to the URL Builder tab. In the URL builder, enter your website URL and other campaign details. Except for the Website URL and Campaign Source, all other fields are optional.


1 Answers

_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.

like image 189
Yahel Avatar answered Oct 11 '22 20:10

Yahel