From be6ca6f858b0b994e2ce1506b49f47315e44ab24 Mon Sep 17 00:00:00 2001 From: Justin Chou <111300235+justinchou-google@users.noreply.github.com> Date: Wed, 21 Feb 2024 11:40:55 -0500 Subject: [PATCH] Adds BYOP and Rewarded Ad impressions to prompt frequency capping flow. (#3434) * add byop impression to freq capping events * add rewarded ad * add test * add imp_ad expectation * fix unknown autoprompt type * add coverage * revert * revert more --------- Co-authored-by: justinchou-google --- src/runtime/auto-prompt-manager-test.js | 47 +++++++++++++++++-------- src/runtime/auto-prompt-manager.ts | 5 +++ 2 files changed, 37 insertions(+), 15 deletions(-) diff --git a/src/runtime/auto-prompt-manager-test.js b/src/runtime/auto-prompt-manager-test.js index bb2f794282..44bf0e3406 100644 --- a/src/runtime/auto-prompt-manager-test.js +++ b/src/runtime/auto-prompt-manager-test.js @@ -775,19 +775,27 @@ describes.realWin('AutoPromptManager', (env) => { [ { - storageKey: ImpressionStorageKeys.NEWSLETTER_SIGNUP, eventType: AnalyticsEvent.IMPRESSION_NEWSLETTER_OPT_IN, + storageKey: ImpressionStorageKeys.NEWSLETTER_SIGNUP, + }, + { + eventType: AnalyticsEvent.IMPRESSION_BYOP_NEWSLETTER_OPT_IN, + storageKey: ImpressionStorageKeys.NEWSLETTER_SIGNUP, }, { - storageKey: ImpressionStorageKeys.REGISTRATION_WALL, eventType: AnalyticsEvent.IMPRESSION_REGWALL_OPT_IN, + storageKey: ImpressionStorageKeys.REGISTRATION_WALL, }, { - storageKey: ImpressionStorageKeys.REWARDED_SURVEY, eventType: AnalyticsEvent.IMPRESSION_SURVEY, + storageKey: ImpressionStorageKeys.REWARDED_SURVEY, }, - ].forEach(({storageKey, eventType}) => { - it(`for storageKey=${storageKey} and eventType=${eventType}, should set frequency cap local storage if experiment is enabled`, async () => { + { + eventType: AnalyticsEvent.IMPRESSION_REWARDED_AD, + storageKey: ImpressionStorageKeys.REWARDED_AD, + }, + ].forEach(({eventType, storageKey}) => { + it(`for eventType=${eventType} and storageKey=${storageKey}, should set frequency cap timestamps via local storage if experiment is enabled`, async () => { autoPromptManager.frequencyCappingLocalStorageEnabled_ = true; autoPromptManager.isClosable_ = true; storageMock @@ -3469,7 +3477,7 @@ describes.realWin('AutoPromptManager', (env) => { .once(); await autoPromptManager.showAutoPrompt({alwaysShow: false}); - await tick(20); + await tick(25); expect(logEventSpy).to.be.calledOnceWith({ eventType: AnalyticsEvent.EVENT_PROMPT_FREQUENCY_CAP_MET, @@ -3611,7 +3619,7 @@ describes.realWin('AutoPromptManager', (env) => { .once(); await autoPromptManager.showAutoPrompt({alwaysShow: false}); - await tick(20); + await tick(25); expect(logEventSpy).to.be.calledOnceWith({ eventType: AnalyticsEvent.EVENT_PROMPT_FREQUENCY_CAP_MET, @@ -4047,7 +4055,7 @@ describes.realWin('AutoPromptManager', (env) => { expect(actionFlowSpy).to.not.have.been.called; }); - it('should show the second dismissible prompt if the frequency cap for contributions is met on lock content', async () => { + it('should show the second dismissible prompt if the frequency cap for contributions is met on locked content', async () => { sandbox.stub(pageConfig, 'isLocked').returns(true); setupPreviousImpressionAndDismissals( storageMock, @@ -4082,7 +4090,7 @@ describes.realWin('AutoPromptManager', (env) => { .once(); await autoPromptManager.showAutoPrompt({alwaysShow: false}); - await tick(20); + await tick(25); expect(logEventSpy).to.be.calledOnceWith({ eventType: AnalyticsEvent.EVENT_PROMPT_FREQUENCY_CAP_MET, @@ -4293,7 +4301,7 @@ describes.realWin('AutoPromptManager', (env) => { alwaysShow: false, isClosable: true, }); - await tick(20); + await tick(25); expect(autoPromptManager.promptFrequencyCappingEnabled_).to.equal(true); expect(autoPromptManager.isClosable_).to.equal(true); @@ -4389,7 +4397,7 @@ describes.realWin('AutoPromptManager', (env) => { }); }); - it('should not display a prompt for an unknown autoprompt type if the next action is a monetization prompt', async () => { + it('should display a monetization prompt for an unknown autoprompt type if the next action is a monetization prompt', async () => { setupPreviousImpressionAndDismissals( storageMock, { @@ -4412,11 +4420,11 @@ describes.realWin('AutoPromptManager', (env) => { autoPromptType: 'unknown', alwaysShow: false, }); - await tick(10); + await tick(25); expect(autoPromptManager.promptFrequencyCappingEnabled_).to.equal(true); expect(subscriptionPromptFnSpy).to.not.have.been.called; - expect(contributionPromptFnSpy).to.not.have.been.called; + expect(contributionPromptFnSpy).to.be.calledOnce; expect(startSpy).to.not.have.been.called; }); @@ -4457,7 +4465,7 @@ describes.realWin('AutoPromptManager', (env) => { autoPromptType: 'unknown', alwaysShow: false, }); - await tick(20); + await tick(25); expect(autoPromptManager.promptFrequencyCappingEnabled_).to.equal(true); expect(contributionPromptFnSpy).to.not.have.been.called; @@ -4595,11 +4603,12 @@ describes.realWin('AutoPromptManager', (env) => { storageMock, impressions = {} ) { - const {contribution, newsletter, regwall, survey, subscription} = { + const {contribution, newsletter, regwall, survey, ad, subscription} = { contribution: null, newsletter: null, regwall: null, survey: null, + ad: null, subscription: null, ...impressions, }; @@ -4635,6 +4644,14 @@ describes.realWin('AutoPromptManager', (env) => { ) .resolves(survey) .once(); + storageMock + .expects('get') + .withExactArgs( + ImpressionStorageKeys.REWARDED_AD, + /* useLocalStorage */ true + ) + .resolves(ad) + .once(); storageMock .expects('get') .withExactArgs( diff --git a/src/runtime/auto-prompt-manager.ts b/src/runtime/auto-prompt-manager.ts index b7afd1f54d..2db1d1b147 100644 --- a/src/runtime/auto-prompt-manager.ts +++ b/src/runtime/auto-prompt-manager.ts @@ -89,11 +89,16 @@ const INTERVENTION_TO_STORAGE_KEY_MAP = new Map([ AnalyticsEvent.IMPRESSION_NEWSLETTER_OPT_IN, ImpressionStorageKeys.NEWSLETTER_SIGNUP, ], + [ + AnalyticsEvent.IMPRESSION_BYOP_NEWSLETTER_OPT_IN, + ImpressionStorageKeys.NEWSLETTER_SIGNUP, + ], [ AnalyticsEvent.IMPRESSION_REGWALL_OPT_IN, ImpressionStorageKeys.REGISTRATION_WALL, ], [AnalyticsEvent.IMPRESSION_SURVEY, ImpressionStorageKeys.REWARDED_SURVEY], + [AnalyticsEvent.IMPRESSION_REWARDED_AD, ImpressionStorageKeys.REWARDED_AD], [ AnalyticsEvent.IMPRESSION_SWG_SUBSCRIPTION_MINI_PROMPT, ImpressionStorageKeys.SUBSCRIPTION,