Monitus UI Library

Google Analytics  4

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

if(typeof(MUI) != "undefined") {
	if(typeof(MUI.ga) == "undefined") {
		/**
		 * The GA (Google Analytics) module provides utilities to work with Google Analytics.
		 *
		 * @class ga
		 * @namespace MUI
		 * @static
		 */
		MUI.ga = {
			_var_queue: {},
			/**
			 * Visitor Scope.
			 *
			 * @property kvariable_scope_visitor
			 * @type Number
			 * @final
			 * @default 1
			 */
			kvariable_scope_visitor: 1,
			/**
			 * Session Scope.
			 *
			 * @property kvariable_scope_session
			 * @type Number
			 * @final
			 * @default 2
			 */
			kvariable_scope_session: 2,
			/**
			 * Page Scope.
			 *
			 * @property kvariable_scope_page
			 * @type Number
			 * @final
			 * @default 3
			 */
			kvariable_scope_page: 3,
			
			/**
			 * 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 variabl, or the actual tracker object.
			 * @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
			 */
			track_event: function(pTrackerObject, pEventCategory, pEventAction, pEventLabel, pEventValue, pNonInteraction) {
				if(pEventCategory && pEventAction && (pEventCategory != "") && (pEventAction != "")) {
					var vTracker = null;
					if(typeof(pTrackerObject) == "string") {
						try{
							vTracker = eval(pTrackerObject);
						} catch(e){}
					} else vTracker = pTrackerObject;
					if(typeof(pNonInteraction) == "undefined") pNonInteraction = false;
					if(!vTracker || (typeof(vTracker) == "undefined") || (typeof(vTracker._trackEvent) != "function")) MUI.util.dom.wait_object(pTrackerObject, MUI.ga.track_event, [pEventCategory, pEventAction, MUI.util.string.non_null_string(pEventLabel), pEventValue, pNonInteraction]);
					else {
						var vCategory = MUI.util.string.form_inflate(pEventCategory);
						var vAction = MUI.util.string.form_inflate(pEventAction);
						var vLabel = MUI.util.string.form_inflate(MUI.util.string.non_null_string(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 MUI.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 MUI.ga.track_event_attached_to_object(this);</code>
			 *
			 * @method attach_tracking_info
			 *
			 * @param {object} pObject the object to attach the event data to.
			 * @param {string|object} pTrackerObject the name of the tracker object variabl, or the actual tracker object.
			 * @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
			 */
			attach_tracking_info: function(pObject, pTrackerObject, pEventCategory, pEventAction, pEventLabel, pEventValue, pNonInteraction) {
				if(pObject) {
					pObject.ga_tracker = pTrackerObject;
					pObject.ga_action = MUI.util.string.non_null_string(pEventAction);
					pObject.ga_label = MUI.util.string.non_null_string(pEventLabel);
					pObject.ga_category = MUI.util.string.non_null_string(pEventCategory);
					pObject.ga_value = pEventValue;
					if(typeof(pNonInteraction) == "undefined") pNonInteraction = false;
					pObject.ga_non_interaction = pNonInteraction;
				}
			},
			/**
			 * Track an event using the data previously saved on the target object.
			 *
			 * @method track_event_attached_to_object
			 *
			 * @param {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
			 */
			track_event_attached_to_object: function(pObject) {
				if(pObject) MUI.ga.track_event(pObject.ga_tracker, MUI.util.string.non_null_string(pObject.ga_category), MUI.util.string.non_null_string(pObject.ga_action), MUI.util.string.non_null_string(pObject.ga_label), pObject.ga_value, pObject.ga_non_interaction);
				return true;
			},
			/**
			 * Queues custom variable values so they can be used when the tracker object is finally created.
			 *
			 * @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: MUI.ga.kvariable_scope_visitor, MUI.ga.kvariable_scope_session or MUI.ga.kvariable_scope_page
			 *
			 * @static
			 */
			queue_custom_variable: function(pSlot, pName, pValue, pScope) {
				var vID = pScope + "-" + pSlot;
				pName = pName.replace(/\s+/g, "_");
				if((typeof(MUI.ga._var_queue[vID]) != "undefined") && (MUI.ga._var_queue[vID].name != pName)) MUI.util.report_error("ga_var_conflict", "GA Custom Variable Conflict: {slot=" + pSlot+", scope=" + pScope + ", name=" + pName + "} vs {slot=" + MUI.ga._var_queue[vID].slot + ", scope=" + MUI.ga._var_queue[vID].scope + ", name=" + MUI.ga._var_queue[vID].name + "}");
				else MUI.ga._var_queue[vID] = {slot: pSlot, name: pName, value: pValue.replace(/\s+/g, "_"), scope: pScope};
			},
			/**
			 * Adds the previously queued variables to the tracker object.
			 *
			 * @method append_queued_variables
			 *
			 * @param {object} pTrackerObject the tracker object.
			 *
			 * @static
			 */
			append_queued_variables: function(pTrackerObject) {
				for(var vID in MUI.ga._var_queue) {
					var vInfo = MUI.ga._var_queue[vID];
					pTrackerObject._setCustomVar(vInfo.slot, vInfo.name, vInfo.value, vInfo.scope);
				}
			}
		};
	}
}

Copyright © 2017 Yahoo! Inc. All rights reserved.