Last active
February 9, 2017 18:10
-
-
Save HaQadosch/caadef516ce6e1ad5818ea18a2da61ae 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
// @flow | |
// /* eslint array-callback-return: "error" */ | |
/* eslint block-scoped-var: "error" */ | |
/* eslint no-implied-eval: "error" */ | |
/* eslint comma-style: ["error", "first"] */ | |
/* eslint max-len: ["error", 130] */ | |
/* eslint max-params: ["error", 3] */ | |
/* eslint-env es6 */ | |
/* eslint newline-per-chained-call: ["error", { "ignoreChainWithDepth": 1 }] */ | |
/* eslint operator-linebreak: [2, "before"] */ | |
const lookupCounter = counter => { | |
let foundCounter = {'unique': ''} | |
const counters = window.optimost.config.optimostData.counters | |
try { | |
if (counters) { | |
const cnt = '' + counter | |
; [foundCounter] = counters.filter(({counterId}) => counterId === cnt) | |
} | |
} catch (lookupCounterErr) { | |
window.optimost.saitama.log('! lookupCounter Error: ', lookupCounterErr) | |
} | |
return foundCounter | |
} | |
const cleanParams = query => query && encodeURIComponent(('' + query) | |
.replace(/^[?&]+/, '') | |
.replace(/[?&]+$/, '') | |
) | |
const buildAttrQueryString = (attrs = {}) => { | |
let attrQueryString = '' | |
try { | |
const keyValStringify = key => `&${cleanParams(key)}=${cleanParams(attrs[key])}` | |
const appendQueryString = (qs, attr) => qs + attr | |
attrQueryString = Object.keys(attrs) | |
.map(keyValStringify) | |
.reduce(appendQueryString, '') | |
} catch (buildAttrQueryStringErr) { | |
window.optimost.saitama.log('! buildAttrQueryString Error: ', buildAttrQueryStringErr) | |
} | |
return attrQueryString | |
} | |
const findstickyCookieName = subjectId => { | |
let foundstickyCookieName = '' | |
const experimentBySubjectId = exp => exp.subjectId === subjectId | |
const getStickyCookieName = (exps, exp) => exp.stickyCookieName | |
try { | |
const exps = window.optimost.config.optimostData.experiments | |
foundstickyCookieName = exps | |
.filter(experimentBySubjectId) | |
.reduce(getStickyCookieName, '') | |
} catch (findstickyCookieNameErr) { | |
window.optimost.saitama.log('! findstickyCookieName Error: ', findstickyCookieNameErr) | |
} | |
return foundstickyCookieName | |
} | |
const gumToLiid = cookie => cookie.replace(/gum$/, 'liid') | |
const getCookieQS = cookieName => { | |
let cookieString = '' | |
try { | |
if (cookieName) { | |
const cookieReg = new RegExp(`.*${cookieName}=([^ ;=]+).*`) | |
const cookieVal = document.cookie.replace(cookieReg, (_, val) => val) | |
cookieString = `${cleanParams(cookieName)}=${cleanParams(cookieVal)}` | |
} | |
} catch (getCookieQSErr) { | |
window.optimost.saitama.log('! getCookieQS Error: ', getCookieQSErr) | |
} | |
return cookieString | |
} | |
const buildLiidCookies = (cntData = {'subjectIds': []}) => { | |
let cookiesQS = '' | |
const appendQueryString = (qs, cookie) => cookie | |
? `${qs}&${cookie}` | |
: qs | |
try { | |
const subjectIds = cntData.subjectIds | |
cookiesQS = subjectIds | |
.map(findstickyCookieName) | |
.map(gumToLiid) | |
.map(getCookieQS) | |
.reduce(appendQueryString, '') | |
} catch (getCookieQSErr) { | |
window.optimost.saitama.log('! getCookieQS Error: ', getCookieQSErr) | |
} | |
return cookiesQS | |
} | |
const fireCounterLikeYouMeanIt = (cnt, attrs = false) => { | |
const cacheBuster = (() => { | |
let now = new Date() | |
let timeStamp = Math.round(now.getTime() / 1000) | |
return `?D_ts=${timeStamp}&D_tzo=${now.getTimezoneOffset()}` | |
})() | |
try { | |
const attrsQS = attrs | |
? buildAttrQueryString(attrs) | |
: '' | |
const cntData = lookupCounter(cnt) | |
const liidCookies = buildLiidCookies(cntData) | |
const tld = window.optimost.getBaseUrl(true) | |
const pathnm = cntData.unique | |
const src = tld + pathnm + cacheBuster + attrsQS + liidCookies | |
const sanitizedSRC = window.optimost._private.ApiUtils.sanitizeUrl(src) | |
window.optimost.saitama.log(' fireAjaxCounter url is: ', sanitizedSRC) | |
if (navigator.sendBeacon) { | |
navigator.sendBeacon(sanitizedSRC) | |
window.optimost.saitama.log(`fireAjaxCounter sendBeacon ${sanitizedSRC} fired`) | |
} else { | |
window.jQuery.get( | |
sanitizedSRC | |
, res => window.optimost.saitama.log(`fireAjaxCounter ${sanitizedSRC} pixel fired successfully`)) | |
} | |
} catch (fireAjaxCounterErr) { | |
window.optimost.saitama.log('! fireAjaxCounter error: ', fireAjaxCounterErr) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment