/**
* @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 _storage_redirect_ref = null;
var _context_ref;
var _set_storage_method = '';
var _get_storage_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_storage_method = 'set' + (conf.domainAttribution ? '' : 'Private');
_get_storage_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 Storage
* @memberOf ATInternet.Tracker.Plugins.ContextVariables#
* @function
* @param method {string}
* @param params {Array}
* @returns {*}
* @private
*/
var _pluginStorage = function (method, params) {
var ret = null;
parent['plugins']['exec']('Storage', 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 stored data on redirection
* @memberOf ATInternet.Tracker.Plugins.ContextVariables#
* @function
* @private
*/
var _getStorageRedirectValues = function () {
_storage_redirect_ref = _pluginStorage(_get_storage_method, [['atredir', 'ref']]);
// Burn after reading
_pluginStorage('del', [['atredir', 'ref']]);
};
/**
* Check if a stored data exists with 'object' type, if not, method creates a data to store and returns true
* If the stored data exists but with another type, method returns false
* @memberOf ATInternet.Tracker.Plugins.ContextVariables#
* @function
* @param name {string} Name of the stored data you need to check
* @param options {object} Options of the data to store in case of creation
* @returns {bool}
* @private
*/
var _checkStorage = function (name, options) {
var temp = _pluginStorage(_get_storage_method, [name]);
if (temp !== null) {
return (typeof temp === 'object' && !(temp instanceof Array));
}
else {
_pluginStorage(_set_storage_method, [name, {}, options]);
return true;
}
};
/**
* Set redirect values
* @memberOf ATInternet.Tracker.Plugins.ContextVariables#
* @function
* @private
*/
var _setStorageRedirectValues = function () {
var obj = _pluginUtils('getDocumentLevel', []);
var ref;
if (_context_ref) {
ref = _context_ref;
}
else if (_query_ref !== null) {
ref = _query_ref;
}
else if (obj) {
ref = obj.referrer;
}
else {
ref = 'acc_dir';
}
if (ref && _checkStorage('atredir', {
path: '/',
end: parent.getConfig('redirectionLifetime')
})) {
_pluginStorage(_set_storage_method, [
['atredir', 'ref'],
ref
]);
}
};
/**
* 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 () {
if (window.navigator) {
parent.setParam('lng', (window.navigator.language || window.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 () {
if (window.navigator) {
parent.setParam('jv', (window.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 (_storage_redirect_ref === 'acc_dir') {
referrer = ''
}
else if (_storage_redirect_ref) {
referrer = _storage_redirect_ref;
}
else if (doc) {
referrer = doc.referrer;
}
if (referrer) {
referrer = referrer.replace(/[<>]/g, "").substring(0, 1600).replace(/&/g, '$');
}
return referrer;
};
/**
* 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()) {
_setStorageRedirectValues();
}
else {
_getStorageRedirectValues();
_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 = ['Storage', '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._storage_redirect_ref = _storage_redirect_ref;
self._context_ref = _context_ref;
};
self['_isRedirection'] = _isRedirection;
self['_setDomainAttribution'] = _setDomainAttribution;
self['_pluginCall'] = _pluginCall;
self['_pluginUtils'] = _pluginUtils;
self['_pluginStorage'] = _pluginStorage;
self['_getQueryValues'] = _getQueryValues;
self['_getContextValues'] = _getContextValues;
self['_getStorageRedirectValues'] = _getStorageRedirectValues;
self['_setStorageRedirectValues'] = _setStorageRedirectValues;
self['_checkStorage'] = _checkStorage;
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');