@misiki/gocommerce-connector 0.20.0 → 0.22.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.
@@ -723,6 +723,7 @@ type CartExtras = {
723
723
  key: string;
724
724
  body: string;
725
725
  meta?: Record<string, string>;
726
+ attribution?: Record<string, unknown>;
726
727
  } | null;
727
728
  };
728
729
 
@@ -723,6 +723,7 @@ type CartExtras = {
723
723
  key: string;
724
724
  body: string;
725
725
  meta?: Record<string, string>;
726
+ attribution?: Record<string, unknown>;
726
727
  } | null;
727
728
  };
728
729
 
package/dist/index.cjs CHANGED
@@ -247,6 +247,41 @@ function metaBrowser() {
247
247
  event_source_url: window.location.origin + window.location.pathname
248
248
  };
249
249
  }
250
+ var ATTRIBUTION_KEY = "gc_attribution";
251
+ var ATTRIBUTION_DAYS = 30;
252
+ var TOUCH_FIELDS = [
253
+ "utm_source",
254
+ "utm_medium",
255
+ "utm_campaign",
256
+ "utm_content",
257
+ "utm_term",
258
+ "gclid",
259
+ "gbraid",
260
+ "wbraid",
261
+ "fbclid",
262
+ "ttclid",
263
+ "msclkid",
264
+ "referrer",
265
+ "at"
266
+ ];
267
+ function cleanTouch(raw, now) {
268
+ if (!raw || typeof raw !== "object") return void 0;
269
+ const at = Date.parse(String(raw.at ?? ""));
270
+ if (!Number.isFinite(at) || now - at > ATTRIBUTION_DAYS * 864e5 || at - now > 864e5) return void 0;
271
+ const out = {};
272
+ for (const k of TOUCH_FIELDS) {
273
+ const v = raw[k];
274
+ if (typeof v === "string" && v.trim()) out[k] = v.trim().slice(0, 200);
275
+ }
276
+ return Object.keys(out).length > 1 ? out : void 0;
277
+ }
278
+ function landingAttribution(now = Date.now()) {
279
+ const saved = readJSON(ATTRIBUTION_KEY);
280
+ const last = cleanTouch(saved?.last, now);
281
+ const first = cleanTouch(saved?.first, now) ?? last;
282
+ if (!first && !last) return void 0;
283
+ return { ...first ? { first } : {}, ...last ? { last } : {} };
284
+ }
250
285
  function storedCartId() {
251
286
  const v = readLocal("cart_id");
252
287
  if (!v || v === "undefined" || v === "null") return null;
@@ -2221,10 +2256,18 @@ var CheckoutService = class extends BaseService {
2221
2256
  const retry = extras.attempt?.body === `${code}:${serialised}`;
2222
2257
  const key = retry ? extras.attempt.key : newKey();
2223
2258
  const meta = retry ? extras.attempt?.meta : metaBrowser();
2224
- saveCartExtras(id2, { attempt: { key, body: `${code}:${serialised}`, ...meta ? { meta } : {} } });
2259
+ const attribution = retry ? extras.attempt?.attribution : landingAttribution();
2260
+ saveCartExtras(id2, {
2261
+ attempt: { key, body: `${code}:${serialised}`, ...meta ? { meta } : {}, ...attribution ? { attribution } : {} }
2262
+ });
2263
+ const metadata = {
2264
+ ...extra.metadata,
2265
+ ...meta ? { meta_capi: meta } : {},
2266
+ ...attribution ? { attribution } : {}
2267
+ };
2225
2268
  const result = await this.data(`/api/checkout/${encodeURIComponent(code)}`, {
2226
2269
  method: "POST",
2227
- body: meta ? { ...body, metadata: { ...extra.metadata, meta_capi: meta } } : body,
2270
+ body: Object.keys(metadata).length ? { ...body, metadata } : body,
2228
2271
  headers: { "Idempotency-Key": key }
2229
2272
  });
2230
2273
  if (result.order.access_token) {
@@ -3184,6 +3227,7 @@ var StoreService = class extends BaseService {
3184
3227
  // one storefront serves every store, and one store's pixel must never
3185
3228
  // load on another's pages.
3186
3229
  ...adTags(plugins),
3230
+ googleTagManager: googleTags(plugins),
3187
3231
  // Discount codes are core: every GoCommerce store takes them, so
3188
3232
  // the code box is shown whatever the static record says.
3189
3233
  isDiscountCoupons: { ...basePlugins.isDiscountCoupons, active: true },
@@ -3225,6 +3269,16 @@ function adTags(plugins) {
3225
3269
  })
3226
3270
  );
3227
3271
  }
3272
+ function googleTags(plugins) {
3273
+ const s = plugins.get("google-analytics")?.settings ?? {};
3274
+ const id2 = (field) => String(s[field] ?? "").trim();
3275
+ const [account, part] = id2("conversion_label").split("/");
3276
+ const label = part === void 0 ? account : part;
3277
+ const ads = id2("google_ads_id") || (part === void 0 ? "" : account);
3278
+ const tags = [id2("ga4_measurement_id"), ads, id2("gtm_container_id")].filter(Boolean);
3279
+ if (!tags.length) return { active: false };
3280
+ return { active: true, tags, conversions: ads && label ? [{ event: "purchase", label }] : [] };
3281
+ }
3228
3282
  function deliverable(base, ships) {
3229
3283
  if (ships.everywhere || !ships.countries.length) return {};
3230
3284
  const known = new Map((base.countries ?? []).map((c) => [String(c.code).toUpperCase(), c]));