Created
February 3, 2021 20:10
-
-
Save simone-coelho/acb3193f4dcce63dab030318843c1d51 to your computer and use it in GitHub Desktop.
This file contains 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
// BEGIN: Initialize Optimizely Event Push | |
function dispatchOptimizelyEvent(payload, eventID, eventKey) { | |
var xmlhttp = new XMLHttpRequest(); | |
xmlhttp.onreadystatechange = function() { | |
if (xmlhttp.readyState == 4) { // request finished and response is ready | |
if(xmlhttp.status == 204) { | |
console.log('Fired event to Optimizely: ', eventKey); | |
} else { | |
console.log('Failed to dispatch Optimizely event: ', xmlhttp.responseText); | |
} | |
} | |
}; | |
xmlhttp.open('POST', 'https://logx.optimizely.com/v1/events', true); | |
xmlhttp.setRequestHeader('Content-Type', 'application/json'); | |
xmlhttp.send(JSON.stringify(payload)); | |
} | |
//parse cookie | |
function getCookie(name) { | |
var c = document.cookie.match('(^|[^;]+)\\s*' + name + '\\s*=\\s*([^;]+)'); | |
return c ? c.pop() : ''; | |
} | |
//create event payload, takes in an event ID and the event key | |
function createOptimizelyEvent(eventID, eventKey) { | |
var visitorId = getCookie('optimizelyEndUserId'); //this is the visitor ID that is being used | |
var timestamp = Date.now() + 1000; | |
dispatchOptimizelyEvent({ | |
account_id: 'Global Account ID Goes here not the Project ID', | |
visitors: [{ | |
visitor_id: visitorId, | |
attributes: [], | |
snapshots: [{ | |
decisions: [], | |
events: [{ | |
entity_id: eventID, | |
key: eventKey, | |
timestamp: timestamp, | |
uuid: visitorId + '-' + timestamp.toString() //this should be as random as possible | |
}] | |
}] | |
}], | |
anonymize_ip: true, | |
client_name: 'Optimizely/EventAPI', | |
client_version: '1.0.0', | |
enrich_decisions: true /* if this is set, we do not need to pass experiment decisions explicitly */ | |
}, eventID, eventKey); | |
} | |
// END: Initialize Optimizely Event Push |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment