From bf4de2fbf5bbb39c311ad7fd1458569106df8370 Mon Sep 17 00:00:00 2001 From: Sergei Meza Date: Tue, 18 Oct 2022 11:01:34 +0900 Subject: [PATCH 1/2] 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 From 019343085f0cb2381cb90f52d32497c1ebc6d6d7 Mon Sep 17 00:00:00 2001 From: Sergei Meza Date: Tue, 18 Oct 2022 11:05:25 +0900 Subject: [PATCH 2/2] edit tracker script to work under SSR sites --- tracker/index.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tracker/index.js b/tracker/index.js index 76f7de10..98ed4229 100644 --- a/tracker/index.js +++ b/tracker/index.js @@ -1,3 +1,4 @@ +/* eslint-disable no-console */ (window => { const { screen: { width, height }, @@ -84,7 +85,7 @@ }); const getClientIPAddress = () => { - return new Promise(res => { + return new Promise((res, rej) => { if (ip) { res(ip); return; @@ -95,6 +96,10 @@ ip = data.ip; res(ip); return; + }) + .catch(err => { + console.error(err); + rej(err); }); }); };