diff --git a/js/google_analytics_et.js b/js/google_analytics_et.js index f8d5b2e..4582c11 100644 --- a/js/google_analytics_et.js +++ b/js/google_analytics_et.js @@ -1,7 +1,8 @@ Drupal.behaviors.googleAnalyticsET = { attach : function (context) { - // make sure that the google analytics event tracking object or the universal analytics tracking function exists - // if not then exit and don't track + // Make sure that the google analytics event tracking object or the + // universal analytics tracking function exists if not then exit and + // don't track. if(typeof _gaq == "undefined" && typeof ga == "undefined"){ return; } @@ -13,9 +14,10 @@ Drupal.behaviors.googleAnalyticsET = { label: '', value: 0, noninteraction: false, - options: [] + options: [], + tokens: [] }; - var s = new Array(); + var s = []; for(var i = 0; i < settings.selectors.length; i++) { s[i] = settings.selectors[i].selector; } @@ -33,7 +35,7 @@ Drupal.behaviors.googleAnalyticsET = { ); } -} +}; /** * trackEvent does the actual call to _gaq.push with the _trackEvent type. @@ -53,55 +55,55 @@ Drupal.behaviors.googleAnalyticsET = { * @param opt_value * An integer that you can use to provide numerical data about the user * event. - * @param opt_oninteraction + * @param opt_noninteraction * A boolean that when set to true, indicates that the event hit will not * be used in bounce-rate calculation. + * @param tokens + * A array of tokens and where to find their values in the DOM. */ function trackEvent($obj, id, options, category, action, opt_label, opt_value, opt_noninteraction, tokens) { - var href = $obj.attr('href') == undefined ? false : String($obj.attr('href')); - + var index; // Load token data if present if (undefined != tokens) { - for (var index in tokens) { - tokens[index].value = $(tokens[index].selector)[tokens[index].method](); + for (index in tokens) { + tokens[index].value = jQuery(tokens[index].selector)[tokens[index].method](); } } - - // Replace tokens with values, if present - var properties = new Object(); - properties.category = {string:category}; - properties.action = {string:action}; - properties.label = {string:opt_label}; - for (var index in properties) { + // Replace tokens with values, if present. + var properties = {}; + properties.category = {'string': category}; + properties.action = {'string': action}; + properties.label = {'string': opt_label}; + for (index in properties) { var string = properties[index].string; var stringFirstLetter = string[0]; - // Standard token (from event object) + // Standard token (from event object). if (stringFirstLetter == "!") { switch (string) { case "!text": - properties[index].value = String($obj.text()); - break; + properties[index].value = $obj.text(); + break; case "!href": var href = $obj.attr('href') == undefined ? false : String($obj.attr('href')); properties[index].value = href; - break; + break; case "!currentPage": - properties[index].value = String(window.location.href); - break; + properties[index].value = window.location.href; + break; default: properties[index].value = "Error: Token not found"; } } - // Custom token + // Custom token. else if (stringFirstLetter == "%") { if (undefined != tokens) { - // Token is present + // Token is present. if (undefined != tokens[string]) { - properties[index].value = String(tokens[string].value); + properties[index].value = tokens[string].value; } - // No token found, or not resolved by jQuery moethd + // No token found, or not resolved by jQuery method. else { properties[index].value = "Error: Token not found"; } @@ -110,7 +112,7 @@ function trackEvent($obj, id, options, category, action, opt_label, opt_value, o // Standard text else { - properties[index].value = String(string); + properties[index].value = string; } } @@ -125,7 +127,7 @@ function trackEvent($obj, id, options, category, action, opt_label, opt_value, o // Google complains if category or action are not set so we fail gracefully. if (!category || !action) { - console.log("Google Analytics Event Tracking: category and action must be set.") + console.log("Google Analytics Event Tracking: category and action must be set."); return; } @@ -140,7 +142,7 @@ function trackEvent($obj, id, options, category, action, opt_label, opt_value, o } if (opt_label == '!test' || Drupal.settings.googleAnalyticsETSettings.settings.debug) { - debugEvent($obj, category, action, opt_label, opt_value, opt_noninteraction); + debugEvent($obj, category, action, opt_label, opt_value, opt_noninteraction, tokens); } else if ( typeof ga == 'function' ) { ga('send', { @@ -160,7 +162,7 @@ function trackEvent($obj, id, options, category, action, opt_label, opt_value, o /** * A simple debug function that matches the trackEvent function. */ -function debugEvent($obj, category, action, opt_label, opt_value, opt_noninteraction) { +function debugEvent($obj, category, action, opt_label, opt_value, opt_noninteraction, tokens) { // Saftey First, safe use of console in IE. // http://blog.patspam.com/2009/the-curse-of-consolelog if (!("console" in window)) { @@ -168,13 +170,14 @@ function debugEvent($obj, category, action, opt_label, opt_value, opt_noninterac } else { var trackerObject = { - category : category, - action : action, - opt_label : opt_label, - opt_value : opt_value, - opt_noninteraction : opt_noninteraction, - $object : $obj - } + category : category, + action : action, + opt_label : opt_label, + opt_value : opt_value, + opt_noninteraction : opt_noninteraction, + $object : $obj, + tokens: tokens + }; console.log(trackerObject); } }