(function ($doc, $win) {
var dom;
var currentPageSection = '';
setupDefaults();
addListeners();
function setupDefaults() {
if (!$win.digitalData) $win.digitalData = {}
dom = {
clickType: '.ge-track[data-tracking-type="click_event"]',
altClickType: '.ge-track[data-tracking-type="ClickEvent"]',
imageType: '.ge-track[data-tracking-type="Image"]',
searchType: '.ge-track[data-tracking-type="advance_filter"]',
formType: '.ge-track.ge-form[data-tracking-type]',
pageSection: '.ge-track[data-tracking-type="pageSection"]',
};
currentPageSection = '';
}
function addListeners() {
$doc.addEventListener('click', handleClickType);
$doc.addEventListener('click', handleImageType);
$doc.addEventListener('click', handleSearchType);
(function(send) {
XMLHttpRequest.prototype.send = function(data) {
send.apply(this, arguments);
try{
if(data){
var ajaxSend = JSON.parse(data);
if(ajaxSend.terms){
handleFormType();
}
}
} catch(e){
console.warn('Could not track form submission type.')
}
}
}
)(XMLHttpRequest.prototype.send);
(function(open) {
XMLHttpRequest.prototype.open = function(data) {
open.apply(this, arguments);
try{
if( arguments.length > 2 ){
var data = arguments[1];
if(data.indexOf("term=") > 0){
handleSiteSearchType();
}
}
} catch(e){
console.warn('Could not track form submission type.')
}
}
}
)(XMLHttpRequest.prototype.open);
if (document.querySelectorAll(dom.pageSection).length) {
$win.addEventListener('scroll', debounce(function () {
handleSectionEntry();
}, 250));
}
}
function track(type) {
if ($win._satellite) {
$win._satellite.track(type);
} else {
console.warn('Satellite tracking is not available.')
}
}
function handleClickType(e) {
var target = e.target;
if (target && (target.matches(dom.clickType) || target.matches(dom.altClickType))) {
digitalData.click = target.getAttribute('data-tracking-title');
track('click_event');
}
}
function handleImageType(e) {
var target = e.target;
if (target && (target.matches(dom.searchType) || target.matches(dom.searchType))) {
digitalData.page.category.Image = target.getAttribute('data-tracking-title');
track('image');
}
}
function handleSearchType(e) {
const target = e.target;
if (target && target.matches(dom.searchType)) {
setTimeout(function () {
digitalData.Search = target.getAttribute('data-tracking-title');
track('advance_filter');
}, 250);
}
}
function handleSectionEntry() {
const pageTop = $win.scrollY || $doc.documentElement.scrollTop;
const pageBottom = pageTop + $win.innerHeight;
$doc.querySelectorAll(dom.pageSection).forEach(function (el, i) {
const top = el.getBoundingClientRect().top + $doc.body.scrollTop;
const bottom = top + outerHeight(el);
const title = el.getAttribute('data-tracking-title');
if ((top > pageTop && top < pageBottom) ||
(bottom < pageBottom && bottom > pageTop) ||
(top < pageTop && bottom > pageBottom) &&
title !== currentPageSection) {
currentPageSection = title;
digitalData.page.category.pageSection = currentPageSection;
track('pageSection');
}
});
}
function handleFormType() {
const forms = $doc.querySelectorAll(dom.formType);
if (forms.length > 0 && forms[0].getAttribute('data-tracking-title')) {
digitalData.Search = forms[0].getAttribute('data-tracking-title');
track('registered');
}
}
function handleSiteSearchType() {
var title = getQueryVariable('term') + " | " + (getQueryVariable('filter') ? getQueryVariable('filter') : 'Product'); digitalData.Search = title;
track('search');
}
function getQueryVariable(variable) {
var query = window.location.search.substring(1);
var vars = query.split('&');
for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split('=');
if (decodeURIComponent(pair[0]) == variable) {
return decodeURIComponent(pair[1]);
}
}
return null;
}
function outerHeight(el) {
var height = el.offsetHeight;
var style = getComputedStyle(el);
height += parseInt(style.marginTop) + parseInt(style.marginBottom);
return height;
}
function debounce(func, wait, options) {
var lastArgs,
lastThis,
maxWait,
result,
timerId,
lastCallTime,
lastInvokeTime = 0,
leading = false,
maxing = false,
trailing = true;
if (typeof func != 'function') {
throw new TypeError(FUNC_ERROR_TEXT);
}
wait = toNumber(wait) || 0;
if (isObject(options)) {
leading = !!options.leading;
maxing = 'maxWait' in options;
maxWait = maxing ? nativeMax(toNumber(options.maxWait) || 0, wait) : maxWait;
trailing = 'trailing' in options ? !!options.trailing : trailing;
}
function invokeFunc(time) {
var args = lastArgs,
thisArg = lastThis;
lastArgs = lastThis = undefined;
lastInvokeTime = time;
result = func.apply(thisArg, args);
return result;
}
function leadingEdge(time) {
lastInvokeTime = time;
timerId = setTimeout(timerExpired, wait);
return leading ? invokeFunc(time) : result;
}
function remainingWait(time) {
var timeSinceLastCall = time - lastCallTime,
timeSinceLastInvoke = time - lastInvokeTime,
timeWaiting = wait - timeSinceLastCall;
return maxing
? nativeMin(timeWaiting, maxWait - timeSinceLastInvoke)
: timeWaiting;
}
function shouldInvoke(time) {
var timeSinceLastCall = time - lastCallTime,
timeSinceLastInvoke = time - lastInvokeTime;
return (lastCallTime === undefined || (timeSinceLastCall >= wait) ||
(timeSinceLastCall < 0) || (maxing && timeSinceLastInvoke >= maxWait));
}
function timerExpired() {
var time = now();
if (shouldInvoke(time)) {
return trailingEdge(time);
}
timerId = setTimeout(timerExpired, remainingWait(time));
}
function trailingEdge(time) {
timerId = undefined;
if (trailing && lastArgs) {
return invokeFunc(time);
}
lastArgs = lastThis = undefined;
return result;
}
function cancel() {
if (timerId !== undefined) {
clearTimeout(timerId);
}
lastInvokeTime = 0;
lastArgs = lastCallTime = lastThis = timerId = undefined;
}
function flush() {
return timerId === undefined ? result : trailingEdge(now());
}
function debounced() {
var time = now(),
isInvoking = shouldInvoke(time);
lastArgs = arguments;
lastThis = this;
lastCallTime = time;
if (isInvoking) {
if (timerId === undefined) {
return leadingEdge(lastCallTime);
}
if (maxing) {
timerId = setTimeout(timerExpired, wait);
return invokeFunc(lastCallTime);
}
}
if (timerId === undefined) {
timerId = setTimeout(timerExpired, wait);
}
return result;
}
debounced.cancel = cancel;
debounced.flush = flush;
return debounced;
}
})(document, window)