/**
* @class
* @classdesc Plugin to measure internal searches.
* @name InternalSearch
* @memberof ATInternet.Tracker.Plugins
* @type {function}
* @param parent {object} Instance of the Tag used
* @public
*/
window['ATInternet']['Tracker']['Plugins']['InternalSearch'] = function (parent) {
"use strict";
var self = this,
conf = {};
// Set specific plugin configuration.
// If global configuration already exists, set only undefined properties.
parent.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 tag {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, tag, bufferKey, tagKey) {
if (tag.hasOwnProperty(tagKey)) {
buffer[bufferKey] = tag[tagKey];
}
};
/**
* Send internal searches data after a click.
* @memberof ATInternet.Tracker.Plugins.InternalSearch#
* @function
* @param tagObject {object} Tag object containing properties to use
* @private
*/
var _sendClickInternalSearch = function (tagObject) {
var buffer = {
'np': typeof tagObject['resultPageNumber'] !== 'undefined' ? tagObject['resultPageNumber'] : '1',
'click': 'IS'
};
_setDeclaredProperty(buffer, tagObject, 'mc', 'keyword');
_setDeclaredProperty(buffer, tagObject, 'mcrg', 'resultPosition');
parent.sendHit(buffer, [['hitType', ['InternalSearch']]]);
};
/**
* [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
*/
parent['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
*/
parent['internalSearch']['set'] = self['set'] = function (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 = parent.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
parent['setContext']('InternalSearch', obj);
/* @if debug */
parent.debug('InternalSearch:internalSearch:set', 'DEBUG', 'method ended', 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
*/
parent['internalSearch']['send'] = self['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;
});
_sendClickInternalSearch(tagObject);
/* @if debug */
parent.debug('InternalSearch:internalSearch:send', 'DEBUG', 'method ended', tagObject);
/* @endif */
return resultCheck;
};
/**
* Run plugin.
* @memberof ATInternet.Tracker.Plugins.InternalSearch#
* @function
* @private
*/
var _run = function () {
var newContext;
if (conf.urlKeyword) {
var href = document.location.href;
newContext = {};
parent['plugins']['exec']('Utils', 'getQueryStringValue', [conf.urlKeyword, href], function (data) {
if (data) {
newContext.keyword = data;
}
if (conf.urlResultPageNumber) {
parent['plugins']['exec']('Utils', 'getQueryStringValue', [conf.urlResultPageNumber, href], function (data2) {
newContext.resultPageNumber = data2 || '1';
});
}
});
}
if (newContext) {
parent.setContext('InternalSearch', newContext);
}
parent.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'];
parent['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 */
};
window['ATInternet']['Tracker']['addPlugin']('InternalSearch');