Skip to content

Commit 1590bd8

Browse files
fix: avoid multiple segment identify calls (#458)
1 parent 0ecb72b commit 1590bd8

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

src/analytics/SegmentAnalyticsService.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,10 @@ class SegmentAnalyticsService {
157157
* @param {*} [traits]
158158
*/
159159
identifyAuthenticatedUser(userId, traits) {
160+
if (this.hasIdentifyBeenCalled) {
161+
return;
162+
}
163+
160164
if (!userId) {
161165
throw new Error('UserId is required for identifyAuthenticatedUser.');
162166
}
@@ -175,6 +179,10 @@ class SegmentAnalyticsService {
175179
* @returns {Promise} Promise that will resolve once the document readyState is complete
176180
*/
177181
identifyAnonymousUser(traits) { // eslint-disable-line no-unused-vars
182+
if (this.hasIdentifyBeenCalled) {
183+
return Promise.resolve();
184+
}
185+
178186
if (!this.segmentInitialized) {
179187
return Promise.resolve();
180188
}

src/analytics/interface.test.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,30 @@ describe('Analytics', () => {
112112
expect(() => identifyAuthenticatedUser(null))
113113
.toThrowError(new Error('UserId is required for identifyAuthenticatedUser.'));
114114
});
115+
116+
it('should call segment identify once', () => {
117+
const testTraits = { anything: 'Yay!' };
118+
identifyAuthenticatedUser(testUserId, testTraits);
119+
identifyAuthenticatedUser(testUserId, testTraits);
120+
121+
expect(window.analytics.identify.mock.calls.length).toBe(1);
122+
});
123+
124+
it('should call segment identify if hasIdentifyBeenCalled is false', () => {
125+
const testTraits = { anything: 'Yay!' };
126+
service.hasIdentifyBeenCalled = false;
127+
identifyAuthenticatedUser(testUserId, testTraits);
128+
129+
expect(window.analytics.identify).toHaveBeenCalled();
130+
});
131+
132+
it('should not call segment identify if hasIdentifyBeenCalled is true', () => {
133+
const testTraits = { anything: 'Yay!' };
134+
service.hasIdentifyBeenCalled = true;
135+
identifyAuthenticatedUser(testUserId, testTraits);
136+
137+
expect(window.analytics.identify).not.toHaveBeenCalled();
138+
});
115139
});
116140

117141
describe('analytics identifyAnonymousUser', () => {
@@ -130,6 +154,33 @@ describe('Analytics', () => {
130154

131155
expect(window.analytics.reset.mock.calls.length).toBe(1);
132156
});
157+
158+
it('should call segment reset once', () => {
159+
window.analytics.user = () => ({ id: () => 1 });
160+
const testTraits = { anything: 'Yay!' };
161+
identifyAnonymousUser(testTraits);
162+
identifyAnonymousUser(testTraits);
163+
164+
expect(window.analytics.reset.mock.calls.length).toBe(1);
165+
});
166+
167+
it('should call segment reset if hasIdentifyBeenCalled is false', () => {
168+
window.analytics.user = () => ({ id: () => 2 });
169+
const testTraits = { anything: 'Yay!' };
170+
service.hasIdentifyBeenCalled = false;
171+
identifyAnonymousUser(testTraits);
172+
173+
expect(window.analytics.reset).toHaveBeenCalled();
174+
});
175+
176+
it('should not call segment reset if hasIdentifyBeenCalled is true', () => {
177+
window.analytics.user = () => ({ id: () => 3 });
178+
const testTraits = { anything: 'Yay!' };
179+
service.hasIdentifyBeenCalled = true;
180+
identifyAnonymousUser(testTraits);
181+
182+
expect(window.analytics.reset).not.toHaveBeenCalled();
183+
});
133184
});
134185

135186
function testSendPageAfterIdentify(identifyFunction) {

0 commit comments

Comments
 (0)