Merge pull request #26902 from edx/ddumesnil/segment-logout-aa-513

AA-513: Reset segment state if anon user and there is a segment user id
This commit is contained in:
Dillon Dumesnil
2021-03-10 11:08:16 -08:00
committed by GitHub
2 changed files with 32 additions and 0 deletions

View File

@@ -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
</script>
<!-- end segment footer -->

View File

@@ -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
</script>
<!-- end segment footer -->