@misiki/gocommerce-connector 0.21.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) {