From 4fca4d0d643eb5a988e5c18dbf53b4a187b27b0b Mon Sep 17 00:00:00 2001 From: Chethan-Fyle <93918979+Chethan-Fyle@users.noreply.github.com> Date: Tue, 3 Sep 2024 13:02:54 +0530 Subject: [PATCH] feat: add and use ENABLE_MIXPANEL and USE_MIXPANEL_PROXY env variable (#3195) --- hooks/utils/prod-environment.js | 3 +- .../core/services/router-auth.service.spec.ts | 9 +++++ src/app/core/services/router-auth.service.ts | 5 ++- src/app/core/services/tracking.service.ts | 35 +++++++++++++------ src/environments/environment.ts | 3 +- 5 files changed, 41 insertions(+), 14 deletions(-) diff --git a/hooks/utils/prod-environment.js b/hooks/utils/prod-environment.js index 7a2d31b181..3b76b79aa7 100644 --- a/hooks/utils/prod-environment.js +++ b/hooks/utils/prod-environment.js @@ -17,6 +17,7 @@ export const environment = { LIVE_UPDATE_APP_VERSION: '${process.env.LIVE_UPDATE_APP_VERSION}', SMARTLOOK_API_KEY: '${process.env.SMARTLOOK_API_KEY}', MIXPANEL_PROJECT_TOKEN: '${process.env.MIXPANEL_PROJECT_TOKEN}', - MIXPANEL_PROXY_URL: '${process.env.MIXPANEL_PROXY_URL}', + USE_MIXPANEL_PROXY: '${process.env.USE_MIXPANEL_PROXY}', + ENABLE_MIXPANEL: '${process.env.ENABLE_MIXPANEL}' }; ` \ No newline at end of file diff --git a/src/app/core/services/router-auth.service.spec.ts b/src/app/core/services/router-auth.service.spec.ts index 51939ce897..5901f93da6 100644 --- a/src/app/core/services/router-auth.service.spec.ts +++ b/src/app/core/services/router-auth.service.spec.ts @@ -15,6 +15,7 @@ import { apiAuthRes, authResData1 } from '../mock-data/auth-reponse.data'; import { ExpenseAggregationService } from './expense-aggregation.service'; import { SpenderService } from './platform/v1/spender/spender.service'; import { ApproverService } from './platform/v1/approver/approver.service'; +import { TrackingService } from './tracking.service'; describe('RouterAuthService', () => { let routerAuthService: RouterAuthService; @@ -31,6 +32,7 @@ describe('RouterAuthService', () => { let expenseAggregationService: jasmine.SpyObj; let spenderService: jasmine.SpyObj; let approverService: jasmine.SpyObj; + let trackingService: jasmine.SpyObj; const access_token = 'eyJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE2Nzk5MDQ0NTQsImlzcyI6IkZ5bGVBcHAiLCJ1c2VyX2lkIjoidXN2S0E0WDhVZ2NyIiwib3JnX3VzZXJfaWQiOiJvdVg4ZHdzYkxDTHYiLCJvcmdfaWQiOiJvck5WdGhUbzJaeW8iLCJyb2xlcyI6IltcIkFETUlOXCIsXCJBUFBST1ZFUlwiLFwiRllMRVJcIixcIkhPUFwiLFwiSE9EXCIsXCJPV05FUlwiXSIsInNjb3BlcyI6IltdIiwiYWxsb3dlZF9DSURScyI6IltdIiwidmVyc2lvbiI6IjMiLCJjbHVzdGVyX2RvbWFpbiI6IlwiaHR0cHM6Ly9zdGFnaW5nLmZ5bGUudGVjaFwiIiwiZXhwIjoxNjc5OTA4MDU0fQ.z3i-MqE3NNyxPEvWFCSr3q58rLXn3LZcIBskW9BLN48'; @@ -59,6 +61,7 @@ describe('RouterAuthService', () => { const expenseAggregationServiceSpy = jasmine.createSpyObj('ExpenseAggregationService', ['setRoot']); const spenderServiceSpy = jasmine.createSpyObj('SpenderService', ['setRoot']); const approverServiceSpy = jasmine.createSpyObj('ApproverService', ['setRoot']); + const trackingServiceSpy = jasmine.createSpyObj('TrackingService', ['setRoot']); TestBed.configureTestingModule({ providers: [ @@ -115,6 +118,10 @@ describe('RouterAuthService', () => { provide: ApproverService, useValue: approverServiceSpy, }, + { + provide: TrackingService, + useValue: trackingServiceSpy, + }, ], }); routerAuthService = TestBed.inject(RouterAuthService); @@ -135,6 +142,7 @@ describe('RouterAuthService', () => { expenseAggregationService = TestBed.inject(ExpenseAggregationService) as jasmine.SpyObj; spenderService = TestBed.inject(SpenderService) as jasmine.SpyObj; approverService = TestBed.inject(ApproverService) as jasmine.SpyObj; + trackingService = TestBed.inject(TrackingService) as jasmine.SpyObj; }); it('should be created', () => { @@ -169,6 +177,7 @@ describe('RouterAuthService', () => { expect(expenseAggregationService.setRoot).toHaveBeenCalledOnceWith(domain); expect(spenderService.setRoot).toHaveBeenCalledOnceWith(domain); expect(approverService.setRoot).toHaveBeenCalledOnceWith(domain); + expect(trackingService.setRoot).toHaveBeenCalledOnceWith(domain); done(); }); }); diff --git a/src/app/core/services/router-auth.service.ts b/src/app/core/services/router-auth.service.ts index 6c8efb35f8..e82085f13e 100644 --- a/src/app/core/services/router-auth.service.ts +++ b/src/app/core/services/router-auth.service.ts @@ -17,6 +17,7 @@ import { SpenderService } from './platform/v1/spender/spender.service'; import { ApproverService } from './platform/v1/approver/approver.service'; import { EmailExistsResponse } from '../models/email-exists-response.model'; import { ResendEmailVerification } from '../models/resend-email-verification.model'; +import { TrackingService } from './tracking.service'; @Injectable({ providedIn: 'root', @@ -35,7 +36,8 @@ export class RouterAuthService { private spenderPlatformV1ApiService: SpenderPlatformV1ApiService, private expenseAggregationService: ExpenseAggregationService, private spenderService: SpenderService, - private approverService: ApproverService + private approverService: ApproverService, + private trackingService: TrackingService ) {} checkEmailExists(email: string): Observable { @@ -66,6 +68,7 @@ export class RouterAuthService { this.expenseAggregationService.setRoot(domain); this.spenderService.setRoot(domain); this.approverService.setRoot(domain); + this.trackingService.setRoot(domain); await this.tokenService.setClusterDomain(domain); } diff --git a/src/app/core/services/tracking.service.ts b/src/app/core/services/tracking.service.ts index fde5b195eb..9e872b0296 100644 --- a/src/app/core/services/tracking.service.ts +++ b/src/app/core/services/tracking.service.ts @@ -46,19 +46,32 @@ import mixpanel, { Config } from 'mixpanel-browser'; export class TrackingService { identityEmail = null; - constructor(private authService: AuthService, private deviceService: DeviceService) { - try { - const config: Partial = { - debug: false, - track_pageview: false, - persistence: 'localStorage', - }; + ROOT_ENDPOINT: string; - if (environment.MIXPANEL_PROXY_URL) { - config.api_host = environment.MIXPANEL_PROXY_URL; - } + constructor(private authService: AuthService, private deviceService: DeviceService) {} - mixpanel.init(environment.MIXPANEL_PROJECT_TOKEN, config); + setRoot(rootUrl: string): void { + this.ROOT_ENDPOINT = rootUrl; + this.initializeMixpanel(); + } + + initializeMixpanel(): void { + try { + const enableMixpanel = environment.ENABLE_MIXPANEL; + if (enableMixpanel === 'true') { + const config: Partial = { + debug: false, + track_pageview: false, + persistence: 'localStorage', + }; + + const useMixpanelProxy = environment.USE_MIXPANEL_PROXY; + if (useMixpanelProxy === 'true' && this.ROOT_ENDPOINT) { + config.api_host = this.ROOT_ENDPOINT + '/mixpanel'; + } + + mixpanel.init(environment.MIXPANEL_PROJECT_TOKEN, config); + } } catch (e) {} } diff --git a/src/environments/environment.ts b/src/environments/environment.ts index 5cccde7e79..03f30a5082 100644 --- a/src/environments/environment.ts +++ b/src/environments/environment.ts @@ -18,7 +18,8 @@ export const environment = { LIVE_UPDATE_APP_VERSION: '', SMARTLOOK_API_KEY: '', MIXPANEL_PROJECT_TOKEN: '', - MIXPANEL_PROXY_URL: '', + USE_MIXPANEL_PROXY: '', + ENABLE_MIXPANEL: '', }; /*