Recommended:

  • phpclasses.org
  • jsclasses.org
  • jsmag.com
  • siteapps.com
  • View our reviews on Hot Scripts
  • JS Tutorial
  • scripts.com
  • securesignup.com




Recent Comments

Powered by Disqus




Back to articles

Custom events, variables and visitor marking in Google Analytics

As I mentioned before Google Analytics is a powerful tool for analyzing your website's traffic. And best thing is that is for FREE. But not everyone knows that Google Analytic's options are not limited to just inserting code in your website and waiting for default parameters to just accumulate ifnromation. You may provide custom events specifically for your website, set variables to, for example, mark visitor, and then just get them to check if that user was already marked when performing event and much more.

For example, you can allow visitors to rate your website by creating Rating event. Then when visitor rated your website and it generated an event, you just set a variable, which will allow you to do couple of things:
  • Check if user has already rated and not asking him/her again, so he/she won't be bothered
  • Check the activities of users that have rated your website, aggregated by corresponding rated.

In other words, you could know what pages visitor, which rated you as 5 from 5, actually visited.

Here is an example of setting events, setting getting custom variables using Google Analytics. You can combine them anyway you want to achieve your goals.

var custom_analytic = function(){
    //checking if analytics is loaded
    if(typeof _gaq != "undefined")
    {
        /***********************
        * CREATE CUSTOM VARIABLE
        ***********************/
        //passing function to asynchronous
        _gaq.push(function() {
            //create synchrone tracker
            var tracker = _gat._getTrackerByName();
            /*************************
            * SETTING CUSTOM VARIABLE
            * Index: value from 1 to 5, will be used to retrieve value
            * Key: String to name the value
            * Value: String value itself
            * Scope(optional): Set life of value: 
            *   1 - untill visitor deletes it's cookies 
                    or about 2 years of cookie expiration
            *   2 - for visitors session
            *   3 - only for this pageview
            **************************/
            tracker._setCustomVar(1, 'Marking', 'Purpose', 1);
            //need to invoke trackPageview to send information
            //to google analtics
            tracker._trackPageview();
        });
        
        /***********************
        * GET CUSTOM VARIABLE
        ***********************/
        //passing function to asynchronous
        _gaq.push(function() {
            //create synchrone tracker
            var tracker = _gat._getTrackerByName();
            //check if any value was provided by index
            var val = tracker._getVisitorCustomVar(1);
            //check if value exists in index or
            //of course you can also check for specified value
            if(typeof val == "undefined")
            {
                //value doesn't exist
                //user is new
            }
            else
            {
                //value exists
                //user already made action
                //to create an event, etc
            }
        });
        
        /***********************
        * CREATE EVENT
        ***********************/
        //passing function to asynchronous
        _gaq.push(function() {
            //create synchrone tracker
            var tracker = _gat._getTrackerByName();
            /*************************
            * Category: Event Category
            * Action: Name of action
            * Value: String value
            * Int(optional): Additional value (for example rating)
            **************************/
            tracker._trackEvent('Feedback', "Comment", "Comment text", 5);
        });
    }
    else
    {
        //if not we wait a little longer
        setTimeout(custom_analytic, 100);
    }
}

You may also be interested in:

Powered by BlogAlike.com

blog comments powered by Disqus