Source: MvTesting/mvtesting.js

/**
 * @class
 * @classdesc Plugin to measure multivariate testing.
 * @name MvTesting
 * @memberof ATInternet.Tracker.Plugins
 * @type {function}
 * @param parent {object} Instance of the Tag used
 * @public
 */
window['ATInternet']['Tracker']['Plugins']['MvTesting'] = function (parent) {
    "use strict";

    var _numVariante = 0;
    var _debug = {
        level: 'DEBUG',
        messageEnd: 'method ended'
    };

    /**
     * Get a string value from parameter.
     * @memberof ATInternet.Tracker.Plugins.MvTesting#
     * @function
     * @param value {string} Value to stringify
     * @return {string}
     * @private

     */
    var _value2strIfExist = function (value) {
        if (value === undefined) return '';
        return value + '';
    };

    /**
     * Get a defined value from tagObject property.
     * @memberof ATInternet.Tracker.Plugins.MvTesting#
     * @function
     * @param tagObject {object} Object containing multivariate testing data
     * @param key {string} Key to test
     * @return {string}
     * @private
     */
    var _getValue = function (tagObject, key) {
        var value = '';
        if (tagObject.hasOwnProperty(key)) {
            value = _value2strIfExist(tagObject[key]);
        }
        return value;
    };

    /**
     * Build "test-waveId-creation" string.
     * @memberof ATInternet.Tracker.Plugins.MvTesting#
     * @function
     * @param tagObject {object} Object containing multivariate testing data
     * @returns {string}
     * @private
     */
    var _getTest = function (tagObject) {
        var _test = _getValue(tagObject, 'test');
        var _waveId = _getValue(tagObject, 'waveId');
        var _creation = _getValue(tagObject, 'creation');
        return (_test + '-' + _waveId + '-' + _creation);
    };

    /**
     * Get "abmvc" value and set parameter.
     * @memberof ATInternet.Tracker.Plugins.MvTesting#
     * @function
     * @param tagObject {object} Object containing multivariate testing data
     * @private
     */
    var _processTest = function (tagObject) {
        var _abmvc = _getTest(tagObject);
        parent.setParam('abmvc', _abmvc, {hitType: ['mvtesting']});
    };

    /**
     * Build "variable-version" string.
     * @memberof ATInternet.Tracker.Plugins.MvTesting#
     * @function
     * @param tagObject {object} Object containing multivariate testing data
     * @return {string}
     * @private
     */
    var _getVariante = function (tagObject) {
        var _variable = _getValue(tagObject, 'variable');
        var _version = _getValue(tagObject, 'version');
        return (_variable + '-' + _version);
    };

    /**
     * Get "abmv" value and set parameter.
     * @memberof ATInternet.Tracker.Plugins.MvTesting#
     * @function
     * @param tagObject {object} Object containing multivariate testing data
     * @private
     */
    var _processVariante = function (tagObject) {
        var _abmv = _getVariante(tagObject);
        _numVariante++;
        parent.setParam('abmv' + _numVariante, _abmv, {hitType: ['mvtesting']});
    };

    /**
     * Test if tagObject is an object.
     * @memberof ATInternet.Tracker.Plugins.MvTesting#
     * @function
     * @param tagObject {object} Object containing multivariate testing data
     * @return {boolean}
     * @private
     */
    var _isObject = function (tagObject) {
        return ((typeof tagObject === 'object') && !(tagObject instanceof Array));
    };

    /**
     * Build "p" label with chapters from page context.
     * @memberof ATInternet.Tracker.Plugins.MvTesting#
     * @function
     * @param tagObject {object} Page object containing properties to use
     * @return {string}
     * @private
     */
    var _getFullName = function (tagObject) {
        return parent.utils.manageChapters(tagObject, 'chapter', 3) + (tagObject['name'] ? tagObject['name'] : '');
    };

    /**
     * Send MvTesting data.
     * @memberof ATInternet.Tracker.Plugins.MvTesting#
     * @function
     * @param callback {function} Callback to execute
     * @private
     */
    var _sendDataMvTesting = function (callback) {
        var contextPageObject = parent['getContext']('page') || {};
        parent.setParam('p', _getFullName(contextPageObject), {hitType: ['mvtesting']});
        parent.setParam('s2', contextPageObject['level2'] || '', {hitType: ['mvtesting']});
        parent.setParam('type', 'mvt', {hitType: ['mvtesting']});
        parent.sendHit(null, [['hitType', ['mvtesting']]], callback, null, null);
    };

    /**
     * [Object added by plugin {@link ATInternet.Tracker.Plugins.MvTesting MvTesting}] Tags to manage multivariate testing data.
     * @name mvTesting
     * @memberof ATInternet.Tracker.Tag
     * @inner
     * @type {object}
     * @property {function} set Tag helper, see details here {@link ATInternet.Tracker.Tag#mvTesting.set}
     * @property {function} add Tag helper, see details here {@link ATInternet.Tracker.Tag#mvTesting.add}
     * @public
     */
    parent['mvTesting'] = {};

    /**
     * [Helper added by plugin {@link ATInternet.Tracker.Plugins.MvTesting MvTesting}] Set multivariate testing default properties.
     * @alias mvTesting.set
     * @memberof! ATInternet.Tracker.Tag#
     * @function
     * @param tagObject {object} 3 properties : test, waveId, creation
     * @example
     *  <pre><code class="javascript">tag.mvTesting.set({
     *      test: '1[My_first_Test]',
     *      waveId: 1,
     *      creation: '2[Version2_page_subscription]'
     *  });
     *  </code></pre>
     * @public
     */
    parent['mvTesting']['set'] = function (tagObject) {
        if (_isObject(tagObject) && !ATInternet.Utils.isEmptyObject(tagObject)) {
            parent.dispatchSubscribe('mvTesting'); // Register helper to final call to send hit
            _processTest(tagObject);
        }
        /* @if debug */
        parent.debug('MvTesting:mvTesting:set', _debug.level, _debug.messageEnd, tagObject);
        /* @endif */
    };

    /**
     * [Helper added by plugin {@link ATInternet.Tracker.Plugins.MvTesting MvTesting}] Add a new version for a variable.
     * @alias mvTesting.add
     * @memberof! ATInternet.Tracker.Tag#
     * @function
     * @param tagObject {object} 2 properties : variable, version
     * @example
     *  <pre><code class="javascript">tag.mvTesting.add({
     *      variable: '1[Header]',
     *      version: '2[Grey]'
     *  });
     *  </code></pre>
     * @public
     */
    parent['mvTesting']['add'] = function (tagObject) {
        if (_isObject(tagObject) && !ATInternet.Utils.isEmptyObject(tagObject)) {
            parent.dispatchSubscribe('mvTesting'); // Register helper to final call to send hit
            _processVariante(tagObject);
        }
        /* @if debug */
        parent.debug('MvTesting:mvTesting:add', _debug.level, _debug.messageEnd, tagObject);
        /* @endif */
    };

    /**
     * [Helper added by plugin {@link ATInternet.Tracker.Plugins.MvTesting MvTesting}] Will be called by tracker.dispatch if any mvTesting has been set.
     * @alias mvTesting.onDispatch
     * @memberof! ATInternet.Tracker.Tag#
     * @function
     * @param callback {function} Callback to execute
     * @private
     */
    parent['mvTesting']['onDispatch'] = function (callback) {
        _sendDataMvTesting(callback);
    };

    // For unit tests on private elements !!!
    /* @if test */
    var _this = this;
    _this['_processTest'] = _processTest;
    _this['_processVariante'] = _processVariante;
    _this['_getTest'] = _getTest;
    _this['_getVariante'] = _getVariante;
    _this['_getValue'] = _getValue;
    _this['_value2strIfExist'] = _value2strIfExist;
    _this['_getFullName'] = _getFullName;
    _this['_sendDataMvTesting'] = _sendDataMvTesting;
    /* @endif */
};
window['ATInternet']['Tracker']['addPlugin']('MvTesting');