/**
* @class
* @classdesc Plugin to collect marketing data from Weborama Partner.
* @name Weborama
* @memberof ATInternet.Tracker.Plugins
* @type {function}
* @param parent {object} Instance of the Tag used
* @public
*/
window['ATInternet']['Tracker']['Plugins']['Weborama'] = function (parent) {
'use strict';
var _this = this;
var _config = {};
var _customWeboObject = {};
var _set_cookie_method = '';
var _get_cookie_method = '';
var _cookieVisitValue = null;
var _timeout = false;
var _done = false;
/**
* Init Weborama configuration.
* @memberof ATInternet.Tracker.Plugins.Weborama#
* @function
* @private
*/
var _initConfig = function () {
// Set visitLifetime value from global configuration.
parent.setConfig('visitLifetime', dfltGlobalCfg.visitLifetime, true);
// Set specific plugin configuration.
// If global configuration already exists, set only undefined properties.
parent.configPlugin('Weborama', dfltPluginCfg || {});
// The last instance of tracker which set a weborama will be the one called by weborama API
var uuid = ATInternet.Utils.uuid();
var id = parseInt(uuid.num(8));
parent.configPlugin('Weborama', {id:id}, function (newConf) {
_config = newConf;
});
window.ATInternet.Weborama.tagInstances.push(parent);
_config.callback = '(function(weboObject){window.ATInternet.Weborama.callback(weboObject,'+id+')})';
_config.url = ((document.location.protocol === 'https:') ? 'https://': 'http://') + _config.baseUrl;
};
/**
* Init Weborama info object.
* @memberof ATInternet.Tracker.Plugins.Weborama#
* @function
* @private
*/
var _initInfoWebo = function () {
// Create final Weborama object
_customWeboObject.info = {
version: _config.version,
accountid: _config.accountId,
message: '',
errors: []
};
};
/**
* Define labels to use for unique persistence.
* @memberof ATInternet.Tracker.Plugins.Weborama#
* @function
* @private
*/
var _defineUniquePersistence = function () {
_set_cookie_method = 'set' + (_config.domainAttribution ? '' : 'Private');
_get_cookie_method = 'get' + (_config.domainAttribution ? '' : 'Private');
};
/**
* Get persistent and direct spot values from cookies.
* @memberof ATInternet.Tracker.Plugins.Weborama#
* @function
* @private
*/
var _getCookieValues = function () {
_cookieVisitValue = _pluginCookie(_get_cookie_method, ['atwebosession']);
};
/**
* Helper to use the plugin Cookies.
* @memberof ATInternet.Tracker.Plugins.Weborama#
* @function
* @param method {string} Method to call in the plugin
* @param params {Array} Parameters to transmit
* @returns {*}
* @private
*/
var _pluginCookie = function (method, params) {
var ret = null;
parent['plugins']['exec']('Cookies', method, params, function (data) {
ret = data;
});
return ret;
};
/**
* Run Weborama global processing.
* @memberof ATInternet.Tracker.Plugins.Weborama#
* @function
* @private
*/
var _run = function () {
parent.addSpecificDispatchEventFor('weborama');
var callback = function (error) {
if (!_timeout) {
if (error !== null) {
_setScriptError();
}
_setDataWebo();
_setPageContext();
}
};
ATInternet.Utils.loadScript({url: _config.url + '&a.si=' + _config.accountId + '&a.cb=' + _config.callback + '&rn=' + Math.random()}, callback);
var errorTimeout = function () {
if (!_done) {
_setTimeoutError();
_setDataWebo();
_setPageContext();
}
};
setTimeout(errorTimeout, 500);
};
/**
* Set Weborama script error.
* @memberof ATInternet.Tracker.Plugins.Weborama#
* @function
* @private
*/
var _setScriptError = function () {
_customWeboObject.info.errors.push('noScript');
};
/**
* Process Weborama cookie visit.
* @memberof ATInternet.Tracker.Plugins.Weborama#
* @function
* @private
*/
var _processCookieVisitValue = function () {
var index = ATInternet.Utils.arrayIndexOf(_cookieVisitValue.info.errors, 'timeout');
if (_cookieVisitValue.info.message.length > 0 && index != -1) {
_cookieVisitValue.info.errors.splice(index, 1);
_customWeboObject.info = _cookieVisitValue.info;
_cookieVisitValue = null;
}
else {
_customWeboObject.info.message = 'sessionAlreadyActive';
}
};
/**
* Set Weborama data.
* @memberof ATInternet.Tracker.Plugins.Weborama#
* @function
* @private
*/
var _setDataWebo = function () {
// Set session cookie for the visit.
var options = {
path: _config.path,
session: parent.getConfig('visitLifetime') * 60
};
if (_checkCookie('atwebosession', options, _set_cookie_method, _get_cookie_method)) {
if (_cookieVisitValue === null) {
_pluginCookie(_set_cookie_method, ['atwebosession', _customWeboObject, options]);
}
}
// Update session cookie on trigger 'Tracker:Hit:Sent:Ok'.
parent.onTrigger('Tracker:Hit:Sent:Ok', function () {
_pluginCookie(_get_cookie_method, ['atwebosession']);
});
// Add cookie error message if attvtsession cookie is null.
if (_pluginCookie(_get_cookie_method, ['atwebosession', true]) === null) {
_customWeboObject.info.errors.push('cookieError');
}
};
/**
* Check if a cookie is present and with type object, if not it creates it and returns true,
* if present but with another type it returns false.
* @memberof ATInternet.Tracker.Plugins.Weborama#
* @function
* @param name {string} Name of the cookie you need to check
* @param options {object} Options of the cookie to create
* @return {boolean}
* @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;
}
};
/**
* Set Page context.
* @memberof ATInternet.Tracker.Plugins.Weborama#
* @function
* @private
*/
var _setPageContext = function () {
var contextPageObject = parent.getContext('page') || {};
var contextCustomObject = contextPageObject['customObject'] || {};
contextCustomObject.weborama = _customWeboObject;
contextPageObject.customObject = contextCustomObject;
// Set new custom object in page context
parent.setContext('page', contextPageObject);
parent.processSpecificDispatchEventFor('weborama');
_done = true;
};
/**
* Set Weborama timeout error.
* @memberof ATInternet.Tracker.Plugins.Weborama#
* @function
* @private
*/
var _setTimeoutError = function () {
_customWeboObject.info.errors.push('timeout');
_timeout = true;
};
/**
* [Object added by plugin {@link ATInternet.Tracker.Plugins.Weborama Weborama}] Tags to collect marketing data from Weborama Partner.
* @name weborama
* @memberof ATInternet.Tracker.Tag
* @inner
* @type {object}
* @property {function} set Tag helper, see details here {@link ATInternet.Tracker.Tag#weborama.set}
* @property {function} callback Tag helper, see details here {@link ATInternet.Tracker.Tag#weborama.callback}
* @public
*/
parent['weborama'] = {};
/**
* [Helper added by plugin {@link ATInternet.Tracker.Plugins.Weborama Weborama}] Set Weborama parameters.
* @alias weborama.set
* @memberof! ATInternet.Tracker.Tag#
* @function
* @param name {string} Name of the Tag reference variable
* @example
* <pre><code class="javascript">tag.weborama.set();
* </code></pre>
* @public
*/
parent['weborama']['set'] = _this['set'] = function () {
// Register helper to final call in order to send messages.
parent.dispatchSubscribe('weborama');
_initConfig();
_initInfoWebo();
/* @if test */
var acceptCookie = false;
/* @endif */
if (!parent.getConfig('disableCookie')) {
/* @if test */
acceptCookie = true;
/* @endif */
_defineUniquePersistence();
_getCookieValues();
if (!_cookieVisitValue) {
if (_config.baseUrl && typeof _config.baseUrl === 'string') {
_run();
}
else {
_customWeboObject.info.message = 'noURLSet';
_setDataWebo();
_setPageContext();
}
}
else {
_processCookieVisitValue();
_setDataWebo();
_setPageContext();
}
}
else {
_customWeboObject.info.message = 'disableCookie:true';
_setPageContext();
}
/* @if debug */
parent.debug('Weborama:weborama:set', 'DEBUG', 'method ended', _customWeboObject);
/* @endif */
/* @if test */
return acceptCookie;
/* @endif */
};
/**
* [Helper added by plugin {@link ATInternet.Tracker.Plugins.Weborama Weborama}] Callback (called by Partner).
* @alias weborama.callback
* @memberof! ATInternet.Tracker.Tag#
* @function
* @example
* <pre>code class="javascript">tag.weborama.callback();
* </code></pre>
* @public
*/
parent['weborama']['callback'] = _this['callback'] = function (response) {
if (typeof response !== 'undefined') {
if (response.last_action) {
_customWeboObject.info.message = response.last_action;
}
else {
_customWeboObject.info.message = 'noAction';
}
if (response.webo_cid) {
_customWeboObject.info.userid = response.webo_cid;
}
}
else {
_customWeboObject.info.errors.push('noData');
}
if (_timeout) {
_cookieVisitValue = null;
_setDataWebo();
}
};
/**
* [Helper added by plugin {@link ATInternet.Tracker.Plugins.Weborama Weborama}] Will be called by tracker.dispatch if any Weborama has been set.
* @alias weborama.onDispatch
* @memberof! ATInternet.Tracker.Tag#
* @function
* @private
*/
parent['weborama']['onDispatch'] = _this['onDispatch'] = function () {
if (!parent.dispatchSubscribed('page')) {
/* @if debug */
parent.debug('Weborama:weborama:onDispatch:Error', 'WARNING', 'Weborama data are not sent without page information (use page.set())');
/* @endif */
}
};
// For unit tests on private elements !!!
/* @if test */
_this._initConfig = _initConfig;
_this._initInfoWebo = _initInfoWebo;
_this._defineUniquePersistence = _defineUniquePersistence;
_this._getCookieValues = _getCookieValues;
_this._pluginCookie = _pluginCookie;
_this._run = _run;
_this._setScriptError = _setScriptError;
_this._setDataWebo = _setDataWebo;
_this._checkCookie = _checkCookie;
_this._setPageContext = _setPageContext;
_this._setTimeoutError = _setTimeoutError;
_this.getCookieMethods = function () {
_this._set_cookie_method = _set_cookie_method;
_this._get_cookie_method = _get_cookie_method;
};
/* @endif */
};
window.ATInternet.Weborama = {
tagInstances:[],
callback:function(response,id){
var tagInstances = window.ATInternet.Weborama.tagInstances;
for(var i=0;i<tagInstances.length;i++){
if(tagInstances[i].getConfig('Weborama').id === id){
tagInstances[i]['weborama']['callback'](response);
}
}
}
};
window['ATInternet']['Tracker']['addPlugin']('Weborama');