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

View File

@ -58,7 +58,11 @@ document.head.appendChild(script);
// }; // };
// analytics.subscribe('page_viewed', async event => { // 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 // // product_viewed Event