Skip to content

Commit

Permalink
Does not record impression timestamp for prompts triggered by CTA but…
Browse files Browse the repository at this point in the history
…ton (#3494)

* adds attribute promptIsFromCtaButton when cta button to show prompt is clicked, used to not record impression timestamp

* removes console statements

* remove unused import

* add one more test

---------

Co-authored-by: justinchou-google <justinchou-google@users.noreply.github.com>
  • Loading branch information
justinchou-google and justinchou-google authored Apr 23, 2024
1 parent 24009c5 commit 22e220c
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 6 deletions.
83 changes: 83 additions & 0 deletions src/runtime/auto-prompt-manager-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,60 @@ describes.realWin('AutoPromptManager', (env) => {
});
});

[
{
eventType: AnalyticsEvent.IMPRESSION_SWG_CONTRIBUTION_MINI_PROMPT,
action: 'TYPE_CONTRIBUTION',
},
{
eventType: AnalyticsEvent.IMPRESSION_CONTRIBUTION_OFFERS,
action: 'TYPE_CONTRIBUTION',
},
{
eventType: AnalyticsEvent.IMPRESSION_SWG_SUBSCRIPTION_MINI_PROMPT,
action: 'TYPE_SUBSCRIPTION',
},
{eventType: AnalyticsEvent.IMPRESSION_OFFERS, action: 'TYPE_SUBSCRIPTION'},
].forEach(({eventType, action}) => {
it(`for autoprompt eventType=${eventType} and promptIsFromCta_ = true, should not set frequency cap impressions via local storage for action=${action}`, async () => {
autoPromptManager.frequencyCappingByDismissalsEnabled_ = true;
autoPromptManager.frequencyCappingLocalStorageEnabled_ = true;
autoPromptManager.promptIsFromCtaButton_ = true;
autoPromptManager.isClosable_ = true;
storageMock
.expects('get')
.withExactArgs(StorageKeys.TIMESTAMPS, /* useLocalStorage */ true)
.never();
storageMock
.expects('set')
.withExactArgs(StorageKeys.TIMESTAMPS, /* useLocalStorage */ true)
.never();
// Legacy storage operations
// TODO(justinchou): once deprecated, join with other impression tests.
storageMock
.expects('get')
.withExactArgs(StorageKeys.IMPRESSIONS, /* useLocalStorage */ true)
.resolves(null)
.once();
storageMock
.expects('set')
.withExactArgs(
StorageKeys.IMPRESSIONS,
CURRENT_TIME.toString(),
/* useLocalStorage */ true
)
.resolves()
.once();

await eventManagerCallback({
eventType,
eventOriginator: EventOriginator.UNKNOWN_CLIENT,
isFromUserAction: null,
additionalParameters: null,
});
});
});

[
{
eventType: AnalyticsEvent.IMPRESSION_NEWSLETTER_OPT_IN,
Expand Down Expand Up @@ -1519,6 +1573,26 @@ describes.realWin('AutoPromptManager', (env) => {
});
});

[
{
eventType: AnalyticsEvent.ACTION_SWG_BUTTON_SHOW_OFFERS_CLICK,
},
{
eventType: AnalyticsEvent.ACTION_SWG_BUTTON_SHOW_CONTRIBUTIONS_CLICK,
},
].forEach(({eventType}) => {
it(`should set promptIsFromCtaButton on cta button action: ${eventType}`, async () => {
autoPromptManager.frequencyCappingLocalStorageEnabled_ = true;
await eventManagerCallback({
eventType,
eventOriginator: EventOriginator.UNKNOWN_CLIENT,
isFromUserAction: null,
additionalParameters: null,
});
expect(autoPromptManager.promptIsFromCtaButton_).to.be.true;
});
});

it('should display the mini prompt, but not fetch entitlements and client config if alwaysShow is enabled', async () => {
entitlementsManagerMock.expects('getEntitlements').never();
clientConfigManagerMock.expects('getAutoPromptConfig').never();
Expand Down Expand Up @@ -3916,6 +3990,15 @@ describes.realWin('AutoPromptManager', (env) => {
expect(contributionPromptFnSpy).to.have.been.calledOnce;
});

it('should set promptIsFromCtaButton_ to false when prompt is displayed', async () => {
autoPromptManager.promptIsFromCtaButton_ = true;

await autoPromptManager.showAutoPrompt({});
await tick(20);

expect(autoPromptManager.promptIsFromCtaButton_).to.be.false;
});

it('should not show any prompt if there are no audience actions', async () => {
getArticleExpectation
.resolves({
Expand Down
26 changes: 20 additions & 6 deletions src/runtime/auto-prompt-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@ const ACTION_TO_IMPRESSION_STORAGE_KEY_MAP = new Map([
[TYPE_SUBSCRIPTION, ImpressionStorageKeys.SUBSCRIPTION],
]);

const ACTON_CTA_BUTTON_CLICK = [
AnalyticsEvent.ACTION_SWG_BUTTON_SHOW_OFFERS_CLICK,
AnalyticsEvent.ACTION_SWG_BUTTON_SHOW_CONTRIBUTIONS_CLICK,
];

export interface ShowAutoPromptParams {
autoPromptType?: AutoPromptType;
alwaysShow?: boolean;
Expand All @@ -179,6 +184,7 @@ export class AutoPromptManager {
private monetizationPromptWasDisplayedAsSoftPaywall_ = false;
private hasStoredImpression_ = false;
private hasStoredMiniPromptImpression_ = false;
private promptIsFromCtaButton_ = false;
private lastAudienceActionFlow_: AudienceActionFlow | null = null;
private interventionDisplayed_: Intervention | null = null;
private frequencyCappingByDismissalsEnabled_: boolean = false;
Expand Down Expand Up @@ -282,19 +288,16 @@ export class AutoPromptManager {
if (!article) {
return;
}

this.frequencyCappingByDismissalsEnabled_ =
this.isArticleExperimentEnabled_(
article,
ArticleExperimentFlags.FREQUENCY_CAPPING_BY_DISMISSALS
);

this.frequencyCappingLocalStorageEnabled_ =
this.isArticleExperimentEnabled_(
article,
ArticleExperimentFlags.FREQUENCY_CAPPING_LOCAL_STORAGE
);

this.promptFrequencyCappingEnabled_ = this.isArticleExperimentEnabled_(
article,
ArticleExperimentFlags.PROMPT_FREQUENCY_CAPPING_EXPERIMENT
Expand Down Expand Up @@ -376,6 +379,7 @@ export class AutoPromptManager {
return;
}

this.promptIsFromCtaButton_ = false;
// Add display delay to dismissible prompts.
const displayDelayMs = isClosable
? (clientConfig?.autoPromptConfig?.clientDisplayTrigger
Expand Down Expand Up @@ -1026,6 +1030,9 @@ export class AutoPromptManager {

// ** Frequency Capping Events **
if (this.frequencyCappingLocalStorageEnabled_) {
if (ACTON_CTA_BUTTON_CLICK.find((e) => e === event.eventType)) {
this.promptIsFromCtaButton_ = true;
}
if (this.frequencyCappingByDismissalsEnabled_) {
await this.handleFrequencyCappingLocalStorage_(event.eventType);
} else {
Expand Down Expand Up @@ -1089,7 +1096,10 @@ export class AutoPromptManager {
return;
}

if (monetizationImpressionEvents.includes(analyticsEvent)) {
if (
!this.promptIsFromCtaButton_ &&
monetizationImpressionEvents.includes(analyticsEvent)
) {
if (this.hasStoredMiniPromptImpression_) {
return;
}
Expand Down Expand Up @@ -1290,8 +1300,12 @@ export class AutoPromptManager {
async storeEvent(event: AnalyticsEvent): Promise<void> {
let action;
if (IMPRESSION_EVENTS_TO_ACTION_MAP.has(event)) {
action = IMPRESSION_EVENTS_TO_ACTION_MAP.get(event);
this.storeImpression(action!);
// b/333536312: Only store impression if prompt was not triggered via cta
// click.
if (!this.promptIsFromCtaButton_) {
action = IMPRESSION_EVENTS_TO_ACTION_MAP.get(event);
this.storeImpression(action!);
}
} else if (DISMISSAL_EVENTS_TO_ACTION_MAP.has(event)) {
action = DISMISSAL_EVENTS_TO_ACTION_MAP.get(event);
this.storeDismissal(action!);
Expand Down

0 comments on commit 22e220c

Please sign in to comment.