@misiki/gocommerce-connector 0.19.0 → 0.21.0

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.
@@ -715,10 +715,14 @@ type CartExtras = {
715
715
  currency: string;
716
716
  } | null;
717
717
  discount?: AppliedDiscount | null;
718
- /** The Idempotency-Key of the last checkout attempt, and the body it was sent with. */
718
+ /**
719
+ * The Idempotency-Key of the last checkout attempt, the body it was sent with, and where
720
+ * Meta's pixel ran then, which a retry sends again unchanged.
721
+ */
719
722
  attempt?: {
720
723
  key: string;
721
724
  body: string;
725
+ meta?: Record<string, string>;
722
726
  } | null;
723
727
  };
724
728
 
@@ -715,10 +715,14 @@ type CartExtras = {
715
715
  currency: string;
716
716
  } | null;
717
717
  discount?: AppliedDiscount | null;
718
- /** The Idempotency-Key of the last checkout attempt, and the body it was sent with. */
718
+ /**
719
+ * The Idempotency-Key of the last checkout attempt, the body it was sent with, and where
720
+ * Meta's pixel ran then, which a retry sends again unchanged.
721
+ */
719
722
  attempt?: {
720
723
  key: string;
721
724
  body: string;
725
+ meta?: Record<string, string>;
722
726
  } | null;
723
727
  };
724
728
 
package/dist/index.cjs CHANGED
@@ -236,6 +236,17 @@ function currentMe() {
236
236
  return null;
237
237
  }
238
238
  }
239
+ function metaBrowser() {
240
+ const fbp = readCookie("_fbp");
241
+ if (!fbp || typeof window.fbq !== "function") return void 0;
242
+ const fbc = readCookie("_fbc");
243
+ return {
244
+ fbp,
245
+ ...fbc ? { fbc } : {},
246
+ client_user_agent: window.navigator.userAgent,
247
+ event_source_url: window.location.origin + window.location.pathname
248
+ };
249
+ }
239
250
  function storedCartId() {
240
251
  const v = readLocal("cart_id");
241
252
  if (!v || v === "undefined" || v === "null") return null;
@@ -2207,11 +2218,13 @@ var CheckoutService = class extends BaseService {
2207
2218
  ...extra
2208
2219
  };
2209
2220
  const serialised = JSON.stringify(body);
2210
- const key = extras.attempt?.body === `${code}:${serialised}` ? extras.attempt.key : newKey();
2211
- saveCartExtras(id2, { attempt: { key, body: `${code}:${serialised}` } });
2221
+ const retry = extras.attempt?.body === `${code}:${serialised}`;
2222
+ const key = retry ? extras.attempt.key : newKey();
2223
+ const meta = retry ? extras.attempt?.meta : metaBrowser();
2224
+ saveCartExtras(id2, { attempt: { key, body: `${code}:${serialised}`, ...meta ? { meta } : {} } });
2212
2225
  const result = await this.data(`/api/checkout/${encodeURIComponent(code)}`, {
2213
2226
  method: "POST",
2214
- body,
2227
+ body: meta ? { ...body, metadata: { ...extra.metadata, meta_capi: meta } } : body,
2215
2228
  headers: { "Idempotency-Key": key }
2216
2229
  });
2217
2230
  if (result.order.access_token) {
@@ -3167,6 +3180,11 @@ var StoreService = class extends BaseService {
3167
3180
  // The footer's sign-up box posts to ext/newsletter, and the theme editor's footer
3168
3181
  // switch (D131) can only show it while that is on.
3169
3182
  newsletter: { ...basePlugins.newsletter, ...flag(plugins.has("newsletter")) },
3183
+ // The ad and analytics tags (D160). The engine decides, not the static record:
3184
+ // one storefront serves every store, and one store's pixel must never
3185
+ // load on another's pages.
3186
+ ...adTags(plugins),
3187
+ googleTagManager: googleTags(plugins),
3170
3188
  // Discount codes are core: every GoCommerce store takes them, so
3171
3189
  // the code box is shown whatever the static record says.
3172
3190
  isDiscountCoupons: { ...basePlugins.isDiscountCoupons, active: true },
@@ -3192,6 +3210,32 @@ var StoreService = class extends BaseService {
3192
3210
  }
3193
3211
  };
3194
3212
  var storeService = new StoreService();
3213
+ var AD_TAGS = [
3214
+ ["meta-pixel", "pixel_id", "facebookPixel"],
3215
+ ["microsoft-clarity", "project_id", "microsoftClarity"],
3216
+ ["hotjar", "site_id", "hotjar"],
3217
+ ["tiktok-pixel", "pixel_id", "tiktokPixel"],
3218
+ ["pinterest-tag", "tag_id", "pinterestTag"],
3219
+ ["snapchat-pixel", "pixel_id", "snapchatPixel"]
3220
+ ];
3221
+ function adTags(plugins) {
3222
+ return Object.fromEntries(
3223
+ AD_TAGS.map(([key, field, name]) => {
3224
+ const id2 = String(plugins.get(key)?.settings?.[field] ?? "").trim();
3225
+ return [name, id2 ? { active: true, id: id2 } : { active: false }];
3226
+ })
3227
+ );
3228
+ }
3229
+ function googleTags(plugins) {
3230
+ const s = plugins.get("google-analytics")?.settings ?? {};
3231
+ const id2 = (field) => String(s[field] ?? "").trim();
3232
+ const [account, part] = id2("conversion_label").split("/");
3233
+ const label = part === void 0 ? account : part;
3234
+ const ads = id2("google_ads_id") || (part === void 0 ? "" : account);
3235
+ const tags = [id2("ga4_measurement_id"), ads, id2("gtm_container_id")].filter(Boolean);
3236
+ if (!tags.length) return { active: false };
3237
+ return { active: true, tags, conversions: ads && label ? [{ event: "purchase", label }] : [] };
3238
+ }
3195
3239
  function deliverable(base, ships) {
3196
3240
  if (ships.everywhere || !ships.countries.length) return {};
3197
3241
  const known = new Map((base.countries ?? []).map((c) => [String(c.code).toUpperCase(), c]));