Skip to content

feat(hydrogen): collect marketing consent in the account sheet's sign-in form - #4401

Open
fredericoo wants to merge 2 commits into
fb-account-widget-data-routefrom
fb-account-widget-marketing-consent
Open

fredericoo wants to merge 2 commits into
fb-account-widget-data-routefrom
fb-account-widget-marketing-consent

Conversation

@fredericoo

Copy link
Copy Markdown
Contributor

TL;DR: The <shopify-account> sign-in form can now collect marketing consent on Hydrogen storefronts. The widget data route returns the shop's two marketing consent settings, and the login route forwards the customer's choice to Shopify.

Stacked on #4399, which added GET /account/widget-data with both settings hard-coded to false because the login route dropped the choice.

When marketingConsentSigninEnabled is true, the component shows a checkbox and appends marketing_consent=true|false to its sign-in-url. Shopify's authorize endpoint accepts that param and subscribes a consenting customer to email marketing, but only when the shop collects marketing consent at sign-in. There are no public docs for it; the source is AuthorizeController and its LoginPage concern in Core, which accept only true or false.

Before

GET /account/login?marketing_consent=true   → authorize URL has no marketing_consent
GET /account/widget-data                    → marketingConsentSigninEnabled: false (always)

After

GET /account/login?marketing_consent=true   → authorize URL has marketing_consent=true
GET /account/widget-data                    → marketingConsentSigninEnabled and
                                              marketingConsentPreselected from the shop's settings

What this changes

Login route

  • /account/login forwards marketing_consent to Shopify's authorize URL when the value is true or false. Any other value is dropped.
  • prepareLoginUrl accepts the same choice as marketingConsent?: boolean.

Widget data route

  • Shop.marketingConsentSigninEnabled and Shop.marketingConsentPreselected exist only in the unstable Storefront API version (API change marketing_consent_signin_web_component_fields). They are not in 2026-10 or 2027-01, and the Customer Account API has no equivalent.
  • The route sends one extra request to unstable for those two fields, next to the existing shop query. It uses the request's country, because preselection depends on the visitor's country.
  • When that request fails, the route logs a warning, returns both settings as false, and still responds 200. An aborted request (the visitor leaving, or the route's 8-second deadline) returns false without a warning.
  • marketingConsentPreselected is false whenever marketingConsentSigninEnabled is false.

Storefront client (internal)

  • A handler receives a built client without its token or fetch, so it can't create a second client for unstable. client.ts gets an internal withStorefrontApiVersion(options, apiVersion) helper that sends one graphql() call to another version. It is not exported from the package.
  • docs/tech-debt.md recorded a per-query version override as rejected for the public API. It now notes this internal exception.

Developer impact

Includes a minor changeset for @shopify/hydrogen: prepareLoginUrl gains the marketingConsent option, and AccountWidgetShop's two marketing consent fields widen from false to boolean. No setup changes are needed; storefronts on #4399 get the checkbox when the shop has the setting on.

UX impact

On shops that collect marketing consent at sign-in, the account sheet's sign-in form shows the marketing consent checkbox, pre-checked where the shop's settings allow it for the visitor's country.

Risk

  • Dependency on unstable. This version has no stability guarantee. If Shopify renames or removes the fields, deployed storefronts lose the checkbox, and every widget data request still sends the extra request (uncached, because the client doesn't cache results with errors) and logs a warning until Hydrogen is upgraded. There is precedent: useShopifyCookies on the 2026-04 branch queries consentManagement on unstable.
  • Latency. The response waits for both Storefront API requests, so a slow unstable request delays the account sheet, up to the route's deadline.
  • Linkable consent. /account/login?marketing_consent=true can be linked to directly, so a crafted link sends consent without the customer seeing the checkbox. Shopify's authorize URL has the same property.

How to Test

  1. Follow the setup in feat(hydrogen): render <shopify-account> with ShopifyAccount and serve its data from /account/widget-data #4399, with a store that has "collect marketing consent at sign-in" turned on in its customer account settings.
  2. Open the storefront signed out and open the account sheet. Confirm the marketing consent checkbox shows, and that /account/widget-data returns marketingConsentSigninEnabled: true.
  3. Tick the checkbox, enter an email and continue. In the network panel, confirm the redirect from /account/login to Shopify carries marketing_consent=true.
  4. Finish signing in with a new customer. In the Shopify admin, confirm the customer is subscribed to email marketing.
  5. Repeat with a store that has the setting off. Confirm the checkbox doesn't show.

…n route

The login route passes marketing_consent to Shopify's authorize URL, and the widget
data route reads the shop's two marketing consent settings from the unstable
Storefront API so the sign-in form can show the checkbox.
Replaces the client copy and its args registry with an internal option on graphql(),
stops logging aborts as query failures, and documents the cost of depending on
unstable.
@fredericoo
fredericoo marked this pull request as ready for review November 19, 2026 18:44
@fredericoo
fredericoo requested a review from a team as a code owner November 19, 2026 18:44
@fredericoo
fredericoo added this pull request to stack #4402 November 19, 2026 18:44
@github-actions

Copy link
Copy Markdown
Contributor

📦 Bundle size report

Status Target Scenario Minified Gzip Δ Minified Δ Gzip
🟡 server customer account 31.1 kB 10.01 kB +1.01 kB (+3.4%) +236 B (+2.4%)
🟡 server entry: @shopify/hydrogen 169.63 kB 53.65 kB +56 B (+0.0%) +45 B (+0.1%)
🟡 server entry: @shopify/hydrogen/customer-account 31.46 kB 10.16 kB +1.01 kB (+3.3%) +238 B (+2.4%)
🟡 server request handling 53.92 kB 17.58 kB +58 B (+0.1%) +19 B (+0.1%)
Unchanged (11)
Status Target Scenario Minified Gzip Δ Minified Δ Gzip
⚪ server cart handlers 23.94 kB 7.14 kB ±0 ±0
⚪ server predictive search handlers 9.32 kB 3.71 kB ±0 ±0
⚪ client entry: @shopify/hydrogen/react 98.53 kB 32.02 kB ±0 ±0
⚪ client entry: @shopify/hydrogen/vue 98.67 kB 31.94 kB ±0 ±0
⚪ client react cart 48.3 kB 15.1 kB ±0 ±0
⚪ client react collection 11.59 kB 3.94 kB ±0 ±0
⚪ client react predictive search 9.39 kB 3.43 kB ±0 ±0
⚪ client react product 14.66 kB 5.23 kB ±0 ±0
⚪ client react shop pay button 10.2 kB 4.48 kB ±0 ±0
⚪ client react shopify scripts 18.72 kB 6.93 kB ±0 ±0
⚪ client vanilla cart 37.44 kB 11.59 kB ±0 ±0
How this is measured

Each scenario is bundled from the built package with Rolldown (tree-shaken, minified, dynamic imports inlined), then gzipped. Peer dependencies are external. Scenarios live in scripts/bundle-size-scenarios.ts.

🟢 smaller · 🟡 larger (< 5%) · 🔴 larger (≥ 5%) · 🆕 not in base · ❌ failed to bundle · ⚪ unchanged

@shopify

shopify Bot commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

Oxygen deployed a preview of your fb-account-widget-marketing-consent branch. Details:

Storefront Status Preview link Deployment details Last update (UTC)
Skeleton (skeleton.hydrogen.shop) ✅ Successful (Logs) Preview deployment Inspect deployment November 19, 2026 6:45 PM

Learn more about Hydrogen's GitHub integration.

const REDIRECT_STATUSES = new Set([301, 302, 303, 307, 308]);
const CACHE_ENABLED_GRAPHQL_CLIENTS = new WeakSet<StorefrontClient["graphql"]>();
const SHOULD_CACHE_RESULT = Symbol();
const API_VERSION = Symbol();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blocking: this symbol doesn't survive the dev build, so in development the consent query goes to the bundled version instead of unstable.

Under the development export condition, @shopify/hydrogen resolves to dist/development/core/development.mjs, which pulls in its own copy of client.mjs (dist/development/client/client.mjs). @shopify/hydrogen/customer-account has no development condition, so it always loads dist/client/client.mjs. Each copy creates its own Symbol():

  1. merchant creates the client from @shopify/hydrogen (dev copy)
  2. widget-data.mjs calls withStorefrontApiVersion from the prod copy and sets the prod API_VERSION key
  3. the dev graphql() reads opts[API_VERSION] with its own symbol, gets undefined, falls back to apiUrl
  4. the query hits 2026-10, the fields don't exist, every widget-data request logs a warning and the checkbox never shows

I built the package and called queryAccountWidgetShop with a client from the package entry:

node probe.mjs                          -> [".../api/2026-10/...", ".../api/unstable/..."]
node --conditions=development probe.mjs -> [".../api/2026-10/...", ".../api/2026-10/..."]

Vite resolves the development condition in dev IIRC, so nobody can test this locally. The session tests import from source, so they can't catch it.

Symbol.for("@shopify/hydrogen/storefront-api-version") would make both copies agree. If I'm not mistaken SHOULD_CACHE_RESULT and CACHE_ENABLED_GRAPHQL_CLIENTS have the same split already, but that's out of scope here.

);
// The response waits for both requests, so a slow marketing consent request delays the account
// sheet until the route's deadline.
const [result, marketingConsent] = await Promise.all([

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blocking: a slow unstable request makes the cart sync fail.

The consent query shares the route's 8s deadline, and the client's own timeout is 30s, so the deadline always fires first. Because Promise.all waits on it:

  1. unstable hangs
  2. await shopRequest in handleWidgetDataRoute doesn't resolve until the deadline aborts signal
  3. commitSessionWithCartSync(..., signal) runs with an already-aborted signal
  4. updateBuyerIdentity throws before sending anything, so syncCartBuyerIdentity logs and returns false

After a token refresh the cart never gets attached to the customer. After a 401 the detach fails, so the cart cookie gets expired and the visitor loses their cart. Before this PR only the cached main shop query could do that - now an API version with no stability guarantee can, on every request.

Repro: the existing "attaches the cart to the customer after a token refresh" test, with the unstable fetch hanging until abort and vi.advanceTimersByTimeAsync(8_000). It returns 200 with cart mutations []. Without the hang you get the attach mutation.

Let's give the consent query its own short budget so it can't use up the route's deadline:

const consentSignal = AbortSignal.any([signal, AbortSignal.timeout(MARKETING_CONSENT_TIMEOUT_IN_MS)]);

and add the refresh and 401 cases above as regression tests.

};
} catch (error) {
// An abort is the visitor leaving or the route's deadline, not a failed query.
if (!signal.aborted) log.warn("account widget marketing consent query failed", { error });

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

non-blocking: signal.aborted is also true when the route's own 8s deadline fires, so a slow unstable request gives an 8s account sheet and zero logs. That looks the same as the visitor leaving.

The deadline aborts with a CustomerAccountTimeoutError, so we can tell the two apart:

if (!signal.aborted || signal.reason instanceof CustomerAccountTimeoutError) {
  log.warn("account widget marketing consent query failed", { error });
}

If the consent query gets its own timeout (comment above), that timeout firing should warn too. The deadline test at session.test.ts asserts logger.warn is not called, so that would need flipping.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants