Source: InternalSearch/internalsearch.js

/**
 * @class
 * @classdesc Plugin to measure internal searches.
 * @name InternalSearch
 * @memberof ATInternet.Tracker.Plugins
 * @type {function}
 * @param tag {object} Instance of the Tag used
 * @public
 */
ATInternet.Tracker.Plugins.InternalSearch = function (tag) {
    'use strict';

    var self = this,
        conf = {};
    var _debug = {
        level: 'DEBUG',
        messageEnd: 'method ended'
    };
    // Set specific plugin configuration.
    // If global configuration already exists, set only undefined properties.
    tag.configPlugin('InternalSearch', dfltPluginCfg || {}, function (newConf) {
        conf = newConf;
    });

    /**
     * Set value if property has been declared.
     * @memberof ATInternet.Tracker.Plugins.InternalSearch#
     * @function
     * @param buffer {object} Buffer to process
     * @param tagObject {object} Tag object containing properties to use
     * @param bufferKey {string} Name of the buffer property to set
     * @param tagKey {string} Name of the tag property to test
     * @private
     */
    var _setDeclaredProperty = function (buffer, tagObject, bufferKey, tagKey) {
        if (tagObject.hasOwnProperty(tagKey)) {
            buffer[bufferKey] = tagObject[tagKey];
        }
    };

    /**
     * Send internal searches data after a click.
     * @memberof ATInternet.Tracker.Plugins.InternalSearch#
     * @function
     * @param tagObject {object} Tag object containing properties to use
     * @param elementType {string} Element type (mailto, form, redirection)
     * @private
     */
    var _sendClickInternalSearch = function (tagObject, elementType) {
        var buffer = {
            'np': typeof tagObject['resultPageNumber'] !== 'undefined' ? tagObject['resultPageNumber'] : '1',
            'click': 'IS'
        };
        _setDeclaredProperty(buffer, tagObject, 'mc', 'keyword');
        _setDeclaredProperty(buffer, tagObject, 'mcrg', 'resultPosition');
        var contextPageObject = tag.getContext('page') || {};
        if (contextPageObject.level2) {
            buffer.s2 = contextPageObject.level2;
        }
        tag.sendHit(buffer, [['hitType', ['InternalSearch']]], tagObject['callback'], null, elementType);
    };

    /**
     * [Object added by plugin {@link ATInternet.Tracker.Plugins.InternalSearch InternalSearch}] Tags to manage internal searches data.
     * @name internalSearch
     * @memberof ATInternet.Tracker.Tag
     * @inner
     * @type {object}
     * @property {function} set Tag helper, see details here {@link ATInternet.Tracker.Tag#internalSearch.set}
     * @property {function} send Tag helper, see details here {@link ATInternet.Tracker.Tag#internalSearch.send}
     * @public
     */
    tag.internalSearch = {};

    /**
     * [Helper added by plugin {@link ATInternet.Tracker.Plugins.InternalSearch InternalSearch}]Set internal searches properties.
     * @alias internalSearch.set
     * @memberof! ATInternet.Tracker.Tag#
     * @function
     * @param tagObject {object} 3 properties : keyword, resultPageNumber, resultPosition
     * @example
     * <pre><code class="javascript">tag.internalSearch.set({
     *      keyword: 'test',
     *      resultPageNumber: '123',
     *      resultPosition: '2'
     *  });
     * </code></pre>
     * @public
     */
    tag.internalSearch.set = function (tagObject) {

        tagObject = tagObject || {};

        // Initialize object
        var obj = {};
        _setDeclaredProperty(obj, tagObject, 'keyword', 'keyword');
        _setDeclaredProperty(obj, tagObject, 'resultPageNumber', 'resultPageNumber');

        // Add undeclared properties from url context if exist (InternalSearch context is set to undefined after hit sent, see Page plugin).
        var contextInternalSearchObject = tag.getContext('InternalSearch') || {};
        obj = ATInternet.Utils.completeFstLevelObj(obj, contextInternalSearchObject);

        // Put default value if page number is not defined
        if (typeof obj.resultPageNumber === 'undefined') {
            obj.resultPageNumber = '1';
        }

        // Set internal search context
        tag.setContext('InternalSearch', obj);

        /* @if debug */
        tag.debug('InternalSearch:internalSearch:set', _debug.level, _debug.messageEnd, tagObject);
        /* @endif */
    };

    /**
     * [Helper added by plugin {@link ATInternet.Tracker.Plugins.InternalSearch InternalSearch}] Send internal searches data.
     * @alias internalSearch.send
     * @memberof! ATInternet.Tracker.Tag#
     * @function
     * @param tagObject {object} 3 properties : keyword, resultPageNumber, resultPosition
     * @example
     * <pre><code class="javascript">tag.internalSearch.send({
     *      keyword: 'test',
     *      resultPageNumber: '123',
     *      resultPosition: '2'
     *  });
     * </code></pre>
     * @public
     */
    tag.internalSearch.send = function (tagObject) {

        tagObject = tagObject || {};

        var preservePropagation = true;
        var elementType = '';
        var eventObject = null;
        if (tagObject.hasOwnProperty('event')) {
            eventObject = tagObject.event || window.event;
        }
        if (!ATInternet.Utils.isTabOpeningAction(eventObject) && tagObject.elem) {
            var manage = tag.techClicks.manageClick(tagObject.elem, eventObject);
            preservePropagation = manage.preservePropagation;
            elementType = manage.elementType;
        }
        _sendClickInternalSearch(tagObject, elementType);
        /* @if debug */
        tag.debug('InternalSearch:internalSearch:send', _debug.level, _debug.messageEnd, tagObject);
        /* @endif */
        // Return false if the clicked element is managed
        return preservePropagation;
    };

    /**
     * Run plugin.
     * @memberof ATInternet.Tracker.Plugins.InternalSearch#
     * @function
     * @private
     */
    var _run = function () {
        var newContext;
        if (conf.urlKeyword) {
            var href = document.location.href;
            newContext = {};
            var keyword = tag.utils.getQueryStringValue(conf.urlKeyword, href);
            if (keyword) {
                newContext.keyword = keyword;
            }
            if (conf.urlResultPageNumber) {
                var urlResultPageNumber = tag.utils.getQueryStringValue(conf.urlResultPageNumber, href);
                newContext.resultPageNumber = urlResultPageNumber || '1';
            }
        }
        if (newContext) {
            tag.setContext('InternalSearch', newContext);
        }
        tag.emit('InternalSearch:Ready', {
            lvl: 'INFO',
            details: {
                config: {
                    urlKeyword: conf.urlKeyword,
                    urlResultPageNumber: conf.urlResultPageNumber
                },
                url: href,
                data: newContext
            }
        });
    };

    /**
     * Wait for dependencies before processing.
     * @memberof ATInternet.Tracker.Plugins.InternalSearch#
     * @function
     * @private
     */
    var _init = function () {
        var dependencies = ['Utils'];
        tag.plugins.waitForDependencies(dependencies, _run);
    };

    // Init process
    _init();

    // For unit tests on private elements !!!
    /* @if test */
    self._init = _init;
    self._run = _run;
    self._sendClickInternalSearch = _sendClickInternalSearch;
    self.conf = conf;
    /* @endif */
};
ATInternet.Tracker.addPlugin('InternalSearch');