Last active
September 19, 2019 23:37
-
-
Save thegreekjester/435fd49d5073f0d5858ddd56be1293bc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var templatePayload = { | |
"account_id": 'account123', | |
"visitors": [{ | |
"visitor_id": null, | |
"attributes": {}, | |
"snapshots": [{ | |
"decisions": [], | |
"events": [] | |
/* | |
https://developers.optimizely.com/x/events/api/index.html | |
Event { | |
"entity_id": "123456", | |
"key": "test_event", | |
"timestamp": 1540996187279, | |
"revenue": 10000, | |
"uuid": "2314324123" | |
} | |
*/ | |
}] | |
}], | |
"anonymize_ip": true, | |
"client_name": "Optimizely/client-side-event-tracker", | |
"client_version": "1.0.0", | |
"enrich_decisions": true | |
} | |
var buildPayload = function (visitorId, events) { | |
events = events || []; | |
// enrich event data with required data | |
events = events.map(function (evt) { | |
return Object.assign(evt, { | |
uuid: evt.uuid || Math.random().toString(36).substr(2, 10), | |
timestamp: evt.timestamp || (new Date).getTime() | |
}); | |
}); | |
var sendPayload = Object.assign({}, templatePayload, { visitors: [{ attributes: [], visitor_id: visitorId, snapshots: [{ decisions: [], events: events }] }] }) | |
return sendPayload; | |
} | |
var trackEvent = function () { | |
var payload = buildPayload.apply(null, arguments); | |
return fetch('https://logx.optimizely.com/v1/events', { | |
method: 'post', | |
body: JSON.stringify(payload) | |
}).then(function (response) { | |
if (response.status > 199 && response.status < 300) return Promise.resolve(response); | |
return Promise.reject(response); | |
}); | |
} | |
function getCookie(name) { | |
var match = document.cookie.match(name + '=([^;]*)'); | |
return match ? match[1] : undefined; | |
}; | |
function getUrlParameter(sParam) { | |
var sPageURL = window.location.search.substring(1), | |
sURLVariables = sPageURL.split('&'), | |
sParameterName, | |
i; | |
for (i = 0; i < sURLVariables.length; i++) { | |
sParameterName = sURLVariables[i].split('='); | |
if (sParameterName[0] === sParam) { | |
return sParameterName[1] === undefined ? true : decodeURIComponent(sParameterName[1]); | |
} | |
} | |
}; | |
var session_data = JSON.parse(window.sessionStorage.getItem('optly_data')) | |
if(session_data){ | |
var data = session_data | |
}else{ | |
var data = window.optimizely.get('data'); | |
window.sessionStorage.setItem('optly_data', JSON.stringify(data)); | |
} | |
var endUserId = getCookie('optimizelyEndUserId'); | |
var topLevel = window.location.hostname.split('.').slice(-2).join('.') | |
var tags = [] | |
for (var event in data.events) { | |
if (data.events[event].eventType === 'click') { | |
tags.push({ id: event, selector: data.events[event].eventFilter.selector }) | |
} | |
} | |
tags.forEach(function(obj) { | |
let el = document.querySelector(obj.selector) | |
if (el && el.href) { | |
el.href += (el.href.split('?')[1] ? '&' : '?') + `event=${obj.id}`; | |
el.href += (el.href.split('?')[1] ? '&' : '?') + `opt_id=${endUserId}`; | |
} | |
}) | |
if(window.location.href.indexOf('opt_id') !== -1 && window.location.href.indexOf('event') !== -1){ | |
let newId = getUrlParameter('opt_in'); | |
let event = getUrlParameter('event'); | |
trackEvent(newId, [{ | |
"entity_id": event | |
}]) | |
.then(function (success) { | |
console.log('Success', success); | |
}, function (err) { | |
console.log('Error', err); | |
}); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment