From 16d81f6e6d8e5dd5005552ae9c02b0ed2d93640a Mon Sep 17 00:00:00 2001 From: Dillon Dumesnil Date: Mon, 8 Mar 2021 11:39:45 -0700 Subject: [PATCH] AA-513: Reset segment state if anon user and there is a segment user id If we are seeing an anonymous user, but the segment user id is still set, we believe the segment user id is coming from a different user on the same machine. This will make sure we clear out that storage and then the indentify call will make a new anonymous id --- cms/templates/widgets/segment-io-footer.html | 16 ++++++++++++++++ lms/templates/widgets/segment-io-footer.html | 16 ++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/cms/templates/widgets/segment-io-footer.html b/cms/templates/widgets/segment-io-footer.html index 896e391fe3..a57a10a1ba 100644 --- a/cms/templates/widgets/segment-io-footer.html +++ b/cms/templates/widgets/segment-io-footer.html @@ -30,6 +30,22 @@ else { onLoadCallback(); } + % else: + // If we do not have an authenticated user, but Segment has a user id, + // reset the Segment user state so we start tracking fresh. + // This has to be wrapped in the document.readyState logic because the analytics.user() + // function isn't available until the analytics.js package has finished initializing. + if (document.readyState === 'complete') { + if (analytics.user().id()) { + analytics.reset(); + } + } else { + document.addEventListener('readystatechange', event => { + if (event.target.readyState === 'complete' && analytics.user().id()) { + analytics.reset(); + } + }); + } % endif diff --git a/lms/templates/widgets/segment-io-footer.html b/lms/templates/widgets/segment-io-footer.html index 4f12ffd99d..60e2ffb1c8 100644 --- a/lms/templates/widgets/segment-io-footer.html +++ b/lms/templates/widgets/segment-io-footer.html @@ -22,6 +22,22 @@ } ); }); + % else: + // If we do not have an authenticated user, but Segment has a user id, + // reset the Segment user state so we start tracking fresh. + // This has to be wrapped in the document.readyState logic because the analytics.user() + // function isn't available until the analytics.js package has finished initializing. + if (document.readyState === 'complete') { + if (analytics.user().id()) { + analytics.reset(); + } + } else { + document.addEventListener('readystatechange', event => { + if (event.target.readyState === 'complete' && analytics.user().id()) { + analytics.reset(); + } + }); + } % endif