From bf4de2fbf5bbb39c311ad7fd1458569106df8370 Mon Sep 17 00:00:00 2001 From: Sergei Meza Date: Tue, 18 Oct 2022 11:01:34 +0900 Subject: [PATCH] edit tracker script to work under SSR sites --- tracker/index.js | 67 +++++++++++++++++++++++++--------------------- tracker/shopify.js | 6 ++++- 2 files changed, 42 insertions(+), 31 deletions(-) diff --git a/tracker/index.js b/tracker/index.js index 770b4f1a..76f7de10 100644 --- a/tracker/index.js +++ b/tracker/index.js @@ -84,12 +84,19 @@ }); 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 => { + if (ip) { + res(ip); + return; + } + fetch('https://api64.ipify.org/?format=json') + .then(res => res.json()) + .then(data => { + ip = data.ip; + res(ip); + return; + }); + }); }; const collect = (type, payload) => { @@ -211,36 +218,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); diff --git a/tracker/shopify.js b/tracker/shopify.js index 90c2632e..3d561e5e 100644 --- a/tracker/shopify.js +++ b/tracker/shopify.js @@ -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