Skip to content

Instantly share code, notes, and snippets.

@optimizely-christine
Last active January 26, 2018 22:48
Show Gist options
  • Save optimizely-christine/ec89129b6d5acbfcb55ca4ddd6e806d6 to your computer and use it in GitHub Desktop.
Save optimizely-christine/ec89129b6d5acbfcb55ca4ddd6e806d6 to your computer and use it in GitHub Desktop.
function sendOptimizelyEvent(eventId, key) {
try {
function guid() {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
s4() + '-' + s4() + s4() + s4();
}
function send(url, obj, cb) {
var xhr = new XMLHttpRequest();
xhr.open('POST', url);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.onload = function() {
if (xhr.status === 200) {
cb(null, JSON.parse(xhr.responseText));
}
};
xhr.send(JSON.stringify(obj));
}
var cookies = document.cookie.split(';')
.map(function(str) {
return str.split('=');
})
.reduce(function(acc, cur) {
acc[cur[0].trim()] = cur[1];
return acc;
}, {});
if (!cookies.optimizelyEventAPIData) return;
var sentEvents = cookies.optimizelySentEvents || '';
try {
sentEvents = sentEvents.split('.');
} catch(e) {}
if (sentEvents.indexOf(eventId)>=0) {
return;
}
var snapshots = {
decisions: cookies
.optimizelyEventAPIData
.split('-')
.map(function(idsStr) {
var ids = idsStr.split('.');
return {campaign_id: ids[0], experiment_id: ids[1], variation_id: ids[2]};
}),
events: [{
entity_id: eventId,
key: key,
timestamp: (+new Date),
// Create a randomized ID each time you send an event
// Can be anything
uuid: guid()
}]
};
var attributes = {
"type": "custom",
"value": window.location.pathname,
"entity_id": "10198331013",
"key": "page_type"
};
var data = {
account_id: '8159716341',
project_id: '9987056296',
visitors: [{
session_id: '',
visitor_id: cookies.optimizelyEndUserId,
attributes: [attributes],
snapshots: [snapshots]
}],
anonymize_ip: true,
client_name: 'LendingClubz',
client_version: '1.0.0'
};
// console.log(JSON.stringify(data, null, null, 4))
send('https://logx.optimizely.com/v1/events', data, function() {});
sentEvents.push(eventId);
document.cookie = 'optimizelySentEvents='+sentEvents.join('.');
} catch (e) {}
}
var demoEventList = {
pl_offer: '10002287710',
pl_app_submit: '10086611017',
pl_webapp_complete: '10021077430',
};
var prodEventList = {
pl_offer: '9979318249',
pl_app_submit: '10024866464',
pl_webapp_complete: '10028537411',
};
var pages = {
'/brSuccess.action': ['pl_webapp_complete'],
'/loanoffers.action': ['pl_offer', 'pl_app_submit'],
'/apply/personal/loan-offers': ['pl_offer', 'pl_app_submit'],
'/apply/personal/pending-joint-app': ['pl_app_submit'],
'/apply/personal/nice-loan-aa-notice': ['pl_app_submit'],
'/niceLoanTermsRejected.action': ['pl_app_submit']
};
(function() {
var url = window.location.href.toLowerCase();
Object.keys(pages).forEach(function(page) {
var link = page.toLowerCase();
if (url.includes(link)) {
var environment;
if (url.includes('lendingclub')) {
environment = prodEventList;
} else if (url.includes('demo.tlcinternal')) {
environment = demoEventList;
console.log('TEALIUM: Optimizely on DEMO');
}
if (environment) {
pages[page].forEach(function(event) {
var eventId = environment[event];
sendOptimizelyEvent(eventId, event);
});
}
return;
}
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment