Merge pull request #1 from CChannel/meza/update-tracker-script

edit tracker script to work under SSR sites
pull/1644/head
Sergei Meza 2022-10-18 11:12:38 +09:00 committed by GitHub
commit 449e0f4b0f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 31 deletions

View File

@ -1,3 +1,4 @@
/* eslint-disable no-console */
(window => {
const {
screen: { width, height },
@ -84,12 +85,23 @@
});
const getClientIPAddress = () => {
if (ip) return ip;
fetch('https://api64.ipify.org/?format=json')
.then(res => res.json())
.then(data => {
ip = data.ip;
});
return new Promise((res, rej) => {
if (ip) {
res(ip);
return;
}
fetch('https://api64.ipify.org/?format=json')
.then(res => res.json())
.then(data => {
ip = data.ip;
res(ip);
return;
})
.catch(err => {
console.error(err);
rej(err);
});
});
};
const collect = (type, payload) => {
@ -211,36 +223,36 @@
};
/* Global */
getClientIPAddress();
getClientIPAddress().then(() => {
if (!window.umami) {
const umami = eventValue => trackEvent(eventValue);
umami.trackView = trackView;
umami.trackEvent = trackEvent;
if (!window.umami) {
const umami = eventValue => trackEvent(eventValue);
umami.trackView = trackView;
umami.trackEvent = trackEvent;
window.umami = umami;
window.lemonsquare = umami;
}
window.umami = umami;
window.lemonsquare = umami;
}
/* Start */
/* Start */
if (autoTrack && !trackingDisabled()) {
history.pushState = hook(history, 'pushState', handlePush);
history.replaceState = hook(history, 'replaceState', handlePush);
if (autoTrack && !trackingDisabled()) {
history.pushState = hook(history, 'pushState', handlePush);
history.replaceState = hook(history, 'replaceState', handlePush);
const update = () => {
if (document.readyState === 'complete') {
trackView();
const update = () => {
if (document.readyState === 'complete') {
trackView();
if (cssEvents) {
addEvents(document);
observeDocument();
if (cssEvents) {
addEvents(document);
observeDocument();
}
}
}
};
};
document.addEventListener('readystatechange', update, true);
document.addEventListener('readystatechange', update, true);
update();
}
update();
}
});
})(window);

View File

@ -58,7 +58,11 @@ document.head.appendChild(script);
// };
// analytics.subscribe('page_viewed', async event => {
// lemonsquare.trackView(event.url, event.referrer);
// lemonsquare.trackView(
// event.context.location.pathname,
// event.context.document.referrer,
// WEBSITE_ID,
// );
// });
// // product_viewed Event