(function(jQuery) {
 
  var pageTracker;
 
 
  /**
   * Track a pageview, e.g.:
   * 
   *   $.geekGaTrackPage('UA-0000000-0');
   */
  jQuery.geekGaTrackPage = function(account_id) {
 
    //check whether to use an unsecured or a ssl connection:
    var host = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
    var src = host + 'google-analytics.com/ga.js';
 
    //load the Google Analytics javascript file:
    jQuery.ajax(
      {
        type:      'GET',
        url:       src,
        success:   function() {
                                //the ga.js file was loaded successfully, set the account id:
                                pageTracker = _gat._getTracker(account_id);
 
                                //track the pageview:
                                pageTracker._trackPageview();
                              },
        error:     function() {
                                //the ga.js file wasn't loaded successfully:
                                throw "Unable to load ga.js; _gat has not been defined.";
                              },
        dataType:  'script',
        cache:     true
      }
    );
 
  };
 
 
  /**
   * Track an event, e.g.:
   * 
   *   $('a.twitter').click(function() {
   *     $.geekGaTrackEvent('feed', 'click', 'Twitter', 'willemvzyl');
   *   });
   */
  jQuery.geekGaTrackEvent = function(category, action, label, value) {
 
    if (typeof pageTracker != undefined) {
      //the pageTracker was defined, track the event:
      pageTracker._trackEvent(category, action, label, value);
    } else {
      //the pageTracker wasn't defined:
      throw "Unable to track event; pageTracker has not been defined";
    }
 
  };
 
})(jQuery);
