/**
* @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
*/
ATInternet.Tracker.Plugins.Clicks = function (parent) {
'use strict';
var _this = this;
var _requestMethod = '';
var _debug = {
level: 'DEBUG',
messageEnd: 'method ended'
};
var _config = {};
// Set specific plugin configuration.
// If global configuration already exists, set only undefined properties.
parent.configPlugin('Clicks', dfltPluginCfg || {}, function (newConf) {
_config = newConf;
});
/**
* 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) {
return parent.utils.manageChapters(tagObject, 'chapter', 3) + (tagObject['name'] ? tagObject['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 hitType = ['click'];
var contextPageObject = parent['getContext']('page') || {};
buffer['pclick'] = _getFullName(contextPageObject);
buffer['s2click'] = contextPageObject['level2'] || '';
var customObjectValue = tagObject['customObject'];
if (customObjectValue) {
var hitParam = 'stc';
customObjectValue = parent.processTagObject(hitParam, hitType, customObjectValue);
buffer[hitParam] = {
_value: ATInternet.Utils.jsonSerialize(customObjectValue),
_options: {
hitType: hitType,
encode: true,
separator: ',',
truncate: true
}
};
}
parent.sendHit(buffer, [['hitType', hitType]], tagObject['callback'], _requestMethod);
};
/**
* Launch plugin when all dependencies are loaded.
* @memberof ATInternet.Tracker.Plugins.Clicks#
* @function
* @private
*/
var _init = function () {
_requestMethod = _config.requestMethod;
if (_requestMethod === 'POST' && !ATInternet.Utils.isBeaconMethodAvailable()) {
_requestMethod = 'GET';
}
};
// Initialise global values.
_init();
/**
* [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"><a href="http://example.com/" onclick="return tracker.click.send({elem:this,name:'clickName',level2:'clickLvl2',type:'exit'});">link</a>
* </code></pre>
* @example
* <pre><code class="html"><a href="http://example.com/" onclick="return myFunction(this);">link</a>
* <script type="text/javascript">
* 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
* });
* }
* </script>
* </code></pre>
*/
parent.click.send = _this.send = function (tagObject) {
var preservePropagation = true;
var eventObject = null;
if (tagObject && tagObject.hasOwnProperty('event')) {
eventObject = tagObject.event || window.event;
}
if (!ATInternet.Utils.isTabOpeningAction(eventObject) && tagObject['elem']) {
preservePropagation = parent.techClicks.manageClick(tagObject['elem'], eventObject);
}
_sendClick(tagObject);
/* @if debug */
parent.debug('Clicks:click:send', _debug.level, _debug.messageEnd, tagObject);
/* @endif */
// Return false if the clicked element is managed
return preservePropagation;
};
/**
* [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 = 'click';
parent.plugins.exec('TechClicks', 'isFormSubmit', [tagObject['elem']], function (data) {
event = data ? 'submit' : 'click';
});
ATInternet.Utils.addEvtListener(tagObject['elem'], event, function (eventObject) {
if (!ATInternet.Utils.isTabOpeningAction(eventObject)) {
parent.techClicks.manageClick(tagObject['elem'], eventObject);
}
_sendClick(tagObject);
});
}
/* @if debug */
parent.debug('Clicks:clickListener:send', _debug.level, _debug.messageEnd, 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.level, _debug.messageEnd, 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
* @param callback {function} Callback to execute
* @private
*/
parent.click.onDispatch = _this.onDispatch = function (callback) {
var hitType = ['click'];
var contextClickObject = parent.getContext('click') || {};
var contextPageObject = parent.getContext('page') || {};
parent.setParam('pclick', _getFullName(contextPageObject), {hitType: hitType});
parent.setParam('s2click', contextPageObject.level2 || '', {hitType: hitType});
parent.setParam('p', contextClickObject.name, {hitType: hitType});
parent.setParam('s2', contextClickObject.level2, {hitType: hitType});
var contextObject = contextClickObject.customObject;
if (contextObject) {
var hitParam = 'stc';
var paramOptions = {
hitType: hitType,
encode: true,
separator: ',',
truncate: true,
requestMethod: _requestMethod
};
parent.processContextObjectAndSendHit(hitParam, paramOptions, contextObject, callback);
} else {
parent.manageSend(function () {
parent.sendHit(null, [['hitType', hitType]], callback, _requestMethod);
});
}
parent.setContext('click', undefined);
};
// For unit tests on private elements !!!
/* @if test */
_this._sendClick = _sendClick;
_this._getClickType = _getClickType;
/* @endif */
};
ATInternet.Tracker.addPlugin('Clicks');