/**
* @class
* @classdesc Plugin which manages context variables like the screen resolution or the referrer
* @name ContextVariables
* @memberOf ATInternet.Tracker.Plugins
* @type {function}
* @param parent {object} Instance of the Tag used
* @public
*/
window['ATInternet']['Tracker']['Plugins']['ContextVariables'] = function (parent) {
"use strict";
var _query_ref = '';
var _cookie_redirect_ref = null;
var _context_ref;
var _set_cookie_method = '';
var _get_cookie_method = '';
var conf = {};
// Set specific plugin configuration, if global configuration already exists, set only undefined properties
parent.configPlugin('ContextVariables', dfltPluginCfg || {}, function (newConf) {
conf = newConf;
});
parent.setConfig('redirectionLifetime', dfltGlobalCfg.redirectionLifetime, true);
/**
* Check if the current page is considered as a redirection from customer configuration
* @memberOf ATInternet.Tracker.Plugins.ContextVariables#
* @private
*/
var _isRedirection = function () {
return !!parent.getConfig('redirect');
};
/**
* Define domain attribution
* @memberOf ATInternet.Tracker.Plugins.ContextVariables#
* @function
* @private
*/
var _setDomainAttribution = function () {
_set_cookie_method = 'set' + (conf.domainAttribution ? '' : 'Private');
_get_cookie_method = 'get' + (conf.domainAttribution ? '' : 'Private');
};
/**
* Helper to use the exec method of the plugin manager
* @memberOf ATInternet.Tracker.Plugins.ContextVariables#
* @function
* @param plugin {string}
* @param method {string}
* @param params {Array}
* @returns {*}
* @private
*/
var _pluginCall = function (plugin, method, params) {
var ret = null;
parent['plugins']['exec'](plugin, method, params, function (data) {
ret = data;
});
return ret;
};
/**
* Helper to use the plugin Utils
* @memberOf ATInternet.Tracker.Plugins.ContextVariables#
* @function
* @param method {string}
* @param params {Array}
* @returns {*}
* @private
*/
var _pluginUtils = function (method, params) {
return _pluginCall('Utils', method, params);
};
/**
* Helper to use the plugin Cookies
* @memberOf ATInternet.Tracker.Plugins.ContextVariables#
* @function
* @param method {string}
* @param params {Array}
* @returns {*}
* @private
*/
var _pluginCookie = function (method, params) {
var ret = null;
parent['plugins']['exec']('Cookies', method, params, function (data) {
ret = data;
});
return ret;
};
/**
* Get values from context
* @memberOf ATInternet.Tracker.Plugins.ContextVariables#
* @function
* @private
*/
var _getContextValues = function () {
_context_ref = parent.getContext('forcedReferer');
};
/**
* Get values from the query string
* @memberOf ATInternet.Tracker.Plugins.ContextVariables#
* @function
* @private
*/
var _getQueryValues = function () {
var href = _pluginUtils('getLocation', []);
_query_ref = _pluginUtils('getQueryStringValue', ['xtref', href]);
if (_query_ref === undefined) {
_query_ref = '';
}
};
/**
* Get values from redirection's cookie
* @memberOf ATInternet.Tracker.Plugins.ContextVariables#
* @function
* @private
*/
var _getCookieRedirectValues = function () {
_cookie_redirect_ref = _pluginCookie(_get_cookie_method, [['atredir', 'ref']]);
// Burn after reading
_pluginCookie('del', [['atredir', 'ref']]);
};
/**
* Set cookie redirect values
* @memberOf ATInternet.Tracker.Plugins.ContextVariables#
* @function
* @private
*/
var _setCookieRedirectValues = function () {
var obj = _pluginUtils('getDocumentLevel', []);
var ref = _context_ref ? _context_ref : (_query_ref != null) ? _query_ref : obj.referrer || 'acc_dir';
if (ref && _checkCookie('atredir', {
path: '/',
end: parent.getConfig('redirectionLifetime')
})) {
_pluginCookie(_set_cookie_method, [
['atredir', 'ref'],
ref
]);
}
};
/**
* Check if a cookie exists with 'object' type, if not, method creates cookie and returns true
* If the cookie exists but with another type, method returns false
* @memberOf ATInternet.Tracker.Plugins.ContextVariables#
* @function
* @param name {string} Name of the cookie you need to check
* @param options {object} Options of the cookie in the case of creation
* @returns {bool}
* @private
*/
var _checkCookie = function (name, options) {
var temp = _pluginCookie(_get_cookie_method, [name]);
if (temp !== null) {
return (typeof temp === 'object' && !(temp instanceof Array));
}
else {
_pluginCookie(_set_cookie_method, [name, {}, options]);
return true;
}
};
/**
* Add the tag version to the hit
* @memberOf ATInternet.Tracker.Plugins.ContextVariables#
* @function
* @private
*/
var _getVersion = function () {
parent.setParam('vtag', parent.version, {permanent: true, hitType: ['all']});
};
/**
* Add the current platform to hits
* @memberOf ATInternet.Tracker.Plugins.ContextVariables#
* @function
* @private
*/
var _getPlatform = function () {
parent.setParam('ptag', 'js', {permanent: true, hitType: ['all']});
};
/**
* Add the screen resolution to the hit
* @memberOf ATInternet.Tracker.Plugins.ContextVariables#
* @function
* @private
*/
var _getScreenResolution = function () {
var str = '';
try {
str += window.screen.width + "x" + window.screen.height + "x" + window.screen.pixelDepth + "x" + window.screen.colorDepth;
} catch (c) {
/* @if debug */
parent.debug('ContextVariables:ScreenResolution', 'ERROR', 'Screen resolution exception', {except: c});
/* @endif */
}
parent.setParam('r', str, {permanent: true, hitType: ['all']});
};
/**
* Add the browser resolution to the hit
* @memberOf ATInternet.Tracker.Plugins.ContextVariables#
* @function
* @private
*/
var _getBrowserResolution = function () {
var str = '';
if (window.innerWidth) {
str += window.innerWidth + 'x' + window.innerHeight;
} else if (document.body && document.body.offsetWidth) {
str += document.body.offsetWidth + 'x' + document.body.offsetHeight;
}
parent.setParam('re', str, {permanent: true, hitType: ['all']});
};
/**
* Add the user language to the hit
* @memberOf ATInternet.Tracker.Plugins.ContextVariables#
* @function
* @private
*/
var _getUserLanguage = function () {
parent.setParam('lng', (navigator.language || navigator.userLanguage), {permanent: true, hitType: ['all']});
};
/**
* Add the page id to the hit
* @memberOf ATInternet.Tracker.Plugins.ContextVariables#
* @function
* @private
*/
var _getPageId = function () {
var uuid = ATInternet.Utils.uuid();
var str = uuid.num(13);
parent.setParam('idp', str, {permanent: true, hitType: ['page', 'clickzone']});
};
/**
* Add an indicator which tells if java is enabled ('1') or not ('0') in the hit
* @memberOf ATInternet.Tracker.Plugins.ContextVariables#
* @function
* @private
*/
var _getJavaIndicator = function () {
parent.setParam('jv', (navigator.javaEnabled() ? '1' : '0'), {hitType: ['page']});
};
/**
* Add the local hour to the hit
* @memberOf ATInternet.Tracker.Plugins.ContextVariables#
* @function
* @private
*/
var _getLocalHour = function () {
var _getLocalHour = function () {
var date = new Date;
return date.getHours() + "x" + date.getMinutes() + "x" + date.getSeconds();
};
parent.setParam('hl', _getLocalHour, {permanent: true, hitType: ['all']});
};
/**
* Get the referrer and process it with specific cases
* @memberOf ATInternet.Tracker.Plugins.ContextVariables#
* @function
* @private
*/
var _getReferrerValue = function (doc) {
var referrer = '';
if (_context_ref) {
referrer = _context_ref;
}
else if (_query_ref === 'acc_dir') {
referrer = '';
}
else if (_query_ref != null) {
referrer = _query_ref;
}
else if (_cookie_redirect_ref === 'acc_dir') {
referrer = ''
}
else if (_cookie_redirect_ref) {
referrer = _cookie_redirect_ref;
}
else {
referrer = doc.referrer;
}
return referrer.replace(/[<>]/g, "").substring(0, 1600).replace(/&/g, '$');
};
/**
* Add the referrer to the hit
* @memberOf ATInternet.Tracker.Plugins.ContextVariables#
* @function
* @private
*/
var _getReferrer = function () {
var obj = _pluginUtils('getDocumentLevel', []);
parent.setParam('ref', _getReferrerValue(obj), {permanent: true, last: true, hitType: ['page']});
};
/**
* Get all context variables and add them to concerned hitType(s)
* @memberOf ATInternet.Tracker.Plugins.ContextVariables#
* @function
* @private
*/
var _addAllContextVariables = function () {
_setDomainAttribution();
_getQueryValues();
_getContextValues();
if (_isRedirection()) {
_setCookieRedirectValues();
}
else {
_getCookieRedirectValues();
_getVersion();
_getPlatform();
_getScreenResolution();
_getBrowserResolution();
_getLocalHour();
_getUserLanguage();
_getPageId();
_getJavaIndicator();
_getReferrer();
}
parent.emit('ContextVariables:Ready', {lvl: 'INFO'});
};
/**
* Init method, check dependencies and launch the plugin when all dependencies are present
* @memberOf ATInternet.Tracker.Plugins.ContextVariables#
* @function
* @private
*/
var _init = function () {
var dependencies = ['Cookies', 'Utils'];
parent['plugins']['waitForDependencies'](dependencies, _addAllContextVariables);
};
_init();
// For unit tests on private elements !!!
/* @if test */
var self = this;
self.getAllParams = function () {
self._query_ref = _query_ref;
self._cookie_redirect_ref = _cookie_redirect_ref;
self._context_ref = _context_ref;
};
self['_isRedirection'] = _isRedirection;
self['_setDomainAttribution'] = _setDomainAttribution;
self['_pluginCall'] = _pluginCall;
self['_pluginUtils'] = _pluginUtils;
self['_pluginCookie'] = _pluginCookie;
self['_getQueryValues'] = _getQueryValues;
self['_getContextValues'] = _getContextValues;
self['_getCookieRedirectValues'] = _getCookieRedirectValues;
self['_setCookieRedirectValues'] = _setCookieRedirectValues;
self['_checkCookie'] = _checkCookie;
self['_getVersion'] = _getVersion;
self['_getPlatform'] = _getPlatform;
self['_getScreenResolution'] = _getScreenResolution;
self['_getBrowserResolution'] = _getBrowserResolution;
self['_getUserLanguage'] = _getUserLanguage;
self['_getPageId'] = _getPageId;
self['_getJavaIndicator'] = _getJavaIndicator;
self['_getLocalHour'] = _getLocalHour;
self['_getReferrerValue'] = _getReferrerValue;
self['_getReferrer'] = _getReferrer;
self['_addAllContextVariables'] = _addAllContextVariables;
self['_init'] = _init;
/* @endif */
};
window['ATInternet']['Tracker']['addPlugin']('ContextVariables');