Skip to content

Instantly share code, notes, and snippets.

@rawmain
Last active September 26, 2022 17:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rawmain/53750b5ebff7b986b622c8d37c32510e to your computer and use it in GitHub Desktop.
Save rawmain/53750b5ebff7b986b622c8d37c32510e to your computer and use it in GitHub Desktop.
HOST Analytics
const storage = localStorage;
if (!Math.imul) Math.imul = function(opA, opB) {
opB |= 0;
var result = (opA & 0x003fffff) * opB;
if (opA & 0xffc00000) result += (opA & 0xffc00000) * opB|0;
return result|0;
};
const cyrb53 = function(str, seed = 0) {
let h1 = 0xdeadbeef ^ seed,
h2 = 0x41c6ce57 ^ seed;
for (let i = 0, ch; i < str.length; i++) {
ch = str.charCodeAt(i);
h1 = Math.imul(h1 ^ ch, 2654435761);
h2 = Math.imul(h2 ^ ch, 1597334677);
}
h1 = Math.imul(h1 ^ h1 >>> 16, 2246822507) ^ Math.imul(h2 ^ h2 >>> 13, 3266489909);
h2 = Math.imul(h2 ^ h2 >>> 16, 2246822507) ^ Math.imul(h1 ^ h1 >>> 13, 3266489909);
return 4294967296 * (2097151 & h2) + (h1 >>> 0);
};
const generateId = () => {
let creationTime = new Date().getTime();
let expiryTime = creationTime + (30 * 24 * 3600 * 1000); // Change 30 to any number of days you want the CID to be valid.
let CIDSource = window.location.host + ";" + navigator.userAgent + ";" + navigator.language + ";" + creationTime;
if (window.localStorage) {
CIDhashed = localStorage.getItem('CID_HASHED');
CIDexpiry = localStorage.getItem('CID_EXPIRY');
if ((CIDhashed === null || CIDexpiry === null)
|| (CIDhashed !== null && CIDexpiry !== null && CIDexpiry >= expiryTime)) {
localStorage.setItem('CID_HASHED', cyrb53(CIDSource).toString(16));
localStorage.setItem('CID_EXPIRY', expiryTime);
}
return storage.CID_HASHED;
} else {
return undefined;
}
};
const getId = () => {
if (!storage.CID_HASHED) {
storage.CID_HASHED = generateId();
}
return storage.CID_HASHED;
};
const getExpiry = () => {
if (!storage.CID_EXPIRY) {
storage.CID_HASHED = generateId();
}
return storage.CID_EXPIRY;
};
function setCookie(e, i) {
var s = location.hostname.split(".").reverse(),
n = "." + s[1] + "." + s[0],
t = new Date(new Date().setTime(getExpiry())),
o = e + "=" + encodeURIComponent(i) + ";";
o += "expires=" + t.toUTCString() + ";", o += "path=/;", o += "domain=" + n + ";", document.cookie = o
}
function getCookie(e) {
for (var i = e + "=", s = document.cookie.split(";"), n = 0; n < s.length; n++) {
for (var t = s[n];
" " == t.charAt(0);) t = t.substring(1, t.length);
if (0 == t.indexOf(i)) return t.substring(i.length, t.length)
}
return null
}
function deleteCookie(e, i, s) {
getCookie(e) && createCookie(e, "", -1, i, s)
}
null === getCookie("ibriditics_uuid") && (setCookie("ibriditics_uuid", uuid_gaquattro = getId()), ibriditics_uuid = uuid_gaquattro), null === getCookie("ibriditics_uuid4") && (setCookie("ibriditics_uuid4", uuid_universal = getId()), ibriditics_uuid4 = uuid_universal);
const params = new Proxy(new URLSearchParams(document.currentScript.src.substring(document.currentScript.src.indexOf("?"))), {
get: (e, i) => e.get(i)
});
let id = parseInt(params.id),
beacon_endpoint = "https://hsXXXXXXXXX.TUODOMINIO.EXT/v1/ibriditics"; // sostituire con proprio endpoint in dashboard Host
class WebSessionCounter {
constructor() {
this.update()
}
get count() {
return Number(getCookie("ib_session_number"))
}
set count(e) {
setCookie("ib_session_number", e)
}
get sessionHit() {
return Number(getCookie("ib_session_hit"))
}
set sessionHit(e) {
setCookie("ib_session_hit", e)
}
get sessionId() {
return Number(getCookie("ib_session_id"))
}
set sessionId(e) {
setCookie("ib_session_id", e)
}
get lastActive() {
let e = decodeURIComponent(getCookie("ib_session_last_active"));
return e ? new Date(e) : new Date
}
set lastActive(e) {
setCookie("ib_session_last_active", e.toISOString())
}
update() {
let e = this.count,
i = this.sessionHit;
console.log("session ->" + this.count), 0 === e || this.isNewSession() ? (this.count = e + 1, this.sessionHit = 0, this.sessionId = parseInt(+new Date / 1e3), this.lastActive = new Date, this.sessionStart = !0, console.log("new session ->" + this.count + " " + this.sessionId)) : (this.sessionStart = !1, this.sessionHit = i + 1)
}
isNewSession() {
let e = this.lastActive,
i = new Date;
return [(i - e) / 1e3 / 60 > 30, i.toDateString() !== e.toDateString(), ].some(e => e)
}
}
ibSess = new WebSessionCounter;
var ibriditics_uuid = getCookie("ibriditics_uuid"),
ibriditics_uuid4 = getCookie("ibriditics_uuid4"),
ga_session_number = ibSess.count,
sequence = ibSess.sessionHit,
ga_session_id = ibSess.sessionId,
session_start = !1;
0 == ibSess.sessionHit && (session_start = !0);
var navigator_info = window.navigator,
screen_info = window.screen,
location_info = document.location;
const user_agent = navigator_info.userAgent;
let page_referrer = document.referrer;
"" == page_referrer && (page_referrer = null);
const screen_resolution = screen_info.height + "x" + screen_info.width,
language = navigator.language || navigator.userLanguage,
page_location = location_info.origin + location_info.pathname,
page_hostname = location_info.origin,
page_title = document.title;
let searchParams = new URLSearchParams(location_info.search);
var debug_mode = "true" === searchParams.get("debug_mode"),
dbg = "";
searchParams.get("dbg") && (dbg = "?dbg=" + searchParams.get("dbg"));
var IbriditicQueue = function() {
this.push = function() {
for (var e = 0; e < arguments.length; e++) try {
"function" == typeof arguments[e] ? arguments[e]() : trackers[arguments[e][0]].call(arguments[e][0], arguments[e][1], arguments[e][2])
} catch (i) {}
}
};
const trackers = {
event: function(e, i = {
params: {}
}) {
let s = 0;
("page_view" == e || "PAGEVIEW" == e) && (s = 1);
let n = i.params,
t = !1,
o = !0;
i.params.non_personalized_ads ? (t = !1, o = !0) : (t = !0, o = !1), delete n.non_personalized_ads;
let r = Object.assign({}, n, null === user_agent ? null : {
user_agent
}, null === page_referrer ? null : {
page_referrer
}, null === screen_resolution ? null : {
screen_resolution
}, null === language ? null : {
language
}, null === page_title ? null : {
page_title
}, null === s ? null : {
engagement_time_msec: s
}, null === ga_session_number ? null : {
ga_session_number
}, null === ga_session_id ? null : {
ga_session_id
}, null === session_start ? null : {
session_start
}, null === sequence ? null : {
sequence
});
user_properties = Object.assign({}, null === language ? null : {
language: {
value: language
}
}, null === t ? null : {
consent: {
value: t
}
}, {
platform: {
value: "web"
}
});
var a = {
url: page_location,
id_site: id,
label: e,
debug: debug_mode,
npa: o,
uuid: ibriditics_uuid,
uuid4: ibriditics_uuid4,
params: r,
user_properties: user_properties
},
u = new Blob([JSON.stringify(a)], {
type: "application/json"
});
navigator.sendBeacon(beacon_endpoint + dbg, u)
},
set: function(e, i = {
params: {}
}) {
let s = i.params,
n = !1,
t = !0;
i.params.non_personalized_ads ? (n = !1, t = !0) : (n = !0, t = !1), delete s.non_personalized_ads;
let o = Object.assign({}, s, null === ga_session_number ? null : {
ga_session_number
}, null === ga_session_id ? null : {
ga_session_id
}, null === session_start ? null : {
session_start
});
var r = {
url: page_location,
id_site: id,
label: e,
debug: debug_mode,
npa: t,
uuid: ibriditics_uuid,
uuid4: ibriditics_uuid4,
params: o,
user_properties: i || null
},
a = new Blob([JSON.stringify(r)], {
type: "application/json"
});
navigator.sendBeacon(beacon_endpoint + dbg, a)
}
};
var _old_dataLayer = window.dataLayer;
window.dataLayer = new IbriditicQueue, window.dataLayer.push.apply(window.dataLayer, _old_dataLayer);
function setCookie(e, i) {
var s = location.hostname.split(".").reverse(),
n = "." + s[1] + "." + s[0],
t = new Date(new Date().getTime() + 31536e6),
o = e + "=" + encodeURIComponent(i) + ";";
o += "expires=" + t.toGMTString() + ";", o += "path=/;", o += "domain=" + n + ";", document.cookie = o
}
function getCookie(e) {
for (var i = e + "=", s = document.cookie.split(";"), n = 0; n < s.length; n++) {
for (var t = s[n];
" " == t.charAt(0);) t = t.substring(1, t.length);
if (0 == t.indexOf(i)) return t.substring(i.length, t.length)
}
return null
}
function deleteCookie(e, i, s) {
getCookie(e) && createCookie(e, "", -1, i, s)
}
function uuid4() {
var e = Date.now();
return window.performance && "function" == typeof window.performance.now && (e += performance.now()), "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(i) {
var s = (e + 16 * Math.random()) % 16 | 0;
return e = Math.floor(e / 16), ("x" === i ? s : 11 & s).toString(16)
})
}
function uuid() {
return Math.floor(2147483647 * Math.random()) + "." + Math.floor(Date.now() / 1e3)
}
null === getCookie("ibriditics_uuid") && (setCookie("ibriditics_uuid", uuid_gaquattro = uuid()), ibriditics_uuid = uuid_gaquattro), null === getCookie("ibriditics_uuid4") && (setCookie("ibriditics_uuid4", uuid_universal = uuid4()), ibriditics_uuid4 = uuid_universal);
const params = new Proxy(new URLSearchParams(document.currentScript.src.substring(document.currentScript.src.indexOf("?"))), {
get: (e, i) => e.get(i)
});
let id = parseInt(params.id),
beacon_endpoint = "https://" + document.currentScript.src.split("/")[2] + "/v1/ibriditics";
class WebSessionCounter {
constructor() {
this.update()
}
get count() {
return Number(getCookie("ib_session_number"))
}
set count(e) {
setCookie("ib_session_number", e)
}
get sessionHit() {
return Number(getCookie("ib_session_hit"))
}
set sessionHit(e) {
setCookie("ib_session_hit", e)
}
get sessionId() {
return Number(getCookie("ib_session_id"))
}
set sessionId(e) {
setCookie("ib_session_id", e)
}
get lastActive() {
let e = decodeURIComponent(getCookie("ib_session_last_active"));
return e ? new Date(e) : new Date
}
set lastActive(e) {
setCookie("ib_session_last_active", e.toISOString())
}
update() {
let e = this.count,
i = this.sessionHit;
console.log("session ->" + this.count), 0 === e || this.isNewSession() ? (this.count = e + 1, this.sessionHit = 0, this.sessionId = parseInt(+new Date / 1e3), this.lastActive = new Date, this.sessionStart = !0, console.log("new session ->" + this.count + " " + this.sessionId)) : (this.sessionStart = !1, this.sessionHit = i + 1)
}
isNewSession() {
let e = this.lastActive,
i = new Date;
return [(i - e) / 1e3 / 60 > 30, i.toDateString() !== e.toDateString(), ].some(e => e)
}
}
ibSess = new WebSessionCounter;
var ibriditics_uuid = getCookie("ibriditics_uuid"),
ibriditics_uuid4 = getCookie("ibriditics_uuid4"),
ga_session_number = ibSess.count,
sequence = ibSess.sessionHit,
ga_session_id = ibSess.sessionId,
session_start = !1;
0 == ibSess.sessionHit && (session_start = !0);
var navigator_info = window.navigator,
screen_info = window.screen,
location_info = document.location;
const user_agent = navigator_info.userAgent;
let page_referrer = document.referrer;
"" == page_referrer && (page_referrer = null);
const screen_resolution = screen_info.height + "x" + screen_info.width,
language = navigator.language || navigator.userLanguage,
page_location = location_info.origin + location_info.pathname + location_info.search,
page_hostname = location_info.origin,
page_title = document.title;
let searchParams = new URLSearchParams(location_info.search);
var debug_mode = "true" === searchParams.get("debug_mode"),
dbg = "";
searchParams.get("dbg") && (dbg = "?dbg=" + searchParams.get("dbg"));
var IbriditicQueue = function() {
this.push = function() {
for (var e = 0; e < arguments.length; e++) try {
"function" == typeof arguments[e] ? arguments[e]() : trackers[arguments[e][0]].call(arguments[e][0], arguments[e][1], arguments[e][2])
} catch (i) {}
}
};
const trackers = {
event: function(e, i = {
params: {}
}) {
let s = 0;
("page_view" == e || "PAGEVIEW" == e) && (s = 1);
let n = i.params,
t = !1,
o = !0;
i.params.non_personalized_ads ? (t = !1, o = !0) : (t = !0, o = !1), delete n.non_personalized_ads;
let r = Object.assign({}, n, null === user_agent ? null : {
user_agent
}, null === page_referrer ? null : {
page_referrer
}, null === screen_resolution ? null : {
screen_resolution
}, null === language ? null : {
language
}, null === page_title ? null : {
page_title
}, null === s ? null : {
engagement_time_msec: s
}, null === ga_session_number ? null : {
ga_session_number
}, null === ga_session_id ? null : {
ga_session_id
}, null === session_start ? null : {
session_start
}, null === sequence ? null : {
sequence
});
user_properties = Object.assign({}, null === language ? null : {
language: {
value: language
}
}, null === t ? null : {
consent: {
value: t
}
}, {
platform: {
value: "web"
}
});
var a = {
url: page_location,
id_site: id,
label: e,
debug: debug_mode,
npa: o,
uuid: ibriditics_uuid,
uuid4: ibriditics_uuid4,
params: r,
user_properties: user_properties
},
u = new Blob([JSON.stringify(a)], {
type: "application/json"
});
navigator.sendBeacon(beacon_endpoint + dbg, u)
},
set: function(e, i = {
params: {}
}) {
let s = i.params,
n = !1,
t = !0;
i.params.non_personalized_ads ? (n = !1, t = !0) : (n = !0, t = !1), delete s.non_personalized_ads;
let o = Object.assign({}, s, null === ga_session_number ? null : {
ga_session_number
}, null === ga_session_id ? null : {
ga_session_id
}, null === session_start ? null : {
session_start
});
var r = {
url: page_location,
id_site: id,
label: e,
debug: debug_mode,
npa: t,
uuid: ibriditics_uuid,
uuid4: ibriditics_uuid4,
params: o,
user_properties: i || null
},
a = new Blob([JSON.stringify(r)], {
type: "application/json"
});
navigator.sendBeacon(beacon_endpoint + dbg, a)
}
};
var _old_dataLayer = window.dataLayer;
window.dataLayer = new IbriditicQueue, window.dataLayer.push.apply(window.dataLayer, _old_dataLayer);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment