console.log('greetings from tms'); document.addEventListener("gravito:tcfv2:client", function (event) { // Check that opt-in is coming from CMP if (event.detail.eventType.match(/opt-in/)) { // Gravito ID is not available out-of-the-box, needs to be queried from the backend var gravitoId; getGravitoFirstPartyData(function (error, id) { sendAitaProfileEvent(id,'ping'); runTargeting(id); }); console.log(event.detail); } }); function sendAitaProfileEvent(id,event) { // Set interest segments randomly var segmentNumbers = getRandomSegmentNumbers(1,10); var segmentList = segmentNumbers.join(","); // Init gravito microprofile if(localStorage.getItem("gravito-microprofile")) { gravito.microprofile = JSON.parse(localStorage.getItem("gravito-microprofile")); } else { gravito.microprofile = {} gravito.microprofile.contextTags = {} } // Interest scoring for(tag in segmentNumbers) { var tagName = segmentNumbers[tag]; if(typeof(gravito.microprofile.contextTags[tagName])=="undefined") { gravito.microprofile.contextTags[tagName] = 1; } else { gravito.microprofile.contextTags[tagName] = parseInt(gravito.microprofile.contextTags[tagName]) + 1; } //console.log(tagName); } localStorage.setItem("gravito-microprofile",JSON.stringify(gravito.microprofile)); // Top 3 of interests (after scoring) var sortedInterests = Object.keys(gravito.microprofile.contextTags).sort(function(a,b){return gravito.microprofile.contextTags[a]-gravito.microprofile.contextTags[b]}).reverse(); var topInterests = sortedInterests.slice(0, 3); var topThreeInterests = sortedInterests.slice(0, 3).join(","); console.log(topThreeInterests); gravito.microprofile.interests = topThreeInterests; try { var consentString = JSON.parse(JSON.parse(JSON.parse(localStorage.getItem('gravito-microprofile-data'))).e['gravitoData']).TCString; } catch(e) {} var data = '{"event":"ping","id":"' + id + '","consentString":"' + consentString + '","segmentList":"' + topThreeInterests + '"}' var xhr = new XMLHttpRequest() xhr.open('POST', 'https://aita.gravito.net/api/listen/') //xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); xhr.onreadystatechange = function(response) { if(xhr.readyState == XMLHttpRequest.DONE && xhr.status == 200) { console.log(JSON.parse(response.target.response)); } } xhr.send(JSON.stringify(data)) } function getGravitoFirstPartyData(callback) { var gravitoSLD = "https://gto." + window.location.host.split(".").reverse()[1] + "." + window.location.host.split(".").reverse()[0]; var FirstPartyUrl = gravitoSLD + "/api/v3" + "/firstparty"; if (localStorage.getItem("gravitoFPID")) { callback(null, localStorage.getItem("gravitoFPID")); } else { var xhr = new XMLHttpRequest(); xhr.withCredentials = true; xhr.open("GET", FirstPartyUrl, true); xhr.onload = function () { console.log(xhr.response); var data = JSON.parse(xhr.response); localStorage.setItem("gravitoFPID", data.i); callback(null, data.i); }; xhr.onerror = function () { console.error(xhr.response); callback(xhr.response); }; xhr.send(); } } function getRandomSegmentNumbers(min, max) { var n = []; for(var i=0;i<3;i++){ n.push(Math.floor(Math.random() * max) + min); } return n; } function runTargeting(id) { // Find elements with aita tags for(i=0;i'; console.log(targets.placements); aitaApiGet('https://aita.gravito.net/api/count/?campaign_id= ' + targets.placements[targetedElement].campaign_id + '&event=display'); } }; xhr.send(); } } function aitaApiGet(url) { var xhr = new XMLHttpRequest(); xhr.open('GET', url); xhr.onload = function(data) { console.log("AITA API GET:"); console.log(data); } xhr.send(); }