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,11 +85,22 @@
}); });
const getClientIPAddress = () => { const getClientIPAddress = () => {
if (ip) return ip; return new Promise((res, rej) => {
if (ip) {
res(ip);
return;
}
fetch('https://api64.ipify.org/?format=json') fetch('https://api64.ipify.org/?format=json')
.then(res => res.json()) .then(res => res.json())
.then(data => { .then(data => {
ip = data.ip; ip = data.ip;
res(ip);
return;
})
.catch(err => {
console.error(err);
rej(err);
});
}); });
}; };
@ -211,8 +223,7 @@
}; };
/* Global */ /* Global */
getClientIPAddress(); getClientIPAddress().then(() => {
if (!window.umami) { if (!window.umami) {
const umami = eventValue => trackEvent(eventValue); const umami = eventValue => trackEvent(eventValue);
umami.trackView = trackView; umami.trackView = trackView;
@ -243,4 +254,5 @@
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