Skip to content

Commit

Permalink
Merge pull request #3552 from mhkawano/rewarded_ads_button_callback
Browse files Browse the repository at this point in the history
Rewarded ads button callback
  • Loading branch information
mhkawano authored Sep 10, 2024
2 parents 13532d7 + 01ce86f commit 2722a21
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 84 deletions.
13 changes: 13 additions & 0 deletions demos/public/prod/enterprise/enterprise-funnel.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
<script async src="https://securepubads.g.doubleclick.net/tag/js/gpt.js" crossorigin="anonymous"></script>

<script>
let swg;

function clearLocalStorage() {
localStorage.removeItem("subscribe.google.com:USER_TOKEN");
}
Expand Down Expand Up @@ -95,10 +97,14 @@
button.onclick = () => {
const isClosable = document.getElementById("isClosable").checked;
const suppressToast = document.getElementById("suppressToast").checked;
const onAlternateAction = document.getElementById("onAlternateAction").checked;
const onSignIn = document.getElementById("onSignIn").checked;
intervention.show({
isClosable,
onResult: (result) => addResultRow(intervention.type, result),
suppressToast,
onAlternateAction: onAlternateAction ? () => alert('buy flow launched') : null,
onSignIn: onSignIn ? () => alert('sign in flow launched') : null,
});
}
table.appendChild(row);
Expand All @@ -116,6 +122,7 @@
}

(self.SWG = self.SWG || []).push(async subscriptions => {
swg = subscriptions;
const eventManager = await subscriptions.getEventManager();
eventManager.registerEventListener(logHandler);
const availableInterventions = await subscriptions.getAvailableInterventions();
Expand All @@ -133,6 +140,12 @@ <h1>Funnel for Enterprise Demo</h1>
<input type="checkbox" id="suppressToast" name="scales" checked />
<label for="suppressToast">suppressToast</label>
</input>
<input type="checkbox" id="onAlternateAction" name="scales" checked />
<label for="onAlternateAction">onAlternateAction</label>
</input>
<input type="checkbox" id="onSignIn" name="scales" checked />
<label for="onSignIn">onSignIn</label>
</input>
</div>
<article>
<table>
Expand Down
10 changes: 9 additions & 1 deletion src/api/available-intervention-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ describes.realWin('AvailableIntervention', (env) => {
});

it('calls audience action local flow', async () => {
const onResultHanlder = () => {};
const alternateActionHandler = () => {};
const signInHandler = () => {};
const startStub = sandbox.stub();
const actionFlowStub = sandbox
.stub(audienceActionLocalFlow, 'AudienceActionLocalFlow')
Expand All @@ -71,14 +74,19 @@ describes.realWin('AvailableIntervention', (env) => {

await availableIntervention.show({
isClosable: true,
onResult: onResultHanlder,
onAlternateAction: alternateActionHandler,
onSignIn: signInHandler,
});

expect(actionFlowStub).to.have.been.calledWith(deps, {
isClosable: true,
action: InterventionType.TYPE_REWARDED_AD,
configurationId: 'TEST_CONFIGURATION_ID',
onResult: undefined,
onResult: onResultHanlder,
calledManually: true,
onAlternateAction: alternateActionHandler,
onSignIn: signInHandler,
});
expect(startStub).to.have.been.calledOnce;
});
Expand Down
10 changes: 9 additions & 1 deletion src/api/available-intervention.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,14 @@ export interface ShowInterventionParams {
// indicating if the data was recorded successfully.
onResult?: (result: InterventionResult) => Promise<boolean> | boolean;

// Suppresses the completion toasts of the intervention
// Suppresses the completion toasts of the intervention.
suppressToast?: boolean;

// Callback for clicking clicking on an alternate action. Closes the prompt when called.
onAlternateAction?: () => void;

// Callback for signing in. Closes the prompt when called.
onSignIn?: () => void;
}

export class AvailableIntervention {
Expand Down Expand Up @@ -112,6 +118,8 @@ export class AvailableIntervention {
onResult: params.onResult,
isClosable: params.isClosable,
calledManually: true,
onAlternateAction: params.onAlternateAction,
onSignIn: params.onSignIn,
}).start();
}
throw Error(`Can't show ${this.type}`);
Expand Down
89 changes: 59 additions & 30 deletions src/runtime/audience-action-local-flow-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ describes.realWin('AudienceActionLocalFlow', (env) => {
addEventListener: (event, handler) => {
eventListeners[event] = handler;
},
removeEventListener: sandbox.spy(),
refresh: sandbox.spy(),
};
readyEventArg = {
Expand Down Expand Up @@ -188,8 +189,8 @@ describes.realWin('AudienceActionLocalFlow', (env) => {
const flow = new AudienceActionLocalFlow(
runtime,
params,
/* gptTimeoutMs_= */ 5,
/* thanksTimeoutMs_= */ 5
/* gptTimeoutMs_= */ 1,
/* thanksTimeoutMs_= */ 1
);

await startRewardedAdFlow(flow);
Expand Down Expand Up @@ -254,6 +255,13 @@ describes.realWin('AudienceActionLocalFlow', (env) => {
).to.be.calledOnce.calledWithExactly();
}

function didCleanUpGoogletag() {
expect(
env.win.googletag.destroySlots
).to.be.calledOnce.calledWithExactly([rewardedSlot]);
expect(pubadsobj.removeEventListener).to.have.callCount(4);
}

it('clicking on locked greypane closes', async () => {
const params = {
...DEFAULT_PARAMS,
Expand Down Expand Up @@ -297,12 +305,10 @@ describes.realWin('AudienceActionLocalFlow', (env) => {
await subscribeButton.click();
await tick();

didCleanUpGoogletag();
expect(
params.monetizationFunction
).to.be.calledOnce.calledWithExactly();
expect(
env.win.googletag.destroySlots
).to.be.calledOnce.calledWithExactly([rewardedSlot]);
expect(eventManager.logSwgEvent).to.be.calledWith(
AnalyticsEvent.IMPRESSION_REWARDED_AD
);
Expand Down Expand Up @@ -336,13 +342,11 @@ describes.realWin('AudienceActionLocalFlow', (env) => {
await contributeButton.click();
await tick();

didCleanUpGoogletag();
expect(params.onCancel).to.be.calledOnce.calledWithExactly();
expect(
params.monetizationFunction
).to.be.calledOnce.calledWithExactly();
expect(
env.win.googletag.destroySlots
).to.be.calledOnce.calledWithExactly([rewardedSlot]);
expect(eventManager.logSwgEvent).to.be.calledWith(
AnalyticsEvent.IMPRESSION_REWARDED_AD
);
Expand Down Expand Up @@ -404,6 +408,41 @@ describes.realWin('AudienceActionLocalFlow', (env) => {
expect(signinButton).to.be.null;
});

it('renders enterprise', async () => {
const params = {
...DEFAULT_PARAMS,
autoPromptType: undefined,
calledManually: true,
onAlternateAction: sandbox.spy(),
onSignIn: sandbox.spy(),
};
await renderAndAssertRewardedAd(params, DEFAULT_CONFIG);

const wrapper = await callReadyAndReturnWrapper();

expect(env.win.fetch).to.be.calledWith(
'https://news.google.com/swg/_/api/v1/publication/pub1/getactionconfigurationui?publicationId=pub1&configurationId=xyz&origin=about%3Asrcdoc&previewEnabled=false'
);

const subscribeButton = wrapper.shadowRoot.querySelector(
'.rewarded-ad-support-button'
);
expect(subscribeButton).to.not.be.null;
expect(subscribeButton.innerHTML).contains('Subscribe');
await subscribeButton.click();

const signinButton = wrapper.shadowRoot.querySelector(
'.rewarded-ad-sign-in-button'
);
expect(signinButton).to.not.be.null;
expect(signinButton.innerHTML).contains('Already a subscriber?');
await signinButton.click();

await tick();

expect(params.onAlternateAction).to.be.calledOnce;
expect(params.onSignIn).to.be.calledOnce;
});
it('escapes bad input', async () => {
const BAD_CONFIG = `
{
Expand Down Expand Up @@ -457,14 +496,12 @@ describes.realWin('AudienceActionLocalFlow', (env) => {
await closeButton.click();
await tick();

didCleanUpGoogletag();
expect(env.win.document.body.style.overflow).to.equal('');
const updatedWrapper = env.win.document.querySelector(
'.audience-action-local-wrapper'
);
expect(updatedWrapper).to.be.null;
expect(
env.win.googletag.destroySlots
).to.be.calledOnce.calledWithExactly([rewardedSlot]);
expect(params.onCancel).to.be.calledOnce.calledWithExactly();
expect(eventManager.logSwgEvent).to.be.calledWith(
AnalyticsEvent.ACTION_REWARDED_AD_CLOSE
Expand Down Expand Up @@ -532,6 +569,7 @@ describes.realWin('AudienceActionLocalFlow', (env) => {
);

expect(env.win.googletag.destroySlots).to.not.be.called;
expect(pubadsobj.removeEventListener).to.have.callCount(4);

await didBailout(DEFAULT_PARAMS);
});
Expand All @@ -552,6 +590,7 @@ describes.realWin('AudienceActionLocalFlow', (env) => {
);

expect(env.win.googletag.destroySlots).to.not.be.called;
expect(pubadsobj.removeEventListener).to.have.callCount(4);

expect(params.onCancel).to.be.called;

Expand All @@ -571,6 +610,7 @@ describes.realWin('AudienceActionLocalFlow', (env) => {
);

expect(env.win.googletag.destroySlots).to.not.be.called;
expect(pubadsobj.removeEventListener).to.have.callCount(4);

await didBailout(DEFAULT_PARAMS);
});
Expand All @@ -588,11 +628,9 @@ describes.realWin('AudienceActionLocalFlow', (env) => {
AnalyticsEvent.EVENT_REWARDED_AD_NOT_FILLED
);

expect(
env.win.googletag.destroySlots
).to.be.calledOnce.calledWithExactly([rewardedSlot]);

await didBailout(DEFAULT_PARAMS);

didCleanUpGoogletag();
});

it('fails to render with gpt.js detection shortcut', async () => {
Expand All @@ -614,6 +652,7 @@ describes.realWin('AudienceActionLocalFlow', (env) => {
);

expect(env.win.googletag.destroySlots).to.not.be.called;
expect(pubadsobj.removeEventListener).to.have.callCount(4);

await didBailout(DEFAULT_PARAMS);
});
Expand Down Expand Up @@ -650,9 +689,7 @@ describes.realWin('AudienceActionLocalFlow', (env) => {
AnalyticsEvent.EVENT_REWARDED_AD_GPT_ERROR
);

expect(
env.win.googletag.destroySlots
).to.be.calledOnce.calledWithExactly([rewardedSlot]);
didCleanUpGoogletag();

await didBailout(DEFAULT_PARAMS);
});
Expand All @@ -673,9 +710,7 @@ describes.realWin('AudienceActionLocalFlow', (env) => {
expect(prompt).to.not.be.null;
expect(prompt.innerHTML).contains('Thanks for viewing this ad');

expect(
env.win.googletag.destroySlots
).to.be.calledOnce.calledWithExactly([rewardedSlot]);
didCleanUpGoogletag();

expect(env.win.fetch).to.be.calledWith(
'https://news.google.com/swg/_/api/v1/publication/pub1/completeaudienceaction?sut=abc&configurationId=xyz&audienceActionType=TYPE_REWARDED_AD'
Expand Down Expand Up @@ -840,9 +875,7 @@ describes.realWin('AudienceActionLocalFlow', (env) => {
'.audience-action-local-wrapper'
);
expect(updatedWrapper).to.be.null;
expect(
env.win.googletag.destroySlots
).to.be.calledOnce.calledWithExactly([rewardedSlot]);
didCleanUpGoogletag();
expect(params.onCancel).to.be.calledOnce.calledWithExactly();
expect(eventManager.logSwgEvent).to.be.calledWith(
AnalyticsEvent.ACTION_REWARDED_AD_CLOSE_AD
Expand All @@ -865,9 +898,7 @@ describes.realWin('AudienceActionLocalFlow', (env) => {
'.audience-action-local-wrapper'
);
expect(updatedWrapper).to.not.be.null;
expect(
env.win.googletag.destroySlots
).to.be.calledOnce.calledWithExactly([rewardedSlot]);
didCleanUpGoogletag();
expect(eventManager.logSwgEvent).to.be.calledWith(
AnalyticsEvent.ACTION_REWARDED_AD_CLOSE_AD
);
Expand Down Expand Up @@ -908,9 +939,7 @@ describes.realWin('AudienceActionLocalFlow', (env) => {
'.audience-action-local-wrapper'
);
expect(updatedWrapper).to.be.null;
expect(
env.win.googletag.destroySlots
).to.be.calledOnce.calledWithExactly([rewardedSlot]);
didCleanUpGoogletag();
});

it('tab focus trap works', async () => {
Expand Down
Loading

0 comments on commit 2722a21

Please sign in to comment.