@shopify/hydrogen 2024.4.6 → 2024.4.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -484,7 +484,7 @@ async function fetchWithServerCache(url, requestInit, {
484
484
  }
485
485
 
486
486
  // src/version.ts
487
- var LIB_VERSION = "2024.4.6";
487
+ var LIB_VERSION = "2024.4.7";
488
488
 
489
489
  // src/constants.ts
490
490
  var STOREFRONT_REQUEST_GROUP_ID_HEADER = "Custom-Storefront-Request-Group-ID";
@@ -4122,20 +4122,37 @@ function useCustomerPrivacy(props) {
4122
4122
  if (scriptStatus !== "done" || loadedEvent.current)
4123
4123
  return;
4124
4124
  loadedEvent.current = true;
4125
- if (!consentConfig.checkoutDomain)
4125
+ const { checkoutDomain, storefrontAccessToken } = consentConfig;
4126
+ if (!checkoutDomain)
4126
4127
  logMissingConfig("checkoutDomain");
4127
- if (!consentConfig.storefrontAccessToken)
4128
+ if (!storefrontAccessToken)
4128
4129
  logMissingConfig("storefrontAccessToken");
4129
- if (consentConfig.storefrontAccessToken.startsWith("shpat_") || consentConfig.storefrontAccessToken.length !== 32) {
4130
+ if (storefrontAccessToken.startsWith("shpat_") || storefrontAccessToken.length !== 32) {
4130
4131
  console.error(
4131
4132
  `[h2:error:useCustomerPrivacy] It looks like you passed a private access token, make sure to use the public token`
4132
4133
  );
4133
4134
  }
4134
- if (withPrivacyBanner && window?.privacyBanner) {
4135
- window?.privacyBanner?.loadBanner({
4136
- checkoutRootDomain: consentConfig.checkoutDomain,
4137
- storefrontAccessToken: consentConfig.storefrontAccessToken
4135
+ const config = {
4136
+ checkoutRootDomain: checkoutDomain,
4137
+ storefrontAccessToken
4138
+ };
4139
+ if (checkoutDomain) {
4140
+ let storefrontRootDomain = window.document.location.host;
4141
+ const checkoutDomainParts = checkoutDomain.split(".").reverse();
4142
+ const currentDomainParts = storefrontRootDomain.split(".").reverse();
4143
+ const sameDomainParts = [];
4144
+ checkoutDomainParts.forEach((part, index) => {
4145
+ if (part === currentDomainParts[index]) {
4146
+ sameDomainParts.push(part);
4147
+ }
4138
4148
  });
4149
+ storefrontRootDomain = sameDomainParts.reverse().join(".");
4150
+ if (storefrontRootDomain) {
4151
+ config.storefrontRootDomain = storefrontRootDomain;
4152
+ }
4153
+ }
4154
+ if (withPrivacyBanner && window?.privacyBanner) {
4155
+ window.privacyBanner?.loadBanner(config);
4139
4156
  }
4140
4157
  if (!window.Shopify?.customerPrivacy)
4141
4158
  return;
@@ -4145,8 +4162,7 @@ function useCustomerPrivacy(props) {
4145
4162
  {
4146
4163
  ...consent,
4147
4164
  headlessStorefront: true,
4148
- checkoutRootDomain: consentConfig.checkoutDomain,
4149
- storefrontAccessToken: consentConfig.storefrontAccessToken
4165
+ ...config
4150
4166
  },
4151
4167
  callback
4152
4168
  );
@@ -4282,10 +4298,7 @@ function pageViewHandler(payload) {
4282
4298
  function productViewHandler(payload) {
4283
4299
  let eventPayload = prepareBasePageViewPayload(payload);
4284
4300
  if (eventPayload && validateProducts({
4285
- eventName: PRODUCT_VIEWED,
4286
- productField: "products",
4287
- variantField: "product.<displayed_variant>",
4288
- fromSource: "product_viewed products array",
4301
+ type: "product",
4289
4302
  products: payload.products
4290
4303
  })) {
4291
4304
  const formattedProducts = formatProduct(payload.products);
@@ -4365,10 +4378,7 @@ function sendCartAnalytics({
4365
4378
  sku: matchedLine.merchandise.sku
4366
4379
  };
4367
4380
  if (validateProducts({
4368
- eventName: ADD_TO_CART,
4369
- productField: "merchandise.product",
4370
- variantField: "merchandise",
4371
- fromSource: "cart query",
4381
+ type: "cart",
4372
4382
  products: [product]
4373
4383
  })) {
4374
4384
  hydrogenReact.sendShopifyAnalytics({
@@ -4380,51 +4390,50 @@ function sendCartAnalytics({
4380
4390
  });
4381
4391
  }
4382
4392
  }
4383
- var PRODUCT_VIEWED = "Product viewed";
4384
- var ADD_TO_CART = "Add to cart";
4385
- function missingErrorMessage(eventName, missingFieldName, fromSource) {
4386
- console.error(
4387
- `[h2:error:ShopifyAnalytics] ${eventName}: ${missingFieldName} is required from the ${fromSource}.`
4388
- );
4393
+ function missingErrorMessage(type, fieldName, isVariantField, viewKeyName) {
4394
+ if (type === "cart") {
4395
+ const name = `${isVariantField ? "merchandise" : "merchandise.product"}.${fieldName}`;
4396
+ console.error(
4397
+ `[h2:error:ShopifyAnalytics] Can't set up cart analytics events because the \`cart.lines[].${name}\` value is missing from your GraphQL cart query. In your project, search for where \`fragment CartLine on CartLine\` is defined and make sure \`${name}\` is part of your cart query. Check the Hydrogen Skeleton template for reference: https://github.com/Shopify/hydrogen/blob/main/templates/skeleton/app/lib/fragments.ts#L25-L56.`
4398
+ );
4399
+ } else {
4400
+ const name = `${viewKeyName || fieldName}`;
4401
+ console.error(
4402
+ `[h2:error:ShopifyAnalytics] Can't set up product view analytics events because the \`${name}\` is missing from your \`<Analytics.ProductView>\`. Make sure \`${name}\` is part of your products data prop. Check the Hydrogen Skeleton template for reference: https://github.com/Shopify/hydrogen/blob/main/templates/skeleton/app/routes/products.%24handle.tsx#L159-L165.`
4403
+ );
4404
+ }
4389
4405
  }
4390
4406
  function validateProducts({
4391
- eventName,
4392
- productField,
4393
- variantField,
4394
- products,
4395
- fromSource
4407
+ type,
4408
+ products
4396
4409
  }) {
4397
4410
  if (!products || products.length === 0) {
4398
- missingErrorMessage(eventName, `${productField}`, fromSource);
4411
+ missingErrorMessage(type, "", false, "data.products");
4399
4412
  return false;
4400
4413
  }
4401
4414
  products.forEach((product) => {
4402
4415
  if (!product.id) {
4403
- missingErrorMessage(eventName, `${productField}.id`, fromSource);
4416
+ missingErrorMessage(type, "id", false);
4404
4417
  return false;
4405
4418
  }
4406
4419
  if (!product.title) {
4407
- missingErrorMessage(eventName, `${productField}.title`, fromSource);
4420
+ missingErrorMessage(type, "title", false);
4408
4421
  return false;
4409
4422
  }
4410
4423
  if (!product.price) {
4411
- missingErrorMessage(
4412
- eventName,
4413
- `${variantField}.price.amount`,
4414
- fromSource
4415
- );
4424
+ missingErrorMessage(type, "price.amount", true, "price");
4416
4425
  return false;
4417
4426
  }
4418
4427
  if (!product.vendor) {
4419
- missingErrorMessage(eventName, `${productField}.vendor`, fromSource);
4428
+ missingErrorMessage(type, "vendor", false);
4420
4429
  return false;
4421
4430
  }
4422
4431
  if (!product.variantId) {
4423
- missingErrorMessage(eventName, `${variantField}.id`, fromSource);
4432
+ missingErrorMessage(type, "id", true, "variantId");
4424
4433
  return false;
4425
4434
  }
4426
4435
  if (!product.variantTitle) {
4427
- missingErrorMessage(eventName, `${variantField}.title`, fromSource);
4436
+ missingErrorMessage(type, "title", true, "variantTitle");
4428
4437
  return false;
4429
4438
  }
4430
4439
  });
@@ -4451,7 +4460,7 @@ function formatProduct(products) {
4451
4460
  }
4452
4461
  function logMissingField(fieldName) {
4453
4462
  console.error(
4454
- `[h2:error:CartAnalytics] Can't set up cart analytics events because the \`cart.${fieldName}\` value is missing from your GraphQL cart query. In standard Hydrogen projects, the cart query is contained in \`app/lib/fragments.js\`. Make sure it includes \`cart.${fieldName}\`. Check the Hydrogen Skeleton template for reference: https://github.com/Shopify/hydrogen/blob/main/templates/skeleton/app/lib/fragments.ts#L59.`
4463
+ `[h2:error:CartAnalytics] Can't set up cart analytics events because the \`cart.${fieldName}\` value is missing from your GraphQL cart query. In your project, search for where \`fragment CartApiQuery on Cart\` is defined and make sure \`${fieldName}\` is part of your cart query. Check the Hydrogen Skeleton template for reference: https://github.com/Shopify/hydrogen/blob/main/templates/skeleton/app/lib/fragments.ts#L59.`
4455
4464
  );
4456
4465
  }
4457
4466
  function CartAnalytics({