Skip to content

Commit

Permalink
Bug 1790424 - Fixed screen targeting for mobile download onboarding s…
Browse files Browse the repository at this point in the history
…tring r=pdahiya

Differential Revision: https://phabricator.services.mozilla.com/D171609
  • Loading branch information
jwayn committed Mar 7, 2023
1 parent c0343f9 commit 278e1ec
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 16 deletions.
16 changes: 15 additions & 1 deletion browser/components/newtab/aboutwelcome/AboutWelcomeChild.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ class AboutWelcomeChild extends JSWindowActorChild {
defineAs: "AWSelectTheme",
});

Cu.exportFunction(this.AWEvaluateScreenTargeting.bind(this), window, {
defineAs: "AWEvaluateScreenTargeting",
});

Cu.exportFunction(this.AWSendEventTelemetry.bind(this), window, {
defineAs: "AWSendEventTelemetry",
});
Expand Down Expand Up @@ -141,6 +145,12 @@ class AboutWelcomeChild extends JSWindowActorChild {
);
}

AWEvaluateScreenTargeting(data) {
return this.wrapPromise(
this.sendQuery("AWPage:EVALUATE_SCREEN_TARGETING", data)
);
}

/**
* Send initial data to page including experiment information
*/
Expand Down Expand Up @@ -179,12 +189,16 @@ class AboutWelcomeChild extends JSWindowActorChild {
// override the default with `null`
let defaults = lazy.AboutWelcomeDefaults.getDefaults();

// Removing screens based on their targeting evaluations
let screens = featureConfig.screens ?? defaults.screens;
screens = await this.AWEvaluateScreenTargeting(screens);

const content = await lazy.AboutWelcomeDefaults.prepareContentForReact({
...attributionData,
...experimentMetadata,
...defaults,
...featureConfig,
screens: featureConfig.screens ?? defaults.screens,
screens,
backdrop: featureConfig.backdrop ?? defaults.backdrop,
});

Expand Down
3 changes: 3 additions & 0 deletions browser/components/newtab/aboutwelcome/AboutWelcomeParent.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ XPCOMUtils.defineLazyModuleGetters(lazy, {
"resource://activity-stream/aboutwelcome/lib/AboutWelcomeDefaults.jsm",
ShellService: "resource:///modules/ShellService.jsm",
LangPackMatcher: "resource://gre/modules/LangPackMatcher.jsm",
AWScreenUtils: "resource://activity-stream/lib/AWScreenUtils.jsm",
});

XPCOMUtils.defineLazyGetter(lazy, "log", () => {
Expand Down Expand Up @@ -225,6 +226,8 @@ class AboutWelcomeParent extends JSWindowActorParent {
);
case "AWPage:GET_APP_AND_SYSTEM_LOCALE_INFO":
return lazy.LangPackMatcher.getAppAndSystemLocaleInfo();
case "AWPage:EVALUATE_SCREEN_TARGETING":
return lazy.AWScreenUtils.evaluateTargetingAndRemoveScreens(data);
case "AWPage:NEGOTIATE_LANGPACK":
return lazy.LangPackMatcher.negotiateLangPackForLanguageMismatch(data);
case "AWPage:ENSURE_LANG_PACK_INSTALLED":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,9 @@ const MR_ABOUT_WELCOME_DEFAULT = {
},
{
id: "AW_MOBILE_DOWNLOAD",
targeting: "isFxASignedIn && sync.mobileDevices > 0",
// The mobile download screen should only be shown to users who
// are either not logged into FxA, or don't have any mobile devices syncing
targeting: "!isFxASignedIn || sync.mobileDevices == 0",
content: {
position: "split",
split_narrow_bkg_position: "-160px",
Expand Down Expand Up @@ -385,13 +387,13 @@ function evaluateWelcomeScreenButtonLabel(removeDefault) {
: "mr2022-onboarding-set-default-primary-button-label";
}

function prepareMobileDownload(screens) {
let mobileContent = screens?.find(
function prepareMobileDownload(content) {
let mobileContent = content?.screens?.find(
screen => screen.id === "AW_MOBILE_DOWNLOAD"
)?.content;

if (!mobileContent) {
return;
return content;
}
if (!lazy.BrowserUtils.sendToDeviceEmailsSupported()) {
// If send to device emails are not supported for a user's locale,
Expand All @@ -408,15 +410,13 @@ function prepareMobileDownload(screens) {
mobileContent.hero_image.url.indexOf(".svg")
)}-cn.svg`;
}

return content;
}

async function prepareContentForReact(content) {
const { screens } = content;

// Remove screens based on screen targeting
// by running filter through ASRouter targeting checks
await lazy.AWScreenUtils.evaluateTargetingAndRemoveScreens(screens);

if (content?.template === "return_to_amo") {
return content;
}
Expand Down Expand Up @@ -537,8 +537,7 @@ async function prepareContentForReact(content) {
);
}

prepareMobileDownload(content.screens);
return content;
return prepareMobileDownload(content);
}

const AboutWelcomeDefaults = {
Expand Down
8 changes: 7 additions & 1 deletion browser/components/newtab/lib/AWScreenUtils.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const lazy = {};

XPCOMUtils.defineLazyModuleGetters(lazy, {
ASRouter: "resource://activity-stream/lib/ASRouter.jsm",
ASRouterTargeting: "resource://activity-stream/lib/ASRouterTargeting.jsm",
});

const AWScreenUtils = {
Expand All @@ -35,6 +36,7 @@ const AWScreenUtils = {
async evaluateScreenTargeting(targeting) {
const result = await lazy.ASRouter.evaluateExpression({
expression: targeting,
context: lazy.ASRouterTargeting.Environment,
});
if (result?.evaluationStatus?.success) {
return result.evaluationStatus.result;
Expand All @@ -50,9 +52,11 @@ const AWScreenUtils = {
*
* @param {object[]} screens - An array of screens that will be looped
* through to be evaluated for removal
* @returns {object[]} - A new array containing the screens that were not removed
*/
async evaluateTargetingAndRemoveScreens(screens) {
await this.removeScreens(screens, async screen => {
const filteredScreens = [...screens];
await this.removeScreens(filteredScreens, async screen => {
if (screen.targeting === undefined) {
// Don't remove the screen if we don't have a targeting property
return false;
Expand All @@ -63,6 +67,8 @@ const AWScreenUtils = {
// don't want to remove the screen, while false means we do
return !result;
});

return filteredScreens;
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ describe("AWScreenUtils", () => {
globals = new GlobalOverrider();
globals.set({
ASRouter,
ASRouterTargeting: {
Environment: {},
},
});

sandbox = sinon.createSandbox();
Expand Down Expand Up @@ -87,8 +90,10 @@ describe("AWScreenUtils", () => {
},
};
});
await AWScreenUtils.evaluateTargetingAndRemoveScreens(screens);
assert.deepEqual(screens, expectedScreens);
const evaluatedStrings = await AWScreenUtils.evaluateTargetingAndRemoveScreens(
screens
);
assert.deepEqual(evaluatedStrings, expectedScreens);
});
it("should not remove screens with no targeting", async () => {
const screens = [
Expand All @@ -114,8 +119,10 @@ describe("AWScreenUtils", () => {
}
return targeting;
});
await AWScreenUtils.evaluateTargetingAndRemoveScreens(screens);
assert.deepEqual(screens, expectedScreens);
const evaluatedStrings = await AWScreenUtils.evaluateTargetingAndRemoveScreens(
screens
);
assert.deepEqual(evaluatedStrings, expectedScreens);
});
});
});

0 comments on commit 278e1ec

Please sign in to comment.