Commit e3f488b9 authored by Rebecca King's avatar Rebecca King Committed by fchasen@mozilla.com
Browse files

Bug 1964836 - Clean up AboutWelcomeShopping and Shopping messages -...

Bug 1964836 - Clean up AboutWelcomeShopping and Shopping messages - r=shopping-reviewers,omc-reviewers,emcminn,fchasen

Differential Revision: https://phabricator.services.mozilla.com/D248957
parent f53b6152
Loading
Loading
Loading
Loading
+0 −15
Original line number Diff line number Diff line
@@ -211,21 +211,6 @@ let JSWINDOWACTORS = {
    matches: ["about:tabcrashed*"],
  },

  AboutWelcomeShopping: {
    parent: {
      esModuleURI: "resource:///actors/AboutWelcomeParent.sys.mjs",
    },
    child: {
      esModuleURI: "resource:///actors/AboutWelcomeChild.sys.mjs",
      events: {
        Update: {},
      },
    },
    matches: ["about:shoppingsidebar"],
    remoteTypes: ["privilegedabout"],
    messageManagerGroups: ["shopping-sidebar", "browsers", "review-checker"],
  },

  AboutWelcome: {
    parent: {
      esModuleURI: "resource:///actors/AboutWelcomeParent.sys.mjs",
+0 −566
Original line number Diff line number Diff line
@@ -398,569 +398,3 @@ export class AboutWelcomeChild extends JSWindowActorChild {
    lazy.log.debug(`Received page event ${event.type}`);
  }
}

const OPTIN_DEFAULT = {
  id: "FAKESPOT_OPTIN_DEFAULT",
  template: "multistage",
  backdrop: "transparent",
  aria_role: "alert",
  UTMTerm: "opt-in",
  screens: [
    {
      id: "FS_OPT_IN",
      content: {
        position: "split",
        title: { string_id: "shopping-onboarding-headline" },
        // We set the dynamic subtitle ID below at the same time as the args;
        // to prevent intermittents caused by the args loading too late.
        subtitle: { string_id: "" },
        above_button_content: [
          {
            type: "text",
            text: {
              string_id: "shopping-onboarding-body",
            },
            link_keys: ["learn_more"],
          },
          {
            type: "image",
            url: "chrome://browser/content/shopping/assets/optInLight.avif",
            darkModeImageURL:
              "chrome://browser/content/shopping/assets/optInDark.avif",
            marginInline: "24px",
          },
          {
            type: "text",
            text: {
              string_id:
                "shopping-onboarding-opt-in-privacy-policy-and-terms-of-use3",
            },
            link_keys: ["privacy_policy", "terms_of_use"],
            font_styles: "legal",
          },
        ],
        learn_more: {
          action: {
            type: "OPEN_URL",
            data: {
              args: "https://support.mozilla.org/1/firefox/%VERSION%/%OS%/%LOCALE%/review-checker-review-quality?utm_source=review-checker&utm_campaign=learn-more&utm_medium=in-product",
              where: "tab",
            },
          },
        },
        privacy_policy: {
          action: {
            type: "OPEN_URL",
            data: {
              args: "https://www.mozilla.org/privacy/firefox?utm_source=review-checker&utm_campaign=privacy-policy&utm_medium=in-product&utm_term=opt-in-screen",
              where: "tab",
            },
          },
        },
        terms_of_use: {
          action: {
            type: "OPEN_URL",
            data: {
              args: "https://www.fakespot.com/terms?utm_source=review-checker&utm_campaign=terms-of-use&utm_medium=in-product",
              where: "tab",
            },
          },
        },
        primary_button: {
          should_focus_button: true,
          label: { string_id: "shopping-onboarding-opt-in-button" },
          action: {
            type: "SET_PREF",
            data: {
              pref: {
                name: "browser.shopping.experience2023.optedIn",
                value: 1,
              },
            },
          },
        },
        additional_button: {
          label: {
            string_id: "shopping-onboarding-not-now-button",
          },
          style: "link",
          flow: "column",
          action: {
            type: "SET_PREF",
            data: {
              pref: {
                name: "browser.shopping.experience2023.active",
                value: false,
              },
            },
          },
        },
      },
    },
  ],
};

const SHOPPING_MICROSURVEY = {
  id: "SHOPPING_MICROSURVEY",
  template: "multistage",
  backdrop: "transparent",
  transitions: true,
  UTMTerm: "survey",
  screens: [
    {
      id: "SHOPPING_MICROSURVEY_SCREEN_1",
      above_button_steps_indicator: true,
      content: {
        position: "split",
        layout: "survey",
        steps_indicator: {
          string_id: "shopping-onboarding-welcome-steps-indicator-label",
        },
        title: {
          string_id: "shopping-survey-headline",
        },
        primary_button: {
          label: {
            string_id: "shopping-survey-next-button-label",
            paddingBlock: "5px",
            marginBlock: "0 12px",
          },
          action: {
            type: "MULTI_ACTION",
            collectSelect: true,
            data: {
              actions: [],
            },
            navigate: true,
          },
          disabled: "hasActiveMultiSelect",
        },
        additional_button: {
          label: {
            string_id: "shopping-survey-terms-link",
          },
          style: "link",
          flow: "column",
          action: {
            type: "OPEN_URL",
            data: {
              args: "https://www.mozilla.org/about/legal/terms/mozilla/?utm_source=review-checker&utm_campaign=terms-of-use-screen-1&utm_medium=in-product",
              where: "tab",
            },
          },
        },
        dismiss_button: {
          action: {
            dismiss: true,
          },
          label: {
            string_id: "shopping-onboarding-dialog-close-button",
          },
        },
        tiles: {
          type: "multiselect",
          style: {
            flexDirection: "column",
            alignItems: "flex-start",
          },
          label: {
            string_id: "shopping-survey-question-one",
          },
          data: [
            {
              id: "radio-1",
              type: "radio",
              group: "radios",
              defaultValue: false,
              label: { string_id: "shopping-survey-q1-radio-1-label" },
            },
            {
              id: "radio-2",
              type: "radio",
              group: "radios",
              defaultValue: false,
              label: { string_id: "shopping-survey-q1-radio-2-label" },
            },
            {
              id: "radio-3",
              type: "radio",
              group: "radios",
              defaultValue: false,
              label: { string_id: "shopping-survey-q1-radio-3-label" },
            },
            {
              id: "radio-4",
              type: "radio",
              group: "radios",
              defaultValue: false,
              label: { string_id: "shopping-survey-q1-radio-4-label" },
            },
            {
              id: "radio-5",
              type: "radio",
              group: "radios",
              defaultValue: false,
              label: { string_id: "shopping-survey-q1-radio-5-label" },
            },
          ],
        },
      },
    },
    {
      id: "SHOPPING_MICROSURVEY_SCREEN_2",
      above_button_steps_indicator: true,
      content: {
        position: "split",
        layout: "survey",
        steps_indicator: {
          string_id: "shopping-onboarding-welcome-steps-indicator-label",
        },
        title: {
          string_id: "shopping-survey-headline",
        },
        primary_button: {
          label: {
            string_id: "shopping-survey-submit-button-label",
            paddingBlock: "5px",
            marginBlock: "0 12px",
          },
          action: {
            type: "MULTI_ACTION",
            collectSelect: true,
            data: {
              actions: [],
            },
            navigate: true,
          },
          disabled: "hasActiveMultiSelect",
        },
        additional_button: {
          label: {
            string_id: "shopping-survey-terms-link",
          },
          style: "link",
          flow: "column",
          action: {
            type: "OPEN_URL",
            data: {
              args: "https://www.mozilla.org/about/legal/terms/mozilla/?utm_source=review-checker&utm_campaign=terms-of-use-screen-2&utm_medium=in-product",
              where: "tab",
            },
          },
        },
        dismiss_button: {
          action: {
            dismiss: true,
          },
          label: {
            string_id: "shopping-onboarding-dialog-close-button",
          },
        },
        tiles: {
          type: "multiselect",
          style: {
            flexDirection: "column",
            alignItems: "flex-start",
          },
          label: {
            string_id: "shopping-survey-question-two",
          },
          data: [
            {
              id: "radio-1",
              type: "radio",
              group: "radios",
              defaultValue: false,
              label: { string_id: "shopping-survey-q2-radio-1-label" },
            },
            {
              id: "radio-2",
              type: "radio",
              group: "radios",
              defaultValue: false,
              label: { string_id: "shopping-survey-q2-radio-2-label" },
            },
            {
              id: "radio-3",
              type: "radio",
              group: "radios",
              defaultValue: false,
              label: { string_id: "shopping-survey-q2-radio-3-label" },
            },
          ],
        },
      },
    },
  ],
};

const OPTED_IN_TIME_PREF = "browser.shopping.experience2023.survey.optedInTime";

XPCOMUtils.defineLazyPreferenceGetter(
  lazy,
  "isSurveySeen",
  "browser.shopping.experience2023.survey.hasSeen",
  false
);

XPCOMUtils.defineLazyPreferenceGetter(
  lazy,
  "pdpVisits",
  "browser.shopping.experience2023.survey.pdpVisits",
  0
);

XPCOMUtils.defineLazyPreferenceGetter(
  lazy,
  "optedInTime",
  OPTED_IN_TIME_PREF,
  0
);

let optInDynamicContent;
// Limit pref increase to 5 as we don't need to count any higher than that
const MIN_VISITS_TO_SHOW_SURVEY = 5;
// Wait 24 hours after opt in to show survey
const MIN_TIME_AFTER_OPT_IN = 24 * 60 * 60;

export class AboutWelcomeShoppingChild extends AboutWelcomeChild {
  // Static state used to track session in which user opted-in
  static optedInSession = false;

  // Static used to track PDP visits per session for showing survey
  static eligiblePDPvisits = [];

  constructor() {
    super();
    this.surveyEnabled =
      lazy.NimbusFeatures.shopping2023.getVariable("surveyEnabled");

    // Used by tests
    this.resetChildStates = () => {
      AboutWelcomeShoppingChild.eligiblePDPvisits.length = 0;
      AboutWelcomeShoppingChild.optedInSession = false;
    };
  }

  computeEligiblePDPCount(data) {
    // Increment our pref if this isn't a page we've already seen this session
    if (lazy.pdpVisits < MIN_VISITS_TO_SHOW_SURVEY) {
      this.AWSendToParent("SPECIAL_ACTION", {
        type: "SET_PREF",
        data: {
          pref: {
            name: "browser.shopping.experience2023.survey.pdpVisits",
            value: lazy.pdpVisits + 1,
          },
        },
      });
    }

    // Add this product to our list of unique eligible PDPs visited
    // to prevent errors caused by multiple events being fired simultaneously
    AboutWelcomeShoppingChild.eligiblePDPvisits.push(data?.product_id);
  }

  evaluateAndShowSurvey() {
    // Re-evaluate if we should show the survey
    // Render survey if user is opted-in and has met survey seen conditions
    const now = Date.now() / 1000;
    const hasBeen24HrsSinceOptin =
      lazy.optedInTime && now - lazy.optedInTime >= MIN_TIME_AFTER_OPT_IN;

    this.showMicroSurvey =
      this.surveyEnabled &&
      !lazy.isSurveySeen &&
      !AboutWelcomeShoppingChild.optedInSession &&
      lazy.pdpVisits >= MIN_VISITS_TO_SHOW_SURVEY &&
      hasBeen24HrsSinceOptin;

    if (this.showMicroSurvey && !this.showOnboarding) {
      this.renderMessage();
    }
  }

  setOptInTime() {
    const now = Date.now() / 1000;
    this.AWSendToParent("SPECIAL_ACTION", {
      type: "SET_PREF",
      data: {
        pref: {
          name: OPTED_IN_TIME_PREF,
          value: now,
        },
      },
    });
  }

  handleEvent(event) {
    // Decide when to show/hide onboarding and survey message
    const { productUrl, showOnboarding, data } = event.detail;
    this.showOnboarding = showOnboarding;

    // Display onboarding if a user hasn't opted-in
    const optInReady = showOnboarding && productUrl;
    if (optInReady) {
      // Render opt-in message
      AboutWelcomeShoppingChild.optedInSession = true;
      this.AWSetProductURL(productUrl);
      this.renderMessage();

      return;
    }

    //Store timestamp if user opts in
    if (
      Object.hasOwn(event.detail, "showOnboarding") &&
      !event.detail.showOnboarding &&
      !lazy.optedInTime
    ) {
      this.setOptInTime();
    }
    // Hide the container until the user is eligible to see the survey
    // or user has just completed opt-in
    if (!lazy.isSurveySeen || AboutWelcomeShoppingChild.optedInSession) {
      this.document.getElementById("multi-stage-message-root").hidden = true;
    }

    // Early exit if user has seen survey, if we have no data, encountered
    // an error, or if pdp is ineligible or not unique
    if (
      lazy.isSurveySeen ||
      !data ||
      data.error ||
      !productUrl ||
      (data.needs_analysis &&
        (!data.product_id || !data.grade || !data.adjusted_rating)) ||
      AboutWelcomeShoppingChild.eligiblePDPvisits.includes(data.product_id)
    ) {
      return;
    }

    this.computeEligiblePDPCount(data, productUrl);
    this.evaluateAndShowSurvey();
  }

  renderMessage() {
    this.contentWindow.clearTimeout(this.thankYouFadeTimeout);
    this.document.getElementById("multi-stage-message-root").hidden = false;
    this.document.dispatchEvent(
      new this.contentWindow.CustomEvent("RenderWelcome", { bubbles: true })
    );
  }

  resetOnboardingContainer(root) {
    root.innerHTML = "";
    let newRoot = root.cloneNode(false);
    root.replaceWith(newRoot);
    return newRoot;
  }

  // TODO - Move messages into an ASRouter message provider. See bug 1848251.
  AWGetFeatureConfig() {
    let messageContent = optInDynamicContent;
    if (this.showMicroSurvey && !this.showOnboarding) {
      messageContent = SHOPPING_MICROSURVEY;
      this.setShoppingSurveySeen();
    }
    return Cu.cloneInto(messageContent, this.contentWindow);
  }

  setShoppingSurveySeen() {
    this.AWSendToParent("SPECIAL_ACTION", {
      type: "SET_PREF",
      data: {
        pref: {
          name: "browser.shopping.experience2023.survey.hasSeen",
          value: true,
        },
      },
    });
  }

  // TODO - Add dismiss: true to the primary CTA so it cleans up the React
  // content, which will stop being rendered on opt-in. See bug 1848429.
  AWFinish() {
    if (this._destroyed) {
      return;
    }
    let root = this.document.getElementById("multi-stage-message-root");
    if (root) {
      root = this.resetOnboardingContainer(root);
      root
        .appendChild(this.document.createElement("shopping-message-bar"))
        .setAttribute("type", "thank-you-for-feedback");
      this.contentWindow.clearTimeout(this.thankYouFadeTimeout);
      this.thankYouFadeTimeout = this.contentWindow.setTimeout(() => {
        root = this.resetOnboardingContainer(root);
        root.hidden = true;
      }, 5000);
    }
  }

  AWSetProductURL(productUrl) {
    let productHostname;
    if (productUrl) {
      productHostname = new URL(productUrl).hostname;
    }
    let content = this._AWGetOptInDefaultContent(productHostname);
    optInDynamicContent = content;
  }

  _AWGetOptInDefaultContent(productUrl) {
    let content = JSON.parse(JSON.stringify(OPTIN_DEFAULT));
    const [optInScreen] = content.screens;

    if (productUrl) {
      optInScreen.content.subtitle.string_id =
        "shopping-onboarding-dynamic-subtitle-1";

      switch (
        productUrl // Insert the productUrl into content
      ) {
        case "www.amazon.com":
          optInScreen.content.subtitle.args = {
            currentSite: "Amazon",
            secondSite: "Walmart",
            thirdSite: "Best Buy",
          };
          break;
        case "www.walmart.com":
          optInScreen.content.subtitle.args = {
            currentSite: "Walmart",
            secondSite: "Amazon",
            thirdSite: "Best Buy",
          };
          break;
        case "www.bestbuy.com":
          optInScreen.content.subtitle.args = {
            currentSite: "Best Buy",
            secondSite: "Amazon",
            thirdSite: "Walmart",
          };
          break;
        default:
          optInScreen.content.subtitle.args = {
            currentSite: "Amazon",
            secondSite: "Walmart",
            thirdSite: "Best Buy",
          };
      }

      if (
        Services.prefs.getBoolPref("toolkit.shopping.experience2023.defr") &&
        (productUrl === "www.amazon.fr" || productUrl === "www.amazon.de")
      ) {
        optInScreen.content.subtitle.string_id =
          "shopping-onboarding-single-subtitle";
        optInScreen.content.subtitle.args = {
          currentSite: "Amazon",
        };
      }
    }

    return content;
  }

  AWEnsureLangPackInstalled() {}
}
+0 −48
Original line number Diff line number Diff line
@@ -313,51 +313,3 @@ export class AboutWelcomeParent extends JSWindowActorParent {
    return null;
  }
}

export class AboutWelcomeShoppingParent extends AboutWelcomeParent {
  /**
   * Use gBrowser as the browser in messages from the sidebar content.
   *
   * @param {{name: string, data?: any}} message
   * @override
   */
  receiveMessage(message) {
    const { name, data } = message;
    let browser;

    if (this.manager.rootFrameLoader) {
      let { ownerElement } = this.manager.rootFrameLoader;
      let { topChromeWindow } = ownerElement.ownerGlobal.browsingContext;
      browser = topChromeWindow.gBrowser;
      return this.onContentMessage(name, data, browser);
    }

    lazy.log.warn(`Not handling ${name} because the browser doesn't exist.`);
    return null;
  }

  /**
   * Handle messages from AboutWelcomeChild.sys.mjs
   *
   * @param {string} type
   * @param {any=} data
   * @param {Browser} the global xul:browser
   */
  onContentMessage(type, data, browser) {
    // Only handle the messages that are relevant to the shopping page.
    switch (type) {
      case "AWPage:SPECIAL_ACTION":
      case "AWPage:TELEMETRY_EVENT":
      case "AWPage:EVALUATE_SCREEN_TARGETING":
      case "AWPage:ADD_SCREEN_IMPRESSION":
        return super.onContentMessage(type, data, browser);
    }

    return undefined;
  }

  // Override unnecessary methods
  startAboutWelcomeObserver() {}

  didDestroy() {}
}
+0 −1
Original line number Diff line number Diff line
@@ -9,7 +9,6 @@
// aboutwelcome.html or spotlight.html. Ideally, there should be no `@import`
// statements in the built aboutwelcome.css file.
@import '../../asrouter/content-src/styles/feature-callout';
@import '../../asrouter/content-src/styles/shopping';

/* stylelint-disable max-nesting-depth */

+0 −255

File changed.

Preview size limit exceeded, changes collapsed.

Loading