Monitus UI Library

Google Analytics  4

Monitus UI Library > ga > jmui_ga.js (source view)
Search:
 
Filters
/**
 * The GA (Google Analytics) module provides utilities to work with Google Analytics.
 * 
 * @module ga
 * @title Google Analytics
 * @namespace jMUI
 * @requires util
 */

/**
 * The GA (Google Analytics) module provides utilities to work with Google Analytics.
 *
 * @class ga
 * @namespace jMUI
 * @static
 */
jMUI.ready = false;
jMUI.ga = jMUI.ga || {};
jMUI.ga._var_queue = jMUI.ga._var_queue || {};
/**
 * Visitor Scope.
 *
 * @property kvariable_scope_visitor
 * @type Number
 * @final
 * @default 1
 */
jMUI.ga.variable_scope_visitor = 1;
/**
 * Session Scope.
 *
 * @property kvariable_scope_session
 * @type Number
 * @final
 * @default 2
 */
jMUI.ga.variable_scope_session = 2;
/**
 * Page Scope.
 *
 * @property kvariable_scope_page
 * @type Number
 * @final
 * @default 3
 */
jMUI.ga.variable_scope_page = 3;

jMUI.ga.gaqTracker = jMUI.ga.gaqTracker || function(pTrackerObject) {
	if(typeof(pTrackerObject) == "string") {
		var tokens = pTrackerObject.match(/^_gaq(?:\:(.*))?$/);
		if(tokens) return ((tokens[1] && (tokens[1] != "")) ? tokens[1] + "." : "");
	}
	return null;
};

/**
 * Wrapper function for the GA _trackEvent function; using this function instead, 
 * the tracker object does not have to be created when you call thetrack event,
 * and you can use form inflated values.
 *
 * @method track_event
 *
 * @param {string|object} pTrackerObject the name of the tracker object variable, or the actual tracker object. Should now alawys be "_gaq".
 * @param {string} pEventCategory the category for this event; can be an inflatable template.
 * @param {string} pEventAction the action for this event; can be an inflatable template.
 * @param {string} pEventLabel the label for this event; can be an inflatable template.
 * @param {number} pEventValue the value of this event.
 * @param {bool} pNonInteraction when false, event has an impact on bounce rate. When true, the event will not be used in bounce rate calculations. Default: "false"
 *
 * @static
 */
jMUI.ga.track_event = jMUI.ga.track_event || function(pTrackerObject, pEventCategory, pEventAction, pEventLabel, pEventValue, pNonInteraction) {
	if(pEventCategory && pEventAction && (pEventCategory != "") && (pEventAction != "")) {
		var gaq = jMUI.ga.gaqTracker(pTrackerObject);
		if(gaq !== null) {
			var vCategory = jMUI.util.string.form_inflate(pEventCategory);
			var vAction = jMUI.util.string.form_inflate(pEventAction);
			var vLabel = jMUI.util.string.form_inflate(pEventLabel);
			var vValue = parseInt(pEventValue);
			if(isNaN(vValue)) vValue = undefined;
			if(typeof(window._gaq) != "undefined") _gaq.push([gaq + "_trackEvent", vCategory, vAction, vLabel, vValue, pNonInteraction]);
			if(typeof(window.ga) == "function") {
				var event = {eventCategory: vCategory, eventAction: vAction};
				if(vLabel) event.eventLabel = vLabel;
				if(vValue) event.eventValue = vValue;
				ga('set', 'nonInteraction', pNonInteraction);
				ga('send', 'event', event);
			}
		} else {
			pEventLabel = jMUI.util.string.non_null_string(pEventLabel);
			if(typeof(pNonInteraction) == "undefined") pNonInteraction = false;
			var vTracker = null;
			if(typeof(pTrackerObject) == "string") {
				try{ vTracker = eval(pTrackerObject); } catch(e){}
			} else vTracker = pTrackerObject;
			if(!vTracker || (typeof(vTracker) == "undefined") || (typeof(vTracker._trackEvent) != "function")) jMUI.util.wait_object(pTrackerObject, jMUI.ga.track_event, [pEventCategory, pEventAction, pEventLabel, pEventValue, pNonInteraction]);
			else {
				var vCategory = jMUI.util.string.form_inflate(pEventCategory);
				var vAction = jMUI.util.string.form_inflate(pEventAction);
				var vLabel = jMUI.util.string.form_inflate(pEventLabel);
				var vValue = parseInt(pEventValue);
				if(isNaN(vValue)) vValue = undefined;
				vTracker._trackEvent(vCategory, vAction, vLabel, vValue, pNonInteraction);
			}
		}
	}
};
/**
 * Allows you to attach all the event tracking information to an object on a page; 
 * then, when you are ready to send the event, you can use jMUI.ga.track_event_attached_to_object.
 * Particularely useful for buttons, and onclick events: you attach the event data on the butotn, and then it's onclick function simply calls <code>return jMUI.ga.track_event_attached_to_object(this);</code>
 *
 * @method attach_tracking_info
 *
 * @param {jquery object} pObject the object to attach the event data to.
 * @param {string|object} pTrackerObject the name of the tracker object variable, or the actual tracker object.  Should now alawys be "_gaq".
 * @param {string} pEventCategory the category for this event; can be an inflatable template.
 * @param {string} pEventAction the action for this event; can be an inflatable template.
 * @param {string} pEventLabel the label for this event; can be an inflatable template.
 * @param {number} pEventValue the value of this event.
 * @param {bool} pNonInteraction when false, event has an impact on bounce rate. When true, the event will not be used in bounce rate calculations. Default: &quot;false&quot;
 *
 * @static
 */
jMUI.ga.attach_tracking_info = jMUI.ga.attach_tracking_info || function(pObject, pTrackerObject, pEventCategory, pEventAction, pEventLabel, pEventValue, pNonInteraction) {
	if(pObject) {
		if(typeof(pNonInteraction) == "undefined") pNonInteraction = false;
		pObject.data("ga_tracking_info", {
			"tracker": pTrackerObject,
			"action": jMUI.util.string.non_null_string(pEventAction),
			"label": jMUI.util.string.non_null_string(pEventLabel),
			"category": jMUI.util.string.non_null_string(pEventCategory),
			"value": pEventValue,
			"noninteraction": pNonInteraction
		});
	}
};
/**
 * Track an event using the data previously saved on the target object.
 *
 * @method track_event_attached_to_object
 *
 * @param {jquery object} pObject the object to use as the data source for the event call.
 *
 * @return {true} returns TRUE so it can easily be used in an onclick or onsubmit handler.
 *
 * @static
 */
jMUI.ga.track_event_attached_to_object = jMUI.ga.track_event_attached_to_object || function(pObject) {
	if(pObject) {
		var vInfo = pObject.data("ga_tracking_info");
		if(vInfo) jMUI.ga.track_event(vInfo.tracker, vInfo.category, vInfo.action, vInfo.label, vInfo.value, vInfo.noninteraction);
	}
	return true;
};
/**
 * Queues custom variable values so they can be used when the tracker object is finally created.
 * OBSOLETE: use jMUI.ga.queue_variable instead
 *
 * @method queue_custom_variable
 *
 * @param {number} pSlot the variable slot to use.
 * @param {string} pName the variable name.
 * @param {string} pValue the value set for the variable.
 * @param {number} pScope the variable's scope. One of: jMUI.ga.kvariable_scope_visitor, jMUI.ga.kvariable_scope_session or jMUI.ga.kvariable_scope_page (UA: unused)
 *
 * @static
 */
jMUI.ga.queue_custom_variable = jMUI.ga.queue_custom_variable || function(pSlot, pName, pValue, pScope) { jMUI.ga.queue_variable(pName, pValue, pSlot, pScope, pName); };
/**
 * Queues custom variable values so they can be used when the tracker object is finally created.
 *
 * @method queue_variable
 *
 * @param {string} pName the variable name.
 * @param {string} pValue the value set for the variable.
 * @param {number} pSlot the variable slot to use. (GA only)
 * @param {number} pScope the variable's scope. One of: jMUI.ga.kvariable_scope_visitor, jMUI.ga.kvariable_scope_session or jMUI.ga.kvariable_scope_page (GA only)
 * @param {string} pGAName the variable name to send to GA; if undefined or empty, will be set to pName.
 *
 * @static
 */
jMUI.ga.queue_variable = jMUI.ga.queue_variable || function(pName, pValue, pSlot, pScope, pGAName) {
	pName = pName.replace(/\s+/g, "_");
	var vID = (pScope && pSlot) ? pScope + "-" + pSlot : pName;
	if((typeof(jMUI.ga._var_queue[vID]) != "undefined") && (jMUI.ga._var_queue[vID].name != pName)) jMUI.internal._report_error("ga_var_conflict", "GA Custom Variable Conflict: {slot=" + pSlot+", scope=" + pScope + ", name=" + pName + "} vs {slot=" + jMUI.ga._var_queue[vID].slot + ", scope=" + jMUI.ga._var_queue[vID].scope + ", name=" + jMUI.ga._var_queue[vID].name + "}");
	else jMUI.ga._var_queue[vID] = {name: pName, value: pValue.replace(/\s+/g, "_"), slot: pSlot, scope: pScope, gaName: pGAName};
};
/**
 * Adds the previously queued variables to the tracker object.
 *
 * @method append_queued_variables
 *
 * @param {object} pTrackerObject the tracker object.  Should now alawys be "_gaq".
 *
 * @static
 */
jMUI.ga.append_queued_variables = jMUI.ga.append_queued_variables || function(pTrackerObject) {
	var gaq = (pTrackerObject ? jMUI.ga.gaqTracker(pTrackerObject) : null);
	if(!gaq && (typeof(window.GoogleAnalyticsObject) !== "undefined")) gaq = window[window.GoogleAnalyticsObject];
	if(!gaq && (typeof(window.ga) !== "undefined")) gaq = window.ga;
	if(!gaq && (typeof(window._gaq) !== "undefined")) gaq = window._gaq;
	if(!gaq) gaq = null;
	for(var vID in jMUI.ga._var_queue) {
		var vInfo = jMUI.ga._var_queue[vID];
		if(gaq !== null) {
			if(typeof(window._gaq) != "undefined") window._gaq.push([gaq + "_setCustomVar", vInfo.slot, vInfo.gaName, vInfo.value, vInfo.scope]);
			if((typeof(window.ga) == "function") && vInfo.name.match(/^(metric|dimension)[1-9][0-9]*/)) window.ga('set', vInfo.name, vInfo.value);
		} else if(pTrackerObject) pTrackerObject._setCustomVar(vInfo.slot, vInfo.name, vInfo.value, vInfo.scope);
	}
};
jMUI.ready = true;

Copyright © 2017 Yahoo! Inc. All rights reserved.