Source: Clicks/clicks.js

/**
 * @class
 * @classdesc Plugin used to manage clicks tag.
 * @name Clicks
 * @memberof ATInternet.Tracker.Plugins
 * @type {function}
 * @param parent {object} Instance of the Tag used
 * @public
 */
window['ATInternet']['Tracker']['Plugins']['Clicks'] = function (parent) {
    "use strict";
    var _this = this;

    /**
     * Get the technical value for the click type
     * @name _getClickType
     * @memberof ATInternet.Tracker.Plugins.Clicks#
     * @function
     * @param typeclick {string} Label of the click type ('exit'/'download'/'action'/'navigation')
     * @returns string {string} Click type ('S','T','A' or 'N'). Empty string if bad type provided
     * @private
     */
    var _getClickType = function (typeclick) {
        var result = '';
        switch (typeclick) {
            case 'exit':
                result = 'S';
                break;
            case 'download':
                result = 'T';
                break;
            case 'action':
                result = 'A';
                break;
            case 'navigation':
                result = 'N';
                break;
            default:
                break;
        }
        return result;
    };

    /**
     * Get click and pclick labels with chapters
     * @name _getFullName
     * @memberof ATInternet.Tracker.Plugins.Clicks#
     * @function
     * @param tagObject {object} tag object
     * @returns string {string} Click name completed with chapters present in tag object
     * @private
     */
    var _getFullName = function (tagObject) {
        var name = tagObject['name'];
        parent['exec']('Utils', 'manageChapters', [tagObject, 'chapter', 3], function (data) {
            name = data + (name ? name : '');
        });
        return name;
    };

    /**
     * Send click hit with a given tag  ex: {elem:this,label:'myClick',level2:'',type:'navigation'}
     * @name _sendClick
     * @memberof ATInternet.Tracker.Plugins.Clicks#
     * @function
     * @param tagObject {object} tag object, can contain the following properties : label,chapter1,chapter2,chapter3,level2,type,elem,customObject
     * @private
     */
    var _sendClick = function (tagObject) {
        var buffer = {
            'p': _getFullName(tagObject),
            's2': tagObject['level2'] || '',
            'click': _getClickType(tagObject['type']) || ''
        };
        var contextPageObject = parent['getContext']('page') || {};
        buffer['pclick'] = _getFullName(contextPageObject);
        buffer['s2click'] = contextPageObject['level2'] || '';
        if (tagObject['customObject']) {
            parent['exec']('Utils', 'customObjectToString', [tagObject['customObject']], function (data) {
                buffer['stc'] = data;
            });
        }

        parent.sendHit(buffer, [['hitType', ['click']]]);
    };

    /**
     * [Object added by plugin {@link ATInternet.Tracker.Plugins.Clicks Clicks}] Tags for clicks.
     * @name click
     * @memberof ATInternet.Tracker.Tag
     * @inner
     * @type {object}
     * @property {function} send Tagging method for clicks (helper), see details here {@link ATInternet.Tracker.Tag#click.send}
     * @property {function} set Alternative tagging method for clicks (helper), see details here {@link ATInternet.Tracker.Tag#click.set}
     * @public
     */
    parent['click'] = {};

    /**
     * [Object added by plugin {@link ATInternet.Tracker.Plugins.Clicks Clicks}] Tags for clicks using a listener.
     * @name clickListener
     * @memberof ATInternet.Tracker.Tag
     * @inner
     * @type {object}
     * @property {function} send Tagging method for clicks (helper), see details here {@link ATInternet.Tracker.Tag#clickListener.send}
     * @public
     */
    parent['clickListener'] = _this['clickListener'] = {};

    /**
     * [Helper added by plugin {@link ATInternet.Tracker.Plugins.Clicks Clicks}] Tagging method for clicks (helper). This method should be called when the click happens.
     * @alias click.send
     * @memberof! ATInternet.Tracker.Tag#
     * @function
     * @param tagObject {object} tag object, must contain at least properties 'name' and 'type'. Should contain the property 'elem' to let the sdk manages the click redirection or submit if necessary
     * @public
     * @example
     * <pre><code class="html">&lt;a href="http://example.com/" onclick="return tracker.click.send({elem:this,name:'clickName',level2:'clickLvl2',type:'exit'});"&gt;link&lt;/a&gt;
     * </code></pre>
     * @example
     * <pre><code class="html">&lt;a href="http://example.com/" onclick="return myFunction(this);"&gt;link&lt;/a&gt;
     * &lt;script type="text/javascript"&gt;
     * function myFunction(element){
     *     // your code
     *     return tracker.click.send({
     *         elem:element,
     *         name:'clickName',
     *         chapter1:'chap1',
     *         chapter2:'chap2',
     *         chapter3:'chap3',
     *         level2:'clickLvl2',
     *         type:'exit',
     *         event:eventObj,
     *         callback:callback
     *     });
     * }
     * &lt;/script&gt;
     * </code></pre>
     */
    parent['click']['send'] = _this['send'] = function (tagObject) {
        // check if the clicked element should be managed and return false if it is the case, manageClick also adds the callback process if needed
        var resultCheck = true;
        parent['plugins']['exec']('TechClicks', 'manageClick', [tagObject['elem'], tagObject['event'], tagObject['callback']], function (data) {
            resultCheck = data;
        });
        _sendClick(tagObject);
        /* @if debug */
        parent.debug('Clicks:click:send', 'DEBUG', 'method ended', tagObject);
        /* @endif */
        return resultCheck;
    };

    /**
     * [Helper added by plugin {@link ATInternet.Tracker.Plugins.Clicks Clicks}] Tagging method for clicks (helper). This method should be called when loading the page
     * @alias clickListener.send
     * @memberof! ATInternet.Tracker.Tag#
     * @function
     * @param tagObject {object} tag object
     * @public
     * @example
     * <pre><code class="javascript">tracker.clickListener.send({
     *      elem:document.getElementById('myLink/myForm'),
     *      name:'clickName',
     *      chapter1:'chap1',
     *      chapter2:'chap2',
     *      chapter3:'chap3',
     *      level2:'clickLvl2',
     *      type:'exit'
     *      callback:callback
     *  });
     * </code></pre>
     */
    parent['clickListener']['send'] = _this['clickListener']['send'] = function (tagObject) {
        if (tagObject['elem']) {
            var event;
            parent['plugins']['exec']('TechClicks', 'isFormSubmit', [tagObject['elem']], function (data) {
                event = data ? 'submit' : 'click';
            });
            ATInternet.Utils.addEvtListener(tagObject['elem'], event, function (eventObj) {
                parent['plugins']['exec']('TechClicks', 'manageClick', [tagObject['elem'], eventObj, tagObject['callback']]);
                _sendClick(tagObject);
            });
        }
        /* @if debug */
        parent.debug('Clicks:clickListener:send', 'DEBUG', 'method ended', tagObject);
        /* @endif */
    };

    /**
     * [Helper added by plugin {@link ATInternet.Tracker.Plugins.Clicks Clicks}] Alternative tagging method for clicks (helper). Do not send the hit itself (it must be used with tracker.dispatch).
     * @alias click.set
     * @memberof! ATInternet.Tracker.Tag#
     * @function
     * @param tagObject {object} tag object, must contain at least properties 'name' and 'type' (and 'elem' if link/form with redirection/submit)
     * @public
     * @example
     * <pre><code class="javascript">tracker.click.set({
     *     name:'clickName',
     *     chapter1:'chap1',
     *     chapter2:'chap2',
     *     chapter3:'chap3',
     *     level2:'clickLvl2',
     *     type:'exit'
     * });
     * //...
     * tracker.dispatch();
     * </code></pre>
     */
    parent['click']['set'] = _this['set'] = function (tagObject) {
        parent.dispatchSubscribe('click');
        parent['setContext']('click', {
            'name': _getFullName(tagObject),
            'level2': (tagObject['level2'] || ''),
            'customObject': tagObject['customObject']
        });
        parent['setParam']('click', _getClickType(tagObject['type']) || '', {hitType: ['click']});
        /* @if debug */
        parent.debug('Clicks:click:set', 'DEBUG', 'method ended', tagObject);
        /* @endif */
    };

    /**
     * [Helper added by plugin {@link ATInternet.Tracker.Plugins.Clicks Clicks}] Will be called by tracker.dispatch if any click has been set
     * @alias click.onDispatch
     * @memberof! ATInternet.Tracker.Tag#
     * @function
     * @private
     */
    parent['click']['onDispatch'] = _this['onDispatch'] = function () {
        var opts = {hitType: ['click']};
        parent['setParam']('p', parent.getContext('click')['name'], opts);
        parent['setParam']('s2', parent['getContext']('click')['level2'], opts);
        var obj = parent['getContext']('click')['customObject'];
        if (obj) {
            parent['setParam']('stc', obj, {hitType: ['click'], 'encode': true});
        }
        var contextPageObject = parent['getContext']('page') || {};
        parent['setParam']('pclick', _getFullName(contextPageObject), opts);
        parent['setParam']('s2click', contextPageObject['level2'] || '', opts);
        parent['sendHit'](null, [['hitType', ['click']]]);
        parent['setContext']('click', undefined);
    };

    // For unit tests on private elements !!!
    /* @if test */
    _this['_sendClick'] = _sendClick;
    _this['_getClickType'] = _getClickType;
    /* @endif */
};

window['ATInternet']['Tracker']['addPlugin']('Clicks');