From dcb968676fca9c39df4cc17dd82c6417cc768c64 Mon Sep 17 00:00:00 2001 From: crisnicandrei <62384997+crisnicandrei@users.noreply.github.com> Date: Thu, 9 Nov 2023 12:11:07 +0200 Subject: [PATCH 1/2] PEr-9396 Set a new value in the mixpanel identify method. Add new mixpanel calls to the authentication flow + tests --- .../services/account/account.service.spec.ts | 68 ++++++++++++++++++- .../services/account/account.service.ts | 12 +++- .../services/mixpanel/mixpanel.service.ts | 1 + 3 files changed, 79 insertions(+), 2 deletions(-) diff --git a/src/app/shared/services/account/account.service.spec.ts b/src/app/shared/services/account/account.service.spec.ts index 7c70748c9..3fd4832ac 100644 --- a/src/app/shared/services/account/account.service.spec.ts +++ b/src/app/shared/services/account/account.service.spec.ts @@ -1,4 +1,5 @@ /* @format */ +import { CookieService } from 'ngx-cookie-service'; import { TestBed, inject } from '@angular/core/testing'; import { Router } from '@angular/router'; import { Shallow } from 'shallow-render'; @@ -67,6 +68,33 @@ describe('AccountService', () => { observer.complete(); }); }, + logIn: ( + email: string, + password: string, + rememberMe: boolean, + keepLoggedIn: boolean + ) => { + return new Observable((observer) => { + observer.next( + new AuthResponse({ + isSuccessful: true, + Results: [ + { + data: [ + { + AccountVO: { + primaryEmail: 'test@permanent.org', + fullName: 'Test User', + }, + }, + ], + }, + ], + }) + ); + observer.complete(); + }); + }, }, }) .mock(Router, { @@ -89,7 +117,8 @@ describe('AccountService', () => { }) .mock(EditService, { deleteItems: (items: any[]) => Promise.resolve(true), - }); + }) + .mock(CookieService, { set: (key: string, value: string) => {} }); }); it('should be created', () => { @@ -137,6 +166,7 @@ describe('AccountService', () => { it('should handle successful email verification', async () => { const { instance } = shallow.createService(); + const trackerSpy = spyOn(instance, 'trackAuthWithMixpanel'); const account = new AccountVO({ primaryEmail: 'test@permanent.org', @@ -150,10 +180,12 @@ describe('AccountService', () => { await instance.verifyEmail('sampleToken'); expect(instance.getAccount().emailStatus).toBe('status.auth.verified'); expect(instance.getAccount().keepLoggedIn).toBeTrue(); + expect(trackerSpy).toHaveBeenCalled() }); it('should handle successful phone verification', async () => { const { instance } = shallow.createService(); + const trackerSpy = spyOn(instance, 'trackAuthWithMixpanel'); const account = new AccountVO({ primaryEmail: 'test@permanent.org', @@ -167,6 +199,7 @@ describe('AccountService', () => { await instance.verifyEmail('sampleToken'); expect(instance.getAccount().phoneStatus).toBe('status.auth.verified'); expect(instance.getAccount().keepLoggedIn).toBeTrue(); + expect(trackerSpy).toHaveBeenCalled() }); it('should update the account storage when a file is uploaded successfully', async () => { const { instance, inject } = shallow.createService(); @@ -182,6 +215,39 @@ describe('AccountService', () => { await instance.deductAccountStorage(200); expect(instance.getAccount().spaceLeft).toEqual(99800); }); + + it('should send the account data to mixpanel after signing up', async () => { + const { instance, inject } = shallow.createService(); + const apiService = inject(ApiService); + const trackerSpy = spyOn(instance, 'trackAuthWithMixpanel'); + const account = await instance.signUp( + 'test@permanent.org', + 'Test User', + 'password123', + 'password123', + true, + true, + '', + '', + true + ); + + expect(trackerSpy).toHaveBeenCalled(); + }); + + it('should send the account data to mixpanel after logging in', async () => { + const { instance, inject } = shallow.createService(); + const apiService = inject(ApiService); + const trackerSpy = spyOn(instance, 'trackAuthWithMixpanel'); + const account = await instance.logIn( + 'test@permanent.org', + 'password123', + true, + true + ); + + expect(trackerSpy).toHaveBeenCalled(); + }); it('should add storage back after deleting an item', async () => { const { instance, inject } = shallow.createService(); const editService = inject(EditService); diff --git a/src/app/shared/services/account/account.service.ts b/src/app/shared/services/account/account.service.ts index 5c3d0ab72..c9271aea7 100644 --- a/src/app/shared/services/account/account.service.ts +++ b/src/app/shared/services/account/account.service.ts @@ -360,6 +360,7 @@ export class AccountService { newAccount.isNew = currentAccount.isNew; } this.setAccount(newAccount); + this.trackAuthWithMixpanel('Sign In', newAccount); if (response.getArchiveVO()?.archiveId) { this.setArchive(response.getArchiveVO()); } @@ -426,6 +427,7 @@ export class AccountService { keepLoggedIn, }); this.setAccount(newAccount); + this.trackAuthWithMixpanel('Verify multi-factor', newAccount); const authToken = response.getAuthToken()?.value; if (authToken) { @@ -454,6 +456,7 @@ export class AccountService { keepLoggedIn, }); this.setAccount(account); + this.trackAuthWithMixpanel('Verify email', account); return response; } else { throw response; @@ -567,7 +570,7 @@ export class AccountService { }); newAccount.isNew = true; this.setAccount(newAccount); - this.mixpanel.track('Sign up', { accountId: newAccount.accountId }); + this.trackAuthWithMixpanel('Sign up', newAccount); return newAccount; }) ) @@ -636,4 +639,11 @@ export class AccountService { this.setAccount(newAccount); this.accountStorageUpdate.next(newAccount); } + + public trackAuthWithMixpanel(action: string, accountData: AccountVO): void { + this.mixpanel.track(action, { + accountId: accountData.accountId, + email: accountData.primaryEmail, + }); + } } diff --git a/src/app/shared/services/mixpanel/mixpanel.service.ts b/src/app/shared/services/mixpanel/mixpanel.service.ts index 3918bdaeb..47dc7329e 100644 --- a/src/app/shared/services/mixpanel/mixpanel.service.ts +++ b/src/app/shared/services/mixpanel/mixpanel.service.ts @@ -33,6 +33,7 @@ export class MixpanelService { : `${account.accountId}`; mixpanel.identify(mixpanelIdentifier); mixpanel.people.set({ + $email: account.primaryEmail, accountId: `${account.accountId}`, environment: environment.environment, }); From 3d0db5d4da08f28ddcdf19d669264e01efec290d Mon Sep 17 00:00:00 2001 From: crisnicandrei <62384997+crisnicandrei@users.noreply.github.com> Date: Thu, 9 Nov 2023 15:00:20 +0200 Subject: [PATCH 2/2] Prettier --- src/app/shared/services/account/account.service.spec.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/app/shared/services/account/account.service.spec.ts b/src/app/shared/services/account/account.service.spec.ts index 3fd4832ac..a17d360cd 100644 --- a/src/app/shared/services/account/account.service.spec.ts +++ b/src/app/shared/services/account/account.service.spec.ts @@ -180,7 +180,7 @@ describe('AccountService', () => { await instance.verifyEmail('sampleToken'); expect(instance.getAccount().emailStatus).toBe('status.auth.verified'); expect(instance.getAccount().keepLoggedIn).toBeTrue(); - expect(trackerSpy).toHaveBeenCalled() + expect(trackerSpy).toHaveBeenCalled(); }); it('should handle successful phone verification', async () => { @@ -199,7 +199,7 @@ describe('AccountService', () => { await instance.verifyEmail('sampleToken'); expect(instance.getAccount().phoneStatus).toBe('status.auth.verified'); expect(instance.getAccount().keepLoggedIn).toBeTrue(); - expect(trackerSpy).toHaveBeenCalled() + expect(trackerSpy).toHaveBeenCalled(); }); it('should update the account storage when a file is uploaded successfully', async () => { const { instance, inject } = shallow.createService();