-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
003f78c
commit a8be238
Showing
1 changed file
with
153 additions
and
153 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,173 +1,173 @@ | ||
const cacheTrackEvents = { | ||
interval: null, | ||
responses: [], | ||
isTrackingResponses: false, | ||
trackPageUnload: () => { | ||
window.addEventListener("beforeunload", (event) => { | ||
if (!cacheTrackEvents.isPageViewSent()) { | ||
cacheTrackEvents.push("cached_analytics_page_views", { | ||
name: window.location.href, | ||
properties: { | ||
url: window.location.href, | ||
}, | ||
}); | ||
} | ||
}); | ||
}, | ||
trackResponses: () => { | ||
const originalXhrOpen = XMLHttpRequest.prototype.open; | ||
const originalXhrSend = XMLHttpRequest.prototype.send; | ||
|
||
XMLHttpRequest.prototype.open = function (method, url) { | ||
this._url = url; | ||
this._method = method; | ||
return originalXhrOpen.apply(this, arguments); | ||
}; | ||
|
||
XMLHttpRequest.prototype.send = function (body) { | ||
this.addEventListener("load", function () { | ||
let parsedPayload = null; | ||
|
||
if (typeof body === "string") { | ||
try { | ||
parsedPayload = JSON.parse(body); | ||
} catch (e) { | ||
parsedPayload = body; | ||
} | ||
interval: null, | ||
responses: [], | ||
isTrackingResponses: false, | ||
trackPageUnload: () => { | ||
window.addEventListener("beforeunload", (event) => { | ||
if (!cacheTrackEvents.isPageViewSent()) { | ||
cacheTrackEvents.push("cached_analytics_page_views", { | ||
name: window.location.href, | ||
properties: { | ||
url: window.location.href, | ||
}, | ||
}); | ||
} | ||
}); | ||
}, | ||
trackResponses: () => { | ||
const originalXhrOpen = XMLHttpRequest.prototype.open; | ||
const originalXhrSend = XMLHttpRequest.prototype.send; | ||
|
||
XMLHttpRequest.prototype.open = function (method, url) { | ||
this._url = url; | ||
this._method = method; | ||
return originalXhrOpen.apply(this, arguments); | ||
}; | ||
|
||
XMLHttpRequest.prototype.send = function (body) { | ||
this.addEventListener("load", function () { | ||
let parsedPayload = null; | ||
|
||
if (typeof body === "string") { | ||
try { | ||
parsedPayload = JSON.parse(body); | ||
} catch (e) { | ||
parsedPayload = body; | ||
} | ||
} | ||
|
||
const responseData = { | ||
url: this._url, | ||
method: this._method, | ||
status: this.status, | ||
headers: this.getAllResponseHeaders(), | ||
data: this.responseText, | ||
payload: parsedPayload, | ||
}; | ||
cacheTrackEvents.responses.push(responseData); | ||
}); | ||
const responseData = { | ||
url: this._url, | ||
method: this._method, | ||
status: this.status, | ||
headers: this.getAllResponseHeaders(), | ||
data: this.responseText, | ||
payload: parsedPayload, | ||
}; | ||
cacheTrackEvents.responses.push(responseData); | ||
}); | ||
|
||
return originalXhrSend.apply(this, arguments); | ||
}; | ||
}, | ||
isReady: () => { | ||
if (typeof Analytics === "undefined" || Analytics === null) { | ||
return false; | ||
return originalXhrSend.apply(this, arguments); | ||
}; | ||
}, | ||
isReady: () => { | ||
if (typeof Analytics === "undefined" || Analytics === null) { | ||
return false; | ||
} | ||
|
||
const instances = Analytics.Analytics.getInstances(); | ||
return !!(instances?.tracking && instances?.ab); | ||
}, | ||
parseCookies: (cookieName) => { | ||
const cookies = document.cookie.split("; ").reduce((acc, cookie) => { | ||
const [key, value] = cookie.split("="); | ||
acc[decodeURIComponent(key)] = decodeURIComponent(value); | ||
return acc; | ||
}, {}); | ||
|
||
return JSON.parse(cookies[cookieName] || null); | ||
}, | ||
isPageViewSent: () => | ||
!!cacheTrackEvents.responses.find( | ||
(e) => e.payload?.type === "page" && e.payload?.anonymousId | ||
), | ||
set: (event) => { | ||
cacheTrackEvents.push("cached_analytics_events", event); | ||
}, | ||
push: (cookieName, data) => { | ||
let storedCookies = []; | ||
const cacheCookie = cacheTrackEvents.parseCookies(cookieName); | ||
if (cacheCookie) storedCookies = cacheCookie; | ||
storedCookies.push(data); | ||
|
||
document.cookie = `${cookieName}=${JSON.stringify( | ||
storedCookies | ||
)}; path=/; Domain=.deriv.com`; | ||
}, | ||
track: (event, cache) => { | ||
if (cacheTrackEvents.isReady() && !cache) { | ||
Analytics.Analytics.trackEvent(event.name, event.properties); | ||
} else { | ||
cacheTrackEvents.set(event); | ||
} | ||
}, | ||
pageView: () => { | ||
if (!cacheTrackEvents.isTrackingResponses) { | ||
cacheTrackEvents.trackResponses(); | ||
cacheTrackEvents.trackPageUnload(); | ||
} | ||
|
||
let pageViewInterval = null; | ||
|
||
pageViewInterval = setInterval(() => { | ||
if ( | ||
typeof Analytics.Analytics?.pageView === "function" && | ||
cacheTrackEvents.isReady() | ||
) { | ||
Analytics?.Analytics?.pageView(window.location.href); | ||
} | ||
|
||
const instances = Analytics.Analytics.getInstances(); | ||
return !!(instances?.tracking && instances?.ab); | ||
}, | ||
parseCookies: (cookieName) => { | ||
const cookies = document.cookie.split("; ").reduce((acc, cookie) => { | ||
const [key, value] = cookie.split("="); | ||
acc[decodeURIComponent(key)] = decodeURIComponent(value); | ||
return acc; | ||
}, {}); | ||
|
||
return JSON.parse(cookies[cookieName] || null); | ||
}, | ||
isPageViewSent: () => | ||
!!cacheTrackEvents.responses.find( | ||
(e) => e.payload?.type === "page" && e.payload?.anonymousId | ||
), | ||
set: (event) => { | ||
cacheTrackEvents.push("cached_analytics_events", event); | ||
}, | ||
push: (cookieName, data) => { | ||
let storedCookies = []; | ||
const cacheCookie = cacheTrackEvents.parseCookies(cookieName); | ||
if (cacheCookie) storedCookies = cacheCookie; | ||
storedCookies.push(data); | ||
|
||
document.cookie = `${cookieName}=${JSON.stringify( | ||
storedCookies | ||
)}; path=/; Domain=.deriv.com`; | ||
}, | ||
track: (event,cache) => { | ||
if (cacheTrackEvents.isReady() && !cache) { | ||
Analytics.Analytics.trackEvent(event.name, event.properties); | ||
} else { | ||
cacheTrackEvents.set(event); | ||
if (cacheTrackEvents.isPageViewSent()) { | ||
clearInterval(pageViewInterval); | ||
} | ||
}, | ||
pageView: () => { | ||
if (!cacheTrackEvents.isTrackingResponses) { | ||
cacheTrackEvents.trackResponses(); | ||
cacheTrackEvents.trackPageUnload(); | ||
}, 1000); | ||
}, | ||
listen: (element, { name, properties }, cache) => { | ||
const addClickListener = (el) => { | ||
if (!el.dataset.clickEventTracking) { | ||
el.addEventListener("click", function () { | ||
cacheTrackEvents.track({ | ||
name, | ||
properties, | ||
cache, | ||
}); | ||
}); | ||
el.dataset.clickEventTracking = "true"; | ||
} | ||
}; | ||
|
||
let pageViewInterval = null; | ||
const elements = | ||
element instanceof NodeList ? Array.from(element) : [element]; | ||
|
||
pageViewInterval = setInterval(() => { | ||
if ( | ||
typeof Analytics.Analytics?.pageView === "function" && | ||
cacheTrackEvents.isReady() | ||
) { | ||
Analytics?.Analytics?.pageView(window.location.href); | ||
} | ||
elements.forEach(addClickListener); | ||
}, | ||
|
||
if (cacheTrackEvents.isPageViewSent()) { | ||
clearInterval(pageViewInterval); | ||
} | ||
}, 1000); | ||
}, | ||
listen: (element, { name, properties },cache) => { | ||
const addClickListener = (el) => { | ||
if (!el.dataset.clickEventTracking) { | ||
el.addEventListener("click", function () { | ||
cacheTrackEvents.track({ | ||
name, | ||
properties, | ||
cache | ||
}); | ||
}); | ||
el.dataset.clickEventTracking = "true"; | ||
} | ||
}; | ||
|
||
const elements = | ||
element instanceof NodeList ? Array.from(element) : [element]; | ||
addEventhandler: (items) => { | ||
cacheTrackEvents.interval = setInterval(() => { | ||
let allListenersApplied = true; | ||
|
||
elements.forEach(addClickListener); | ||
}, | ||
items.forEach(({ element, event, cache = false }) => { | ||
const elem = document.querySelectorAll(element); | ||
const elements = elem instanceof NodeList ? Array.from(elem) : [elem]; | ||
|
||
addEventhandler: (items) => { | ||
cacheTrackEvents.interval = setInterval(() => { | ||
let allListenersApplied = true; | ||
|
||
items.forEach(({ element, event, cache = false }) => { | ||
const elem = document.querySelectorAll(element); | ||
const elements = elem instanceof NodeList ? Array.from(elem) : [elem]; | ||
if (!elements.length) { | ||
allListenersApplied = false; | ||
} | ||
|
||
if (!elements.length) { | ||
elements.forEach((el) => { | ||
if (!el.dataset.clickEventTracking) { | ||
cacheTrackEvents.listen(el, event, cache); | ||
allListenersApplied = false; | ||
} | ||
|
||
elements.forEach((el) => { | ||
if (!el.dataset.clickEventTracking) { | ||
cacheTrackEvents.listen(el, event, cache); | ||
allListenersApplied = false; | ||
} | ||
}); | ||
}); | ||
}); | ||
|
||
if (allListenersApplied) { | ||
clearInterval(cacheTrackEvents.interval); | ||
} | ||
}, 1); | ||
|
||
return cacheTrackEvents; | ||
}, | ||
loadEvent: (items) => { | ||
items.forEach(({ event }) => { | ||
const { name, properties } = event; | ||
cacheTrackEvents.track({ | ||
name, | ||
properties, | ||
}); | ||
if (allListenersApplied) { | ||
clearInterval(cacheTrackEvents.interval); | ||
} | ||
}, 1); | ||
|
||
return cacheTrackEvents; | ||
}, | ||
loadEvent: (items) => { | ||
items.forEach(({ event }) => { | ||
const { name, properties } = event; | ||
cacheTrackEvents.track({ | ||
name, | ||
properties, | ||
}); | ||
}); | ||
|
||
return cacheTrackEvents; | ||
}, | ||
}; | ||
return cacheTrackEvents; | ||
}, | ||
}; |