Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/analytics/SegmentAnalyticsService.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ class SegmentAnalyticsService {
* @param {*} [traits]
*/
identifyAuthenticatedUser(userId, traits) {
if (this.hasIdentifyBeenCalled) {
return;
}

if (!userId) {
throw new Error('UserId is required for identifyAuthenticatedUser.');
}
Expand All @@ -175,6 +179,10 @@ class SegmentAnalyticsService {
* @returns {Promise} Promise that will resolve once the document readyState is complete
*/
identifyAnonymousUser(traits) { // eslint-disable-line no-unused-vars
if (this.hasIdentifyBeenCalled) {
return Promise.resolve();
}

if (!this.segmentInitialized) {
return Promise.resolve();
}
Expand Down
51 changes: 51 additions & 0 deletions src/analytics/interface.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,30 @@ describe('Analytics', () => {
expect(() => identifyAuthenticatedUser(null))
.toThrowError(new Error('UserId is required for identifyAuthenticatedUser.'));
});

it('should call segment identify once', () => {
const testTraits = { anything: 'Yay!' };
identifyAuthenticatedUser(testUserId, testTraits);
identifyAuthenticatedUser(testUserId, testTraits);

expect(window.analytics.identify.mock.calls.length).toBe(1);
});

it('should call segment identify if hasIdentifyBeenCalled is false', () => {
const testTraits = { anything: 'Yay!' };
service.hasIdentifyBeenCalled = false;
identifyAuthenticatedUser(testUserId, testTraits);

expect(window.analytics.identify).toHaveBeenCalled();
});

it('should not call segment identify if hasIdentifyBeenCalled is true', () => {
const testTraits = { anything: 'Yay!' };
service.hasIdentifyBeenCalled = true;
identifyAuthenticatedUser(testUserId, testTraits);

expect(window.analytics.identify).not.toHaveBeenCalled();
});
});

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

expect(window.analytics.reset.mock.calls.length).toBe(1);
});

it('should call segment reset once', () => {
window.analytics.user = () => ({ id: () => 1 });
const testTraits = { anything: 'Yay!' };
identifyAnonymousUser(testTraits);
identifyAnonymousUser(testTraits);

expect(window.analytics.reset.mock.calls.length).toBe(1);
});

it('should call segment reset if hasIdentifyBeenCalled is false', () => {
window.analytics.user = () => ({ id: () => 2 });
const testTraits = { anything: 'Yay!' };
service.hasIdentifyBeenCalled = false;
identifyAnonymousUser(testTraits);

expect(window.analytics.reset).toHaveBeenCalled();
});

it('should not call segment reset if hasIdentifyBeenCalled is true', () => {
window.analytics.user = () => ({ id: () => 3 });
const testTraits = { anything: 'Yay!' };
service.hasIdentifyBeenCalled = true;
identifyAnonymousUser(testTraits);

expect(window.analytics.reset).not.toHaveBeenCalled();
});
});

function testSendPageAfterIdentify(identifyFunction) {
Expand Down