Click here to Skip to main content
15,867,488 members
Articles / Web Development / XHTML

Enhancing the Web Analytics Tracking Solution – Google Analytics

Rate me:
Please Sign up or sign in to vote.
2.73/5 (5 votes)
9 Feb 2007CPOL6 min read 45.6K   237   24  
How to enhance and get the most of Google Analytics or any other web tracking tool. This is a good insight on how these applications work, for those who just want to understand more about it.

Introduction

As web developers, one of the most common tasks is to install some kind of web tracking software on the site, so the business people and decision makers can have reports on how the site is performing. One of these tools, and a very good one for its cost (free!), is Google Analytics. In this article, I wish to present a few methods and tips on how to make more of this tool, though they can be implemented and applied to whatever tool or software solution you may be using. The examples in this article use the Google Analytics code, but the principal applies to any analytics tool that works in a similar way. Google Analytics offers just one user defined tracking variable that you can use. This is very limiting for our use, and I hope in the future Google will decide to increase the number of user defined variables it allows on its solution. Other solutions offer more than one custom variable, which obviously gives you a great deal of flexibility in adding insight and depth into the reported data. However, for the purpose of demonstrating a few tracking ideas, even one variable is enough.

The problem

Now let's get to the details of what we want to accomplish. First, I would like to explain the problem we try to solve from a business perspective. Tracking visits to a website is what e-marketing is all about. Each site that uses some kind of online advertising is totally dependent on reporting in order to evaluate if the marketing effort is really worth it. The reporting tools that you get from your ad serving service (such as Google's AdWords, Overture, and so on) gives you very detailed reports on ad performance, and if you set it up correctly on your site, you can have very thorough ROI reports.

Still, there are a few scenarios which "slip" the eyes of these reporting tools. Mostly because it requires more sophisticated coding on the site than the "plug-and-play" code these solutions offer you. Let's look at a classic scenario.

User X goes to some search site called teddysearch.com where you have a paid ad or banner. The user searches for "fluffy teddy bears", and your CPC ad comes up. This user clicks on the ad and is happy to find your e-commerce store which specializes in fluffy teddy bears. The link, which is tagged with your campaign, looks like this:

<a href="http://www.fluffyteddy.com/view.asp?utm_source=
              teddysearch&utm_medium=CPC&utm_content=fluffy">link</a>

Now, while your visitor is innocently viewing your page, Google Analytics code creates a few client-side cookies. This is how Google Analytics is able to track the visitor over page views and reoccurring visits.

The cookie that we are concerned with is called "_utmz", and if we look at the values in the cookie, we will discover all the campaign details:

utmcsr=teddysearch|utmccn=fluffy|utmcmd=CPC

As you can understand, Google Analytics is saving the campaign details in the cookie, and stores it there for 6 months (providing your user does not delete the cookies in the meanwhile).

Storing this data for six months is important because in our scenario this user did not buy the fluffy teddy bear at this moment. He just remembered he had better things to do than browse around for teddy bears, so he leaves your site without purchasing a thing. A week later, a sudden urge of getting his teddy bear emerges in his mind, and he remembered your site. This time, he does not visit the search site, but types your site address directly in his browsers.

Now, because your user is not a web developer, he did not clear his cookies, and therefore when Google Analytics looks to see if a "_utmz" cookie exists, it finds all the details of your CPC campaign, i.e., "teddysearch.com". Now your user happily continues to add his favorite teddy bear to the shopping cart, and then proceeds to checkout.

Wonderful, another happy customer is about to receive a teddy bear, and what is even more wonderful is that Google Analytics just associated this purchase, or transaction, to your "teddysource" CPC campaign.

This is very good in order to track your real cost on advertising. If Google Analytics would not be linking this transaction to the original link it came from, you would think that you are just wasting money on "teddysearch" and no transactions came out of it, as all your transactions would be categorized under "direct", which is the source of anything Google Analytics does have any source for it.

Until now, everything is all nice and clean. But what happens when our users who come to visit our site do not follow the regular, conservative path (and who does?)? Things get a little hard to track. For example, let's assume you have CPC ads in more than one site, and you are trying to see where people are coming from.

Now, in this scenario, our user visited teddysearch.com, saw your ad, clicked and visited the site, but did not buy anything. Like before, Google Analytics recorded this visit as coming from teddysearch.com, but a week later, the same user searches on a different site, sees a different ad, and clicks on that. What happens is that the values in the "_utmz" cookie gets overwritten with the new campaign details.

So now, the "_utmz" cookie will have:

utmcsr=compareyourbear.com|utmccn=teddy|utmcmd=CPC

Now, your user goes and purchases another lovely teddy bear and Google Analytics assigns this transaction to compareyourbear.com, and the fact that this user originally came from teddysearch.com is completely forgotten!

In terms of advertising cost, this transaction actually costs 2 clicks, but was reported as one. I have verified this with Google Analytics tech support, presenting this exact scenario, and receiving an answer which confirmed that the original link is overwritten.

The solution

What I wish to present in this article is a way to get around this problem by adding a bit of code on the site. What this code does is creates another cookie called "MyGoogleTracker" which saves every value in the source element of the tagged URL, and even if the value in the Google cookie is overwritten, the value in this cookie remains, and the new value is appended to it.

Meaning, that in our example, both values will be preserved, in the order they occurred. This value is then placed into the user defined variable that Google Analytics provides, with one line of code.

In the Google Analytics reporting suite, under "user-define", you will see the values coming up in the following way:

teddysearch.com|compareyourbear.com

By using the cross segment tool, you can analyze your data and realize that the second transaction actually belongs to a visitor who went to both sites before making the transaction.

And here is the JavaScript code:

JavaScript
function GetSourceFromUTMZCookie() {
    // getting the value from the original Google Analytics cookie
    var str = document.cookie;
    var source = "";
    var a = str.indexOf("utmcsr=");
    if (a > -1) {
        var val = str.substring(a);
        var b = val.indexOf("|");
        source = val.substring(7,b);
    }
    return source;
}    

function GetValuesFromLocalCookie() {
// Getting the historical values from local cookie
    var str = document.cookie;
    var val = "";
    var a = str.indexOf("MyGoogleTracker=");
    if (a > -1) {
        val = str.substring(a+16);
    }
    return val;
}    

function CreateMyTrackingCookie(source) {
// create the local cookie and append old values
    var existing = GetValuesFromLocalCookie();
    if (existing != source) {
        if (existing != "") {
            source = existing + "|" + source;
        }
        var date = new Date();
        date.setTime(date.getTime()+(365*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
        document.cookie ='_MyGoogleTracker='+source+'; expires='+expires+'; path=/'
    }

}
    
    
//    alert(document.cookie);
//    alert(GetValuesFromLocalCookie());
    var source = GetSourceFromUTMZCookie();
    CreateMyTrackingCookie(source);
//    alert(source);

Place the following code in the end to make the call to the user defined variable:

JavaScript
// sets up the call to the 'user defined' variable in Google Analytics 
__utmSetVar(GetValuesFromLocalCookie());

I hope this article will give you some insight into how Google Analytics works, and the possibilities there are in more advanced web tracking.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
Guy Laor is a web developer and architect. He has developed many websites among them www.rgistore.com

Comments and Discussions

 
-- There are no messages in this forum --