You are on page 1of 3

var nextSource = sources[index];

index += 1;
if (lazyLoadState[nextSource] === 'done') {
loadNext();
} else {
addToHead(nextSource, loadNext);
}
};

// start
loadNext();
});
}

// This is a preloader for the Google Tag Manager script


// The primary purpose of this script is to collect data for tracking pixels
// Simple variables, JS code, can be written directly in GTM
// More complex code should go here for a better code review process

(function () {
var GTM_DEBUG = false;
var MESG_PREFIX = "[GTM-Data]";

try {
// Sets up data for Google Tag Manager
GTM_DEBUG_MESG('Creating dataLayer');
window.dataLayer = window.dataLayer || [];

if (window.__CH_PERSON) {
dataLayer.push({
'User ID': window.__CH_PERSON.id,
'User Email': window.__CH_PERSON.email,
'User Name': window.__CH_PERSON.username,
'isVerifiedEducator': window.__CH_PERSON.isVerifiedEducator,
'isNewlyVerified': window.__CH_PERSON.isNewlyVerified,
'isApprovedExpert': window.__CH_PERSON.isApprovedExpert,
'approvedExpertType': window.__CH_PERSON.approvedExpertType,
'dimension2': window.__CH_PERSON.dimension2,
'dimension3': window.__CH_PERSON.dimension3,
'dimension4': window.__CH_PERSON.dimension4,
'dimension5': window.__CH_PERSON.dimension5
});
}
if (window.__CH_RECORD_SESSION) {
var triggers = window.__CH_RECORD_SESSION.triggers || [];
dataLayer.push({
'recordingTriggers': triggers
});
if (triggers.length) {
dataLayer.push({
'event': 'record-session'
});
}
}

if (window.__CH_WEBSERVER_NAME) {
dataLayer.push({
'Webserver Name': window.__CH_WEBSERVER_NAME
});
}
} catch (error) {
if (typeof (console) !== 'undefined') {
console.error(MESG_PREFIX + " ERROR: " + error);
}
}

window.attemptSessionRecording = function (triggerName) {


if (!window.__CH_RECORD_SESSION || window.__CH_RECORD_SESSION.length === 0)
{
fetch('/api/v1/sessionTriggers/'+triggerName+'/trigger/', {
method: 'POST',
credentials: 'same-origin',
}).then(function(response) {
return response.json();
}).then(function(data) {
if (data.shouldTrigger.includes('fireTriggerEvent')) {
dataLayer.push({
'event': 'record-session'
});

//Tell FullStory the event reason


dataLayer.push({
'recordingTriggers': '["'+triggerName+'"]'
});
window.__CH_RECORD_SESSION = [triggerName];
}
});
}
}

// Helper Functions

function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g,
" "));
}

//Get cookie item by name


function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1);
if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
}
return "";
}

//Formatted debug string with a single var to turn off


function GTM_DEBUG_MESG(mesg) {
if (GTM_DEBUG && typeof (console) !== 'undefined') {
console.debug(MESG_PREFIX + " " + mesg);
}
}
})();

You might also like