diff --git a/libs/payments/metrics/src/lib/glean/glean.config.ts b/libs/payments/metrics/src/lib/glean/glean.config.ts index f3972b0de30..0ea9fd99498 100644 --- a/libs/payments/metrics/src/lib/glean/glean.config.ts +++ b/libs/payments/metrics/src/lib/glean/glean.config.ts @@ -1,7 +1,9 @@ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -import { IsEnum, IsString } from 'class-validator'; +import { faker } from '@faker-js/faker'; +import { Provider } from '@nestjs/common'; +import { IsBoolean, IsEnum, IsString } from 'class-validator'; enum GleanChannel { Development = 'development', @@ -10,6 +12,9 @@ enum GleanChannel { } export class PaymentsGleanConfig { + @IsBoolean() + enabled!: boolean; + @IsString() applicationId!: string; @@ -22,3 +27,16 @@ export class PaymentsGleanConfig { @IsString() loggerAppName!: string; } + +export const MockPaymentsGleanConfig = { + enabled: true, + applicationId: faker.string.uuid(), + version: '0.0.1', + channel: GleanChannel.Development, + loggerAppName: 'payments-next-test', +} satisfies PaymentsGleanConfig; + +export const MockPaymentsGleanConfigProvider = { + provide: PaymentsGleanConfig, + useValue: MockPaymentsGleanConfig, +} satisfies Provider; diff --git a/libs/payments/metrics/src/lib/glean/glean.manager.spec.ts b/libs/payments/metrics/src/lib/glean/glean.manager.spec.ts index a2709836f31..504e4714130 100644 --- a/libs/payments/metrics/src/lib/glean/glean.manager.spec.ts +++ b/libs/payments/metrics/src/lib/glean/glean.manager.spec.ts @@ -10,6 +10,10 @@ import { } from './glean.factory'; import { PaymentsGleanProvider } from './glean.types'; import { MockPaymentsGleanFactory } from './glean.test-provider'; +import { + MockPaymentsGleanConfigProvider, + PaymentsGleanConfig, +} from './glean.config'; const mockCommonMetricsData = { commonMetricsData: CommonMetricsFactory(), @@ -26,7 +30,11 @@ describe('PaymentsGleanManager', () => { beforeEach(async () => { const moduleRef = await Test.createTestingModule({ - providers: [MockPaymentsGleanFactory, PaymentsGleanManager], + providers: [ + MockPaymentsGleanFactory, + PaymentsGleanManager, + MockPaymentsGleanConfigProvider, + ], }).compile(); paymentsGleanManager = moduleRef.get(PaymentsGleanManager); @@ -156,4 +164,70 @@ describe('PaymentsGleanManager', () => { }); }); }); + + describe('enabled is false', () => { + { + let paymentsGleanManager: PaymentsGleanManager; + let paymentsGleanServerEventsLogger: any; + + beforeEach(async () => { + const moduleRef = await Test.createTestingModule({ + providers: [ + MockPaymentsGleanFactory, + PaymentsGleanManager, + MockPaymentsGleanConfigProvider, + { + provide: PaymentsGleanConfig, + useValue: { + enabled: false, + }, + }, + ], + }).compile(); + + paymentsGleanManager = moduleRef.get(PaymentsGleanManager); + paymentsGleanServerEventsLogger = moduleRef.get(PaymentsGleanProvider); + jest.spyOn(paymentsGleanServerEventsLogger, 'recordPaySetupView'); + jest.spyOn(paymentsGleanServerEventsLogger, 'recordPaySetupEngage'); + jest.spyOn(paymentsGleanServerEventsLogger, 'recordPaySetupSubmit'); + jest.spyOn(paymentsGleanServerEventsLogger, 'recordPaySetupSuccess'); + jest.spyOn(paymentsGleanServerEventsLogger, 'recordPaySetupFail'); + }); + + it('recordFxaPaySetupView', () => { + paymentsGleanManager.recordFxaPaySetupView(mockCommonMetricsData); + expect( + paymentsGleanServerEventsLogger.recordPaySetupView + ).not.toHaveBeenCalled(); + }); + + it('recordFxaPaySetupEngage', () => { + paymentsGleanManager.recordFxaPaySetupEngage(mockCommonMetricsData); + expect( + paymentsGleanServerEventsLogger.recordPaySetupEngage + ).not.toHaveBeenCalled(); + }); + + it('recordFxaPaySetupSubmit', () => { + paymentsGleanManager.recordFxaPaySetupSubmit(mockCommonMetricsData); + expect( + paymentsGleanServerEventsLogger.recordPaySetupSubmit + ).not.toHaveBeenCalled(); + }); + + it('recordFxaPaySetupSuccess', () => { + paymentsGleanManager.recordFxaPaySetupSuccess(mockCommonMetricsData); + expect( + paymentsGleanServerEventsLogger.recordPaySetupSuccess + ).not.toHaveBeenCalled(); + }); + + it('recordFxaPaySetupFail', () => { + paymentsGleanManager.recordFxaPaySetupFail(mockCommonMetricsData); + expect( + paymentsGleanServerEventsLogger.recordPaySetupFail + ).not.toHaveBeenCalled(); + }); + } + }); }); diff --git a/libs/payments/metrics/src/lib/glean/glean.manager.ts b/libs/payments/metrics/src/lib/glean/glean.manager.ts index 1212abf38eb..33d81dbe47a 100644 --- a/libs/payments/metrics/src/lib/glean/glean.manager.ts +++ b/libs/payments/metrics/src/lib/glean/glean.manager.ts @@ -15,12 +15,14 @@ import { mapUtm } from './utils/mapUtm'; import { mapSubscription } from './utils/mapSubscription'; import { mapRelyingParty } from './utils/mapRelyingParty'; import { normalizeGleanFalsyValues } from './utils/normalizeGleanFalsyValues'; +import { PaymentsGleanConfig } from './glean.config'; @Injectable() export class PaymentsGleanManager { constructor( @Inject(PaymentsGleanProvider) - private paymentsGleanServerEventsLogger: PaymentsGleanServerEventsLogger + private paymentsGleanServerEventsLogger: PaymentsGleanServerEventsLogger, + private paymentsGleanConfig: PaymentsGleanConfig ) {} recordFxaPaySetupView(metrics: { @@ -28,9 +30,11 @@ export class PaymentsGleanManager { cartMetricsData: CartMetrics; cmsMetricsData: CmsMetricsData; }) { - this.paymentsGleanServerEventsLogger.recordPaySetupView( - this.populateCommonMetrics(metrics) - ); + if (this.paymentsGleanConfig.enabled) { + this.paymentsGleanServerEventsLogger.recordPaySetupView( + this.populateCommonMetrics(metrics) + ); + } } recordFxaPaySetupEngage(metrics: { @@ -38,9 +42,11 @@ export class PaymentsGleanManager { cartMetricsData: CartMetrics; cmsMetricsData: CmsMetricsData; }) { - this.paymentsGleanServerEventsLogger.recordPaySetupEngage( - this.populateCommonMetrics(metrics) - ); + if (this.paymentsGleanConfig.enabled) { + this.paymentsGleanServerEventsLogger.recordPaySetupEngage( + this.populateCommonMetrics(metrics) + ); + } } recordFxaPaySetupSubmit( @@ -51,10 +57,13 @@ export class PaymentsGleanManager { }, paymentProvider?: PaymentProvidersType ) { - this.paymentsGleanServerEventsLogger.recordPaySetupSubmit({ - ...this.populateCommonMetrics(metrics), - subscription_payment_provider: normalizeGleanFalsyValues(paymentProvider), - }); + if (this.paymentsGleanConfig.enabled) { + this.paymentsGleanServerEventsLogger.recordPaySetupSubmit({ + ...this.populateCommonMetrics(metrics), + subscription_payment_provider: + normalizeGleanFalsyValues(paymentProvider), + }); + } } recordFxaPaySetupSuccess( @@ -67,10 +76,13 @@ export class PaymentsGleanManager { ) { const commonMetrics = this.populateCommonMetrics(metrics); - this.paymentsGleanServerEventsLogger.recordPaySetupSuccess({ - ...commonMetrics, - subscription_payment_provider: normalizeGleanFalsyValues(paymentProvider), - }); + if (this.paymentsGleanConfig.enabled) { + this.paymentsGleanServerEventsLogger.recordPaySetupSuccess({ + ...commonMetrics, + subscription_payment_provider: + normalizeGleanFalsyValues(paymentProvider), + }); + } } recordFxaPaySetupFail( @@ -83,10 +95,13 @@ export class PaymentsGleanManager { ) { const commonMetrics = this.populateCommonMetrics(metrics); - this.paymentsGleanServerEventsLogger.recordPaySetupFail({ - ...commonMetrics, - subscription_payment_provider: normalizeGleanFalsyValues(paymentProvider), - }); + if (this.paymentsGleanConfig.enabled) { + this.paymentsGleanServerEventsLogger.recordPaySetupFail({ + ...commonMetrics, + subscription_payment_provider: + normalizeGleanFalsyValues(paymentProvider), + }); + } } private populateCommonMetrics(metrics: {