/**
* @class
* @classdesc Plugin used to manage identified visitors
* @name IdentifiedVisitor
* @memberOf ATInternet.Tracker.Plugins
* @type {function}
* @param parent {object} Instance of the Tag used
* @public
*/
window['ATInternet']['Tracker']['Plugins']['IdentifiedVisitor'] = function (parent) {
"use strict";
var self = this;
// Global configuration, storage and query values
var _storage_textual = null;
var _storage_numeric = null;
var _storage_category = null;
var _storage_vrn = null;
var _query_textual = '';
var _query_numeric = '';
var _storage_redirect_textual = null;
var _storage_redirect_numeric = null;
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('IdentifiedVisitor', dfltPluginCfg || {}, function (newConf) {
conf = newConf;
});
parent.setConfig('redirectionLifetime', dfltGlobalCfg.redirectionLifetime, true);
/**
* Check if current page is a redirection one
* @memberof ATInternet.Tracker.Plugins.IdentifiedVisitor#
* @function
* @return {boolean}
* @private
*/
var _isRedirection = function () {
return !!parent.getConfig('redirect');
};
/**
* Define domain attribution for the numsite
* @memberof ATInternet.Tracker.Plugins.IdentifiedVisitor#
* @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.IdentifiedVisitor#
* @function
* @param plugin {string} Plugin to use
* @param method {string} Method to call
* @param params {Array} Arguments to give
* @return {*}
* @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.IdentifiedVisitor#
* @function
* @param method {string} Method to call
* @param params {Array} Arguments to give
* @return {*}
* @private
*/
var _pluginUtils = function (method, params) {
return _pluginCall('Utils', method, params);
};
/**
* Helper to use the plugin Storage
* @memberof ATInternet.Tracker.Plugins.IdentifiedVisitor#
* @function
* @param method {string} Method to call
* @param params {Array} Arguments to give
* @return {*}
* @private
*/
var _pluginStorage = function (method, params) {
var ret = null;
parent['plugins']['exec']('Storage', method, params, function (data) {
ret = data;
});
return ret;
};
/**
* Get identified visitor values in the query string
* @memberof ATInternet.Tracker.Plugins.IdentifiedVisitor#
* @function
* @private
*/
var _getQueryValues = function () {
var href = _pluginUtils('getLocation', []);
_query_textual = _pluginUtils('getQueryStringValue', ['xtat', href]);
_query_numeric = _pluginUtils('getQueryStringValue', ['xtan', href]);
};
/**
* Get identified visitor values in the storage from a redirection
* @memberof ATInternet.Tracker.Plugins.IdentifiedVisitor#
* @function
* @private
*/
var _getStorageRedirectValues = function () {
_storage_redirect_textual = _pluginStorage(_get_storage_method, [['atredir', 'at']]);
_storage_redirect_numeric = _pluginStorage(_get_storage_method, [['atredir', 'an']]);
// Burn after reading
_pluginStorage('del', [['atredir', 'at']]);
_pluginStorage('del', [['atredir', 'an']]);
};
/**
* Check if a stored data exists with 'object' type, if not, method creates data to store and returns true
* If exists but with another type, method returns false
* @memberof ATInternet.Tracker.Plugins.IdentifiedVisitor#
* @function
* @param name {string} Name of the stored data
* @param options {object} Options of the data to store in case of creation
* @return boolean {boolean} Returns true if the stored data already exists or if has been created, false if any problem
* @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 identified visitor values in storage for redirection
* @memberof ATInternet.Tracker.Plugins.IdentifiedVisitor#
* @function
* @private
*/
var _setStorageRedirectValues = function () {
if ((_query_textual || _query_numeric) && _checkStorage('atredir', {
path: '/',
end: parent.getConfig('redirectionLifetime')
})) {
if (_query_numeric) {
_pluginStorage(_set_storage_method, [
['atredir', 'an'],
_query_numeric
]);
}
if (_query_textual) {
_pluginStorage(_set_storage_method, [
['atredir', 'at'],
_query_textual
]);
}
}
};
/**
* get identified visitor and new visitor (vrn) values from the persistent storage
* @memberof ATInternet.Tracker.Plugins.IdentifiedVisitor#
* @function
* @private
*/
var _getStorageValues = function () {
_storage_textual = _pluginStorage(_get_storage_method, [
['atidvisitor', 'at']
]);
_storage_numeric = _pluginStorage(_get_storage_method, [
['atidvisitor', 'an']
]);
_storage_category = _pluginStorage(_get_storage_method, [
['atidvisitor', 'ac']
]);
_storage_vrn = _pluginStorage(_get_storage_method, [
['atidvisitor', 'vrn']
]);
};
/**
* Check/create/write a property in the storage session (create it if necessary)
* @memberof ATInternet.Tracker.Plugins.IdentifiedVisitor#
* @function
* @param key {string} Property name
* @param value {string} Value for the property
* @private
*/
var _setStorage = function (key, value) {
if (_checkStorage('atidvisitor', {path: '/', session: conf.lifetime * 24 * 60 * 60})) {
_pluginStorage(_set_storage_method, [
['atidvisitor', key],
value
]);
}
};
/**
* Add the key/value to the next hits, and check/create the session storage to update its property
* @memberof ATInternet.Tracker.Plugins.IdentifiedVisitor#
* @function
* @param key {string} Property to set
* @param value {string} Value to set for the property
* @private
*/
var _set = function (key, value) {
parent.setParam(key, value, {'hitType': ['all'], permanent: true});
_setStorage(key, value);
};
/**
* Set the identified visitor in buffer and storage
* @memberof ATInternet.Tracker.Plugins.IdentifiedVisitor#
* @function
* @private
*/
var _setParams = function () {
//Function used to build object from string value
var _processValue = function (obj, str) {
if (/-/.test(str)) {
obj.category = str.split('-')[0];
obj.id = str.split('-')[1];
}
else {
obj.id = str;
}
};
//Numeric identifier processing
var numObj = {category: '', id: ''};
var numStr = _query_numeric || _storage_redirect_numeric;
_processValue(numObj, numStr);
//Textual identifier processing
var textObj = {category: '', id: ''};
var textStr = _query_textual || _storage_redirect_textual;
_processValue(textObj, textStr);
//Set textual identifier
if (textObj.id) {
if (textObj.category) {
_set('ac', textObj.category);
}
_set('at', textObj.id);
}
else if (_storage_textual) {
parent.setParam('at', _storage_textual, {'hitType': ['all'], permanent: true});
if (_storage_category) {
parent.setParam('ac', _storage_category, {'hitType': ['all'], permanent: true});
}
}
//Set numeric identifier depending on priority
if (numObj.id) {
if (numObj.category) {
_set('ac', numObj.category);
}
_set('an', numObj.id);
}
else if (_storage_numeric) {
parent.setParam('anc', _storage_category + '-' + _storage_numeric, {'hitType': ['all'], permanent: true});
}
};
/**
* Add a new visitor property in the page context
* @memberof ATInternet.Tracker.Plugins.IdentifiedVisitor#
* @function
* @private
*/
var _addPageContext = function () {
var contextPageObject = parent['getContext']('page') || {};
contextPageObject.vrn = 1;
parent['setContext']('page', contextPageObject);
};
/**
* Set new visitor (vrn) in storage and page context
* @memberof ATInternet.Tracker.Plugins.IdentifiedVisitor#
* @function
* @private
*/
var _setVrnValue = function () {
var regVal = '-' + parent.getConfig('site') + '-';
var regExp = new RegExp(regVal);
if (!regExp.test(_storage_vrn)) {
_storage_vrn = (_storage_vrn || '') + regVal;
_setStorage('vrn', _storage_vrn);
_addPageContext();
}
};
/**
* Run the automatic process
* @memberof ATInternet.Tracker.Plugins.IdentifiedVisitor#
* @function
* @private
*/
var _run = function () {
_setDomainAttribution(); //commun
_getQueryValues(); //commun
if (_isRedirection()) {
_setStorageRedirectValues(); //redirect
}
else {
_getStorageRedirectValues(); //pas redirect
_getStorageValues(); //pas redirect
_setParams(); //pas redirect
_setVrnValue(); // pas redirect
}
parent.emit('IdentifiedVisitor:Ready', {
lvl: 'INFO',
details: {
storageRedirectTextual: _storage_redirect_textual,
storageRedirectNumeric: _storage_redirect_numeric,
storageTextual: _storage_textual,
storageNumeric: _storage_numeric,
storageCategory: _storage_category,
storageVrn: _storage_vrn
}
});
};
/**
* Check dependencies and launch the automatic process when all dependencies are present
* @memberof ATInternet.Tracker.Plugins.IdentifiedVisitor#
* @function
* @private
*/
var _init = function () {
var dependencies = ['Storage', 'Utils'];
parent['plugins']['waitForDependencies'](dependencies, _run);
};
// Launch process
_init();
/**
* [Object added by plugin {@link ATInternet.Tracker.Plugins.IdentifiedVisitor IdentifiedVisitor}] Tags to identify a visitor.
* @name identifiedVisitor
* @memberof ATInternet.Tracker.Tag
* @inner
* @type {object}
* @property {function} set Tag helper, see details here {@link ATInternet.Tracker.Tag#identifiedVisitor.set}
* @property {function} unset Tag helper, see details here {@link ATInternet.Tracker.Tag#identifiedVisitor.unset}
* @public
*/
parent['identifiedVisitor'] = {};
/**
* [Helper added by plugin {@link ATInternet.Tracker.Plugins.IdentifiedVisitor IdentifiedVisitor}] Set an identified visitor
* @alias identifiedVisitor.set
* @memberof! ATInternet.Tracker.Tag#
* @function
* @param tagObject {object} 2 properties : id, category
* @example
* <pre><code class="javascript">
* tag.identifiedVisitor.set({
* id:'id123', // or a numeric value, id:123456
* category:'123'
* });
* </code></pre>
* @public
*/
parent['identifiedVisitor']['set'] = self['set'] = function (tagObject) {
var identifier = tagObject['id'], category = tagObject['category'];
if (typeof identifier === 'number') {
_set('an', identifier.toString());
}
else {
_set('at', identifier);
}
if (typeof category !== 'undefined') {
_set('ac', category);
}
/* @if debug */
parent.debug('IdentifiedVisitor:identifiedVisitor:set', 'DEBUG', 'method ended', tagObject);
/* @endif */
};
/**
* [Helper added by plugin {@link ATInternet.Tracker.Plugins.IdentifiedVisitor IdentifiedVisitor}] Unset an identified visitor (delete all stored data and parameters from buffer)
* @alias identifiedVisitor.unset
* @memberof! ATInternet.Tracker.Tag#
* @function
* @public
*/
parent['identifiedVisitor']['unset'] = self['unset'] = function(){
var delParams = function(tab, str) {
for (var i = 0; i < tab.length; i++) {
parent['exec']('Storage', 'del', [['atidvisitor', tab[i]]]);
parent['delParam'](tab[i]);
}
parent['delParam'](str);
};
delParams(['an','at','ac'],'anc');
/* @if debug */
parent.debug('IdentifiedVisitor:identifiedVisitor:unset', 'DEBUG', 'method ended');
/* @endif */
};
// For unit tests on private elements !!!
/* @if test */
self._IDENTIFIEDVISITOR_LIFETIME = conf.lifetime;
self.getAllParams = function () {
self._storage_textual = _storage_textual;
self._storage_numeric = _storage_numeric;
self._storage_category = _storage_category;
self._storage_vrn = _storage_vrn;
self._query_textual = _query_textual;
self._query_numeric = _query_numeric;
self._storage_redirect_textual = _storage_redirect_textual;
self._storage_redirect_numeric = _storage_redirect_numeric;
};
self._set_storage_method = _set_storage_method;
self._get_storage_method = _get_storage_method;
self._setDomainAttribution = _setDomainAttribution;
self._pluginCall = _pluginCall;
self._pluginUtils = _pluginUtils;
self._pluginStorage = _pluginStorage;
self._getQueryValues = _getQueryValues;
self._getStorageRedirectValues = _getStorageRedirectValues;
self._setStorageRedirectValues = _setStorageRedirectValues;
self._getStorageValues = _getStorageValues;
self._checkStorage = _checkStorage;
self._run = _run;
self._setParams = _setParams;
self._setVrnValue = _setVrnValue;
self._addPageContext = _addPageContext;
self._init = _init;
self._set = _set;
self._setStorage = _setStorage;
/* @endif */
};
window['ATInternet']['Tracker']['addPlugin']('IdentifiedVisitor');