Sunday, February 7, 2010

Google Analytics _setVar () Function

There is always a solution to your data problems with Google Analytics (GA). You just need to find the correct plug-in/function. In my case, I was trying to find out a way in which I could filter out the visits generated when I surf my own blog. Initially I tried using the Filter manager in GA to exclude my IP. But that didn’t seem to work because of the dynamic IP assignment by ISPs.


The solution to this problem in GA is fairly simple in case you locate the correct function to use. In this case it was the _setVar function.
When this function is invoked (through any event like clicking on a link , etc) a cookie (_utmv) is set on the user’s PC and categorizes him with a value set by the function.
So that brings us back to my problem. Using the _setVar function if I could set a cookie on my PC and exclude all sessions/visits by that cookie (using filter manager), that would solve my problem. Therefore, I created a dummy blog post and named it “Exclude”. Also instead of the simple GA code at the bottom of the blog template
"<"script type='text/javascript'">"

var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));"<"/script">"

"<"script type='text/javascript'">"
try {
var pageTracker = _gat._getTracker("UA-12201163-1");
pageTracker._trackPageview();
} catch(err) {}"<"/script">"
  

I set the following code:
"<"script type='text/javascript'">"
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
"<"/script">"
"<"script type='text/javascript'">"
try {
var pageTracker = _gat._getTracker("UA-12201163-1");
pageTracker._trackPageview();
} catch(err) {}"<"/script">"
"<"b:if cond='data:blog.pageName == "Exclude"'">"
try {
var pageTracker = _gat._getTracker("UA-12201163-1");
pageTracker._trackPageview();
pageTracker._setVar("Exclude");
} catch(err) {}
"<"/b:if">"

Next, I visited the dummy “Exclude” post on my blog and as a result _utmv cookie was set with the value “Exclude”.  Last Step was to use the Filter Manager in GA to filter out all sessions associated with the cookie “Exclude”. I also removed the dummy page, so that none of the other visitors get the cookie. That was it. 
 

Conversion Attribution:
The _setVar can be used for another very important purpose. In case you want to assign sessions or conversions to a particular originating source/medium, you can use this function. Let me give you an example. Let’s say a visitor clicked on the PPC campaign for your site. Visited a particular product page (in case you are an ecommerce site) and exited. Later he came in to your site via organic search and ends up purchasing that product. By default GA will assign this conversion to the “organic search” (last source for that particular visitor), but if you think about it, it was actually the PPC ad that generated the customer’s interest in your site and product. So the credit for the conversion should ideally go to the PPC campaign rather than the organic search result.
The _setVar function can help to do exactly this. When come visits your site from a PPC campaign set a user defined variable Google Analytics that does not get overwritten. You obviously have to do some coding to accomplish this, but it's fairly straightforward.

Even if the regular Google Analytics cookies are overwritten, you still have the User Defined variables hanging around unchanged so that you can see which percentage of direct and organic (and other) traffic has at one point clicked on one of your ads.
You do this by using segmentation within your All Traffic Sources report using the User Defined Variable(the variable that you set using the _setVar funtion )


No comments:

Post a Comment

Feeds