Shopify / hydrogen

Hydrogen lets you build faster headless storefronts in less time, on Shopify.
https://hydrogen.shop
MIT License
1.24k stars 246 forks source link

ShopifyCustomerPrivacy fixes and improvements #2103

Closed juanpprieto closed 1 month ago

juanpprieto commented 1 month ago

These are some fixes and optimizations based on a 3P consent implementation.

[x] Add validation and error logging to ensure conset.storefrontAccessToken is a valid public token and not a server token [x] Add an explicit return type of CustomerPrivacy | null to getCustomerPrivacy [x] Add a type of (data: {error: string} | undefined) => void to the setTrackingConsent callback [x] Fix the VisitorConsent type to expect Boolean values instead of "true", "false" strings. While implementing a 3P consent sync with Shopify, we noticed that sale_of_data must be a boolean or setTrackingConsent would error. @alejandro-shopify could you confirm that these can be booleans? CC @wizardlyhel

shopify[bot] commented 1 month ago
Oxygen deployed a preview of your juanpprieto/fix-analytics branch. Details: Storefront Status Preview link Deployment details Last update (UTC)
custom-cart-method ✅ Successful (Logs) Preview deployment Inspect deployment May 14, 2024 6:49 PM
vite ✅ Successful (Logs) Preview deployment Inspect deployment May 14, 2024 6:49 PM
third-party-queries-caching ✅ Successful (Logs) Preview deployment Inspect deployment May 14, 2024 6:49 PM
optimistic-cart-ui ✅ Successful (Logs) Preview deployment Inspect deployment May 14, 2024 6:50 PM
subscriptions ✅ Successful (Logs) Preview deployment Inspect deployment May 14, 2024 6:50 PM
Skeleton (skeleton.hydrogen.shop) ✅ Successful (Logs) Preview deployment Inspect deployment May 14, 2024 6:49 PM

Learn more about Hydrogen's GitHub integration.

alejandro-shopify commented 1 month ago

Yes, the args to setTrackingConsent are booleans. Full types:

export interface SetGranularConsentParameter {
  [ConsentDisplayKeys.MARKETING]?: boolean;
  [ConsentDisplayKeys.ANALYTICS]?: boolean;
  [ConsentDisplayKeys.PREFERENCES]?: boolean;
  [ConsentDisplayKeys.SALE_OF_DATA]?: boolean;
  [ConsentDisplayKeys.EMAIL]?: String;
  [StorefrontApiConsentMetadata.HEADLESS_STOREFRONT]?: boolean;
  [StorefrontApiConsentMetadata.ROOT_DOMAIN]?: string;
  [StorefrontApiConsentMetadata.CHECKOUT_ROOT_DOMAIN]?: string;
  [StorefrontApiConsentMetadata.STOREFRONT_ROOT_DOMAIN]?: string;
  [StorefrontApiConsentMetadata.STOREFRONT_ACCESS_TOKEN]?: string;
  [StorefrontApiConsentMetadata.IS_EXTENSION_TOKEN]?: boolean;
  [StorefrontApiConsentMetadata.METAFIELDS]?: Metafield[];
}
export function setTrackingConsent(
  consent: boolean | SetGranularConsentParameter,
  callback?: (err?: null | {}, success?: {}) => {} | void,
): setTrackingConsentPromise 

Do not use the shorthard (setTrackingConsent(false, () => console.log("everything declined")) as that is deprecated, it's legacy from where consent was binary and not per purpose.

github-actions[bot] commented 1 month ago

We detected some changes in packages/*/package.json or packages/*/src, and there are no updates in the .changeset. If the changes are user-facing and should cause a version bump, run npm run changeset add to track your changes and include them in the next release CHANGELOG. If you are making simple updates to examples or documentation, you do not need to add a changeset.