Source: ContextVariables/contextvariables.js

/**
 * @class
 * @classdesc Plugin which manages context variables like the screen resolution or the referrer
 * @name ContextVariables
 * @memberOf ATInternet.Tracker.Plugins
 * @type {function}
 * @param tag {object} Instance of the Tag used
 * @public
 */
ATInternet.Tracker.Plugins.ContextVariables = function (tag) {

    '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
    tag.configPlugin('ContextVariables', dfltPluginCfg || {}, function (newConf) {
        conf = newConf;
    });
    tag.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 !!tag.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 plugin Storage
     * @memberOf ATInternet.Tracker.Plugins.ContextVariables#
     * @function
     * @param method {string}
     * @param params {Array}
     * @returns {*}
     * @private
     */
    var _pluginStorage = function (method, params) {
        var ret = null;
        tag.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 = tag.getContext('forcedReferer');
    };

    /**
     * Get values from the query string
     * @memberOf ATInternet.Tracker.Plugins.ContextVariables#
     * @function
     * @private
     */
    var _getQueryValues = function () {
        var href = tag.utils.getLocation();
        _query_ref = tag.utils.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 = tag.utils.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: tag.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 () {
        tag.setParam('vtag', tag.version, {permanent: true, hitType: ['all']});
    };

    /**
     * Add the current platform to hits
     * @memberOf ATInternet.Tracker.Plugins.ContextVariables#
     * @function
     * @private
     */
    var _getPlatform = function () {
        tag.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 */
            tag.debug('ContextVariables:ScreenResolution', 'ERROR', 'Screen resolution exception', {except: c});
            /* @endif */
        }
        tag.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;
        }
        tag.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) {
            tag.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);
        tag.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) {
            tag.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 () {
        tag.setParam('hl', function () {
            var date = new Date;
            return date.getHours() + 'x' + date.getMinutes() + 'x' + date.getSeconds();
        }, {
            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;
        } else {
            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 = tag.utils.getDocumentLevel();
        tag.setParam('ref', _getReferrerValue(obj), {
            permanent: true,
            last: true,
            hitType: ['page', 'ecommerce', 'avinsights', 'events']
        });
    };

    /**
     * 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();
        }
        tag.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'];
        tag['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._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 */
};

ATInternet.Tracker.addPlugin('ContextVariables');