Skip to content

Commit 7560fb3

Browse files
author
Adam Stankiewicz
authored
fix: prevent JS error when calling analytics.user() (#164)
1 parent dfe677f commit 7560fb3

4 files changed

Lines changed: 10 additions & 15 deletions

File tree

.env.development

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ STUDIO_BASE_URL=http://localhost:18010
1414
MARKETING_SITE_BASE_URL=http://localhost:18000
1515
ORDER_HISTORY_URL=http://localhost:1996/orders
1616
REFRESH_ACCESS_TOKEN_ENDPOINT=http://localhost:18000/login_refresh
17-
SEGMENT_KEY=ul
17+
SEGMENT_KEY=null
1818
SITE_NAME=Open edX
1919
USER_INFO_COOKIE_NAME=edx-user-info
2020
LOGO_URL=https://edx-cdn.org/v3/default/logo.svg

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ coverage
55
dist
66
node_modules
77
/docs/api
8+
.env.private

src/analytics/SegmentAnalyticsService.js

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -158,28 +158,17 @@ class SegmentAnalyticsService {
158158
identifyAnonymousUser(traits) {
159159
// if we do not have an authenticated user (indicated by being in this method),
160160
// but we still have a user id associated in segment, reset the local segment state
161-
// This has to be wrapped in the document.readyState logic because the analytics.user()
161+
// This has to be wrapped in the analytics.ready() callback because the analytics.user()
162162
// function isn't available until the analytics.js package has finished initializing.
163163
return new Promise((resolve, reject) => { // eslint-disable-line no-unused-vars
164-
if (document.readyState === 'complete') {
164+
global.analytics.ready(() => {
165165
if (global.analytics.user().id()) {
166166
global.analytics.reset();
167167
}
168168
global.analytics.identify(traits);
169169
this.hasIdentifyBeenCalled = true;
170170
resolve();
171-
} else {
172-
document.addEventListener('readystatechange', event => {
173-
if (event.target.readyState === 'complete') {
174-
if (global.analytics.user().id()) {
175-
global.analytics.reset();
176-
}
177-
global.analytics.identify(traits);
178-
this.hasIdentifyBeenCalled = true;
179-
resolve();
180-
}
181-
});
182-
}
171+
});
183172
});
184173
}
185174

src/analytics/interface.test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ beforeEach(() => {
4646
window.analytics.page = jest.fn();
4747
window.analytics.track = jest.fn();
4848
window.analytics.reset = jest.fn();
49+
window.analytics.ready = jest.fn((callback) => {
50+
if (callback) {
51+
callback();
52+
}
53+
});
4954
});
5055

5156
describe('analytics sendTrackingLogEvent', () => {

0 commit comments

Comments
 (0)