Source: Tracker/builder.js

/**
 * @class
 * @classdesc Hits builder.
 * @name BuildManager
 * @param parent {object} Instance of the Tag used
 * @public
 */
var BuildManager = function (parent) {
    'use strict';

    var self = this;
    var MAXSIZE = 1600;
    var AVAILABLESIZE = 1600;

    /**
     * List of querystring parameters which can be truncated
     * For others parameters (ati, atc, pdtl, etc.), see truncate option
     * @type {String[]}
     * @private
     */
    var _multiHitable = ['dz'];

    /**
     * Create an element of the "preQuery" object.
     * @memberof BuildManager#
     * @function
     * @param param {string} name of a querystring parameter
     * @param value {string} value of a querystring parameter
     * @param truncate {boolean} true if the parameter can truncate
     * @param multihit {boolean} true if the parameter must be added in all multi-hits
     * @param separator {string} separator to consider for multi-hits
     * @param encode {boolean} true if if the parameter value is encoded
     * @param last {boolean} true if if the parameter must be placed at the end
     * @returns {object}
     * @private
     */
    var _makeSubQueryObject = function (param, value, truncate, multihit, separator, encode, last) {
        var p = '&' + param + '=';
        return {
            param: p,
            paramSize: p.length,
            str: value,
            strSize: value.length,
            truncate: truncate,
            multihit: multihit,
            separator: separator || '',
            encode: encode,
            last: last
        };
    };

    /**
     * Create one hit with the first elements of the given dictionary and deletes them from it.
     * @memberof BuildManager#
     * @function
     * @param subQueries {object} dictionary generated with the _preQuery method
     * @param maxSizeHit {number} size available for inserting hit parameters
     * @returns {Array}:
     *  - index 0: query string to put on a hit
     * <br />- index 1: dictionary remains unused, null if empty
     * @private
     */
    var _makeOneHit = function (subQueries, maxSizeHit) {
        var hit = '';
        var freeSpace = 0;
        var usedSpace = 0;
        var index = 0;
        var subQueryObject = null;
        var remainingParameters = null;
        for (var key in subQueries) {
            if (subQueries.hasOwnProperty(key)) {
                subQueryObject = subQueries[key];
                if (subQueryObject) {
                    freeSpace = maxSizeHit - usedSpace;
                    if (subQueryObject.last && remainingParameters !== null) {
                        remainingParameters[key] = subQueryObject;
                    }
                    else if (subQueryObject.strSize + subQueryObject.paramSize <= freeSpace) {
                        // The new parameter fits into the hit
                        hit += subQueryObject.param + subQueryObject.str;
                        usedSpace += subQueryObject.paramSize + subQueryObject.strSize;
                    }
                    else {
                        remainingParameters = remainingParameters || {};
                        remainingParameters[key] = subQueryObject;
                        if (subQueryObject.truncate) {
                            // The new parameter does not fit into the hit but it may be truncated
                            index = freeSpace - subQueryObject.paramSize;
                            if (subQueryObject.separator) {
                                var query = subQueryObject.str.substring(0, freeSpace);
                                if (subQueryObject.encode) {
                                    index = query.lastIndexOf(encodeURIComponent(subQueryObject.separator)) || index;
                                }
                                else {
                                    index = query.lastIndexOf(subQueryObject.separator) || index;
                                }
                            }
                            hit += subQueryObject.param + subQueryObject.str.substring(0, index);
                            usedSpace += subQueryObject.paramSize + subQueryObject.str.substring(0, index).size;
                            remainingParameters[key].str = subQueryObject.str.substring(index, subQueryObject.strSize);
                            remainingParameters[key].strSize = remainingParameters[key].str.length;
                        }
                    }
                }
            }
        }
        return [hit, remainingParameters];
    };

    /**
     * First step in the formating of the querystring parameters: make a dictionary paramName => object (see _makeSubQueryObject).
     * <br />The resulting dictionary contains all parameters in string format, in the right order, ready to be truncate if they can, etc.
     * @memberof BuildManager#
     * @function
     * @param buffer {object} Buffer part to process
     * @returns {Array} :
     *  - index 0 : preProcessed parameters
     * <br />- index 1 : sum of the parameter size
     * <br />- index 2 : true if a parameter was too long and can't be truncated
     * <br />- index 3 : if sizeError name of the offending parameter
     * @private
     */
    var _preQuery = function (buffer) {
        var subQueries = {};
        var sizeError = false;
        var keySizeError = undefined;
        var bufferValue, bufferOptions;
        var encode, truncate, multihit, last;
        var subQueryObject, lastKey, lastSubQueryObject;
        var baseMultihit = '';
        for (var key in buffer) {
            if (buffer.hasOwnProperty(key)) {
                encode = false;
                truncate = false;
                multihit = false;
                last = false;
                bufferValue = buffer[key].value;
                bufferOptions = buffer[key].options || {};
                if (typeof bufferOptions.encode === 'boolean') {
                    encode = bufferOptions.encode;
                }
                // If the parameter is a function, it is executed to obtain a value
                // which will not pass the following processing (transformation into a string, encoding, etc.)
                if (typeof bufferValue === 'function') {
                    bufferValue = bufferValue();
                }
                if (bufferValue instanceof Array) {
                    // If the value is an array, it is transformed into strings separated by a given separator or by ","
                    bufferValue = bufferValue.join(bufferOptions.separator || ',');
                }
                else if (typeof bufferValue === 'object') {
                    // If it's an object, we serialize it
                    bufferValue = ATInternet.Utils.jsonSerialize(bufferValue);
                }
                else if (typeof bufferValue === 'undefined') {
                    bufferValue = 'undefined';
                }
                else {
                    bufferValue = bufferValue.toString();
                }
                if (encode) {
                    bufferValue = encodeURIComponent(bufferValue);
                }
                if (ATInternet.Utils.arrayIndexOf(_multiHitable, key) > -1) {
                    truncate = true;
                }
                else if (typeof bufferOptions.truncate === 'boolean') {
                    truncate = bufferOptions.truncate;
                }
                if (typeof bufferOptions.multihit === 'boolean') {
                    multihit = bufferOptions.multihit;
                }
                if (typeof bufferOptions.last === 'boolean') {
                    last = bufferOptions.last;
                }
                subQueryObject = _makeSubQueryObject(key, bufferValue, truncate, multihit, bufferOptions.separator, encode, last);
                if (multihit) {
                    // We remove the size of multihit parameters
                    AVAILABLESIZE -= (subQueryObject.paramSize + subQueryObject.strSize);
                    baseMultihit += subQueryObject.param + subQueryObject.str;
                }
                else {
                    if (!last) {
                        subQueries[key] = subQueryObject;
                        if (((subQueries[key].paramSize + subQueries[key].strSize) > AVAILABLESIZE) && !subQueries[key].truncate) {
                            // The parameter is too long and cannot be truncated
                            parent.emit('Tracker:Hit:Build:Error', {
                                lvl: 'ERROR',
                                msg: 'Too long parameter: "' + subQueries[key].param + '"',
                                details: {value: subQueries[key].str}
                            });
                            sizeError = true;
                            keySizeError = key;
                            break;
                        }
                    }
                    else {
                        // The parameter has the option'last', we keep it to place it at the end of the dictionary
                        if (subQueryObject.paramSize + subQueryObject.strSize > AVAILABLESIZE) {
                            subQueryObject.str = subQueryObject.str.substring(0, AVAILABLESIZE - subQueryObject.paramSize);
                            subQueryObject.strSize = subQueryObject.str.length;
                        }
                        lastKey = key;
                        lastSubQueryObject = subQueryObject;
                    }
                }
            }
        }
        if (lastKey) {
            subQueries[lastKey] = lastSubQueryObject;
        }
        return [subQueries, sizeError, keySizeError, baseMultihit];
    };

    /**
     * Build at least one queryString (except for "?s=xxxx" parameter) using {@link ATInternet.Tracker.BufferManager parameters in the BufferManager}.
     * <br />Indeed if the total size of the params involves a too long hit it's truncated in several hits (multihits).
     * @memberof BuildManager#
     * @function
     * @param customParams {object} Custom parameters forced for queryString
     * @param withOptions {boolean} true if custom params contain options
     * @param filters {Array} List of buffer filters
     * @param callback {function} Callback which will use the queryString
     * @returns {string|Array} String if the total size was short enough, other wise it will be an array.
     * <br />Warning: it will be an array with one element if one of the parameter was too long and couldn't be truncated.
     * @private
     */
    var _buildParams = function (customParams, withOptions, filters, callback) {

        var baseMultihit = '';
        var createQueriesString = function (buffer) {
            if (buffer === {}) {
                return [];
            }
            var queries = [];
            var resPreQuery = _preQuery(buffer);
            var queryStrParams = resPreQuery[0];
            var sizeError = resPreQuery[1];
            baseMultihit = resPreQuery[3];
            var resHit;
            if (sizeError) {
                // There is an error on the resPreQuery parameter[2]
                // We generate an array with only one element to indicate an error
                // This array will contain a hit with the parameter that caused the "truncated error" and
                // the parameter "mherr"
                var paramError = resPreQuery[2];
                var queryError = queryStrParams[paramError];
                queryError['str'] = queryError['str'].substring(0, AVAILABLESIZE - queryError['paramSize']);
                queryError['strSize'] = queryError['str'].length;
                var newPreQueries = {};
                newPreQueries['mherr'] = _makeSubQueryObject('mherr', '1', false, false, '', false);
                newPreQueries[paramError] = queryError;
                queryStrParams = newPreQueries;
            }
            resHit = _makeOneHit(queryStrParams, AVAILABLESIZE);
            if (resHit[1] === null) {
                // We are not in the case of a multihit so we return a string
                queries = resHit[0];
            }
            else {
                // We are in the case of a multihit so we return an array
                queries.push(resHit[0]);
                while (resHit[1] !== null) {
                    resHit = _makeOneHit(resHit[1], AVAILABLESIZE);
                    queries.push(resHit[0]);
                }
            }
            return queries;
        };

        var QueriesString = '';
        if (!parent['buffer']['presentInFilters'](filters, 'hitType')) {
            filters = parent['buffer']['addInFilters'](filters, 'hitType', ['page']);
        }
        filters = parent['buffer']['addInFilters'](filters, 'hitType', ['all']);

        var params, key;
        if (typeof customParams === 'object' && customParams !== null) {
            // Retrieve the filters and add the filter for permanent
            filters = parent['buffer']['addInFilters'](filters, 'permanent', true);
            params = parent['buffer']['get'](filters, true);
            var paramValue, paramOptions, isValue, isOptions, subKey;
            for (key in customParams) {
                if (customParams.hasOwnProperty(key)) {
                    paramValue = customParams[key];
                    paramOptions = {};
                    if (withOptions) {
                        if (typeof customParams[key] === 'object') {
                            isValue = false;
                            isOptions = false;
                            for (subKey in customParams[key]) {
                                if (customParams[key].hasOwnProperty(subKey)) {
                                    if (subKey === 'value') {
                                        isValue = true;
                                    }
                                    else if (subKey === 'options' && typeof customParams[key].options === 'object' && customParams[key].options !== null) {
                                        isOptions = true;
                                    }
                                }
                            }
                            if (isValue && isOptions) {
                                paramValue = customParams[key].value;
                                paramOptions = customParams[key].options;
                            }
                        }
                    }
                    params[key] = {
                        'value': paramValue,
                        'options': paramOptions
                    };
                }
            }
            QueriesString = createQueriesString(params);
        }
        else {
            params = parent.buffer.get(filters, true);
            QueriesString = createQueriesString(params);
            // We take out of the buffer all the parameters that are not permanent
            for (key in params) {
                if (params.hasOwnProperty(key)) {
                    if (!params[key].options || !params[key].options.permanent) {
                        parent['buffer']['del'](key);
                    }
                }
            }
        }
        callback && callback(QueriesString, baseMultihit);
    };

    /**
     * Build the base URL with level 1 and call the callback given with it
     * @memberof BuildManager#
     * @function
     * @param forcedDomain {String} Force the domain of the base URL
     * @param callback {function} Callback which will use the base URL
     * @private
     */
    var _buildConfig = function (forcedDomain, callback) {
        var configSecure = parent.getConfig('secure');
        var collectDomain = '';
        if (forcedDomain) {
            collectDomain = forcedDomain;
        }
        else {
            var protocolSecure = (document.location.protocol === 'https:');
            var isSecure = configSecure || protocolSecure;
            var log = isSecure ? parent.getConfig('logSSL') : parent.getConfig('log');
            var domain = parent.getConfig('domain');
            if (log && domain) {
                collectDomain = log + '.' + domain;
            }
            else {
                collectDomain = isSecure ? parent.getConfig('collectDomainSSL') : parent.getConfig('collectDomain');
            }
        }
        var baseURL = parent.getConfig('baseURL');
        var pixelPath = parent.getConfig('pixelPath');
        pixelPath = pixelPath || '/';
        if (pixelPath.charAt(0) !== '/') {
            pixelPath = '/' + pixelPath;
        }
        var site = parent.getConfig('site');
        if ((baseURL || (collectDomain && pixelPath)) && site) {
            site = '?s=' + site;
            var protocol = '//';
            if (configSecure) {
                protocol = 'https:' + protocol;
            }
            callback && callback(null, (baseURL ? baseURL : protocol + collectDomain + pixelPath) + site);
        }
        else {
            callback && callback({message: 'Config error'});
        }
    };

    /**
     * Build a list of hits (base URL + queryString)
     * @memberof BuildManager#
     * @function
     * @param customParams {object} Custom parameters forced
     * @param withOptions {boolean} true if custom params contain options
     * @param filters {Array} List of buffer filters
     * @param forcedDomain {String} Force the domain of the base URL
     * @param callback {function} callback which will use the hit list
     * @private
     */
    var _build = function (customParams, withOptions, filters, forcedDomain, callback) {
        _buildConfig(forcedDomain, function (err, baseHit) {
            if (!err) {
                // We remove the size of the hit base and reserve a space
                // for the multihit management parameter just in case
                AVAILABLESIZE = MAXSIZE - (baseHit.length + '&mh=xxxx-xxxx-xxxxxxxxxxxxx'.length);
                _buildParams(customParams, withOptions, filters, function (queriesString, baseMultihit) {
                    var hits = [];
                    var uuid = ATInternet.Utils.uuid();
                    var guid = uuid.num(13);
                    if (!(queriesString instanceof Array)) {
                        hits.push(baseHit + baseMultihit + queriesString);
                    }
                    else {
                        for (var i = 1; i <= queriesString.length; i++) {
                            hits.push(baseHit + baseMultihit + '&mh=' + i + '-' + queriesString.length + '-' + guid + queriesString[i - 1]);
                        }
                    }
                    callback && callback(null, hits);
                });
            }
            else {
                callback && callback(err);
            }
        });
    };

    /**
     * Send at least one hit, more if it's too long (multihits).
     * @name send
     * @memberof BuildManager#
     * @function
     * @param customParams {object} Object which contains some hit parameters that you would like to send specifically (they are given priority over the current buffer)
     * @param filters {Array} List of buffer filters
     * @param callback {function} Callback to execute
     * @param forcedDomain {String} Force the domain of the base URL
     * @param withOptions {boolean} true if custom params contain options
     * @public
     */
    self['send'] = function (customParams, filters, callback, forcedDomain, withOptions) {
        _build(customParams, withOptions, filters, forcedDomain, function (err, hits) {
            if (!err) {
                for (var i = 0; i < hits.length; i++) {
                    self.sendUrl(hits[i], callback);
                }
            }
            else {
                parent.emit('Tracker:Hit:Build:Error', {
                    lvl: 'ERROR',
                    msg: err.message,
                    details: {}
                });
                callback && callback();
            }
        });
    };

    /**
     * Send single hit from complete url.
     * <br />An event will be sent {@link ATInternet.Tracker.TriggersManager} :
     * <br />- "Tracker:Hit:Sent:Ok" with hit data if succeed
     * <br />- "Tracker:Hit:Sent:Error" with error data otherwise
     * @name sendUrl
     * @memberof BuildManager#
     * @function
     * @param hit {string} Url to send
     * @param callback {function} Callback to execute
     * @public
     */
    self['sendUrl'] = function (hit, callback) {
        var makeTriggerCallback = function (trackerTrigger, hit, level) {
            return (function () {
                return function (evt) {
                    var protocol = '';
                    if (hit.charAt(0) === '/') {
                        var configSecure = parent.getConfig('secure');
                        var protocolSecure = (document.location.protocol === 'https:');
                        var isSecure = configSecure || protocolSecure;
                        protocol = isSecure ? 'https:' : 'http:';
                    }
                    parent.emit(trackerTrigger, {
                        lvl: level,
                        details: {
                            hit: protocol + hit,
                            event: evt
                        }
                    });
                    callback && callback();
                };
            })();
        };
        if (ATInternet.Utils.isOptedOut() && !parent.getConfig('sendHitWhenOptOut')) {
            makeTriggerCallback('Tracker:Hit:Sent:NoTrack', hit, 'INFO')();
        }
        else {
            var img = new Image();
            img.onload = makeTriggerCallback('Tracker:Hit:Sent:Ok', hit, 'INFO');
            img.onerror = makeTriggerCallback('Tracker:Hit:Sent:Error', hit, 'ERROR');
            img.src = hit;
        }
    };

    // For unit tests on private elements !!!
    /* @if test */
    self.setAvailableSize = function (size) {
        AVAILABLESIZE = size;
    };
    self.multiHitable = _multiHitable;
    self.makeSubQueryObject = _makeSubQueryObject;
    self.makeOneHit = _makeOneHit;
    self.preQuery = _preQuery;
    self.buildParams = _buildParams;
    self.buildConfig = _buildConfig;
    self.build = _build;
    /* @endif */
};