You are on page 1of 3

!

(function (w, d, p, u, a, t) {
"use strict";

const T0 = 1 * new Date();


const K_TTL = 'au_seg_ttl';
const K_DATA = 'au_seg_data';
const AU_SEG = {'segments': ['AU_AUD_KST5U8', 'AU_AUD_X86C3Q', 'AU_AUD_XVRZX5',
'AU_AUD_K714BA', 'AU_AUD_OS7HC6', 'AU_AUD_ULEQRT', 'AU_SEG_GENDER_MALE',
'AU_AUD_CIIJOI', 'AU_AUD_VMQ5V7', 'AU_AUD_L98IY2', 'AU_SEG_STREAMER_3',
'AU_AUD_9ALLJC', 'AU_SEG_AGE_25-34', 'AU_AUD_4VL3DN', 'AU_AUD_EMIMA7',
'AU_AUD_2VCRGE', 'AU_AUD_FZBGST', 'AU_AUD_2AEAXO', 'AU_AUD_OJWO2Q',
'AU_AUD_YYMFNQ', 'AU_AUD_HKDLY4', 'AU_AUD_MJJ91R', 'AU_AUD_BZ2GUL',
'AU_AUD_NI5YXA', 'AU_SEG_AGE_18-24', 'AU_AUD_WFCGH1', 'AU_AUD_20BX7Z',
'AU_SEG_AGE_25-54']};

function getTaggerId() {
try {
const item = localStorage.getItem('au/tid');
if (item !== null) {
const taggerId = JSON.parse(item);
return taggerId['v'];
}
return null;
}
catch (err) {
return null;
}
}

function getAudigentId() {
const fkey = '_au_1d=';
const decodedCookie = decodeURIComponent(document.cookie);
const ca = decodedCookie.split(';');
for (let i = 0; i < ca.length; ++i) {
let c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(fkey) == 0) {
return c.substring(fkey.length, c.length);
}
}
return null;
}

function getCache() {
if (typeof (Storage) !== 'undefined') {
const ttl = parseInt(w.localStorage.getItem(K_TTL) || '0');
if (ttl > T0) {
return JSON.parse(w.localStorage.getItem(K_DATA));
}
}
return null;
}

function setCache(response) {
if (typeof (Storage) !== 'undefined' && response.segments.length > 0) {
const t1 = T0 + (5 * 60 * 1000);
w.localStorage.setItem(K_TTL, t1);
w.localStorage.setItem(K_DATA, JSON.stringify(response));
}
}

function responseHandler(response, withCache) {


// default behaviour saves segments in the window context
w.au_seg = response;

const t2 = 1 * new Date();


console.log('[Audigent/' + p + '] Segment Service > Response Time ' + (t2 -
T0).toString() + ' millis');

// trigger custom event to notify other libraries that the data is ready
d.dispatchEvent(new CustomEvent('auSegReady', { 'detail': response }));

if (withCache) {
setCache(response);
}
}

const cres = getCache();


if (cres === null) {
console.log('[Audigent/' + p + '] Segment Service > Cache Miss');

responseHandler(AU_SEG, false);

u = 'https://' + u + '/api/v1/segments?url=' +
encodeURIComponent(d.location.href) + '&partner_id=' + p;
a = getAudigentId();
if (typeof a === 'string') {
u = u + '&au_id=' + a;
}
t = getTaggerId();
if (typeof t === 'string') {
u = u + '&tagger_id=' + t;
}

const xhr = new XMLHttpRequest();


xhr.timeout = 3000; // 3 secs
xhr.open('GET', u, true);
xhr.onreadystatechange = () => {
if (xhr.readyState == XMLHttpRequest.DONE && xhr.status == 200) {
const nres = JSON.parse(xhr.responseText);
responseHandler(nres, true);
}
};
xhr.ontimeout = () => {
console.warn('[Audigent/' + p + '] Segment Service > Timeout');
}
xhr.onerror = () => {
console.warn('[Audigent/' + p + '] Segment Service > Invalid
Response');
};
xhr.send(null);
} else {
console.log('[Audigent/' + p + '] Segment Service > Cache Hit');

responseHandler(cres, true);
}
})(window, document, '158', 'seg.ad.gt');

You might also like