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
45 lines
1.7 KiB
HTML
45 lines
1.7 KiB
HTML
<%page expression_filter="h"/>
|
|
<%! from openedx.core.djangolib.js_utils import js_escaped_string %>
|
|
|
|
% if settings.LMS_SEGMENT_KEY:
|
|
<!-- begin segment footer -->
|
|
<script type="text/javascript">
|
|
% if user.is_authenticated:
|
|
$(window).load(function() {
|
|
analytics.identify(
|
|
"${ user.id | n, js_escaped_string }",
|
|
{
|
|
email: "${ user.email | n, js_escaped_string }",
|
|
username: "${ user.username | n, js_escaped_string }"
|
|
},
|
|
{
|
|
integrations: {
|
|
// Disable MailChimp because we don't want to update the user's email
|
|
// and username in MailChimp on every page load. We only need to capture
|
|
// this data on registration/activation.
|
|
MailChimp: false
|
|
}
|
|
}
|
|
);
|
|
});
|
|
% 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 -->
|
|
% endif
|