@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.
@@ -174,6 +174,41 @@ function metaBrowser() {
174
174
  event_source_url: window.location.origin + window.location.pathname
175
175
  };
176
176
  }
177
+ var ATTRIBUTION_KEY = "gc_attribution";
178
+ var ATTRIBUTION_DAYS = 30;
179
+ var TOUCH_FIELDS = [
180
+ "utm_source",
181
+ "utm_medium",
182
+ "utm_campaign",
183
+ "utm_content",
184
+ "utm_term",
185
+ "gclid",
186
+ "gbraid",
187
+ "wbraid",
188
+ "fbclid",
189
+ "ttclid",
190
+ "msclkid",
191
+ "referrer",
192
+ "at"
193
+ ];
194
+ function cleanTouch(raw, now) {
195
+ if (!raw || typeof raw !== "object") return void 0;
196
+ const at = Date.parse(String(raw.at ?? ""));
197
+ if (!Number.isFinite(at) || now - at > ATTRIBUTION_DAYS * 864e5 || at - now > 864e5) return void 0;
198
+ const out = {};
199
+ for (const k of TOUCH_FIELDS) {
200
+ const v = raw[k];
201
+ if (typeof v === "string" && v.trim()) out[k] = v.trim().slice(0, 200);
202
+ }
203
+ return Object.keys(out).length > 1 ? out : void 0;
204
+ }
205
+ function landingAttribution(now = Date.now()) {
206
+ const saved = readJSON(ATTRIBUTION_KEY);
207
+ const last = cleanTouch(saved?.last, now);
208
+ const first = cleanTouch(saved?.first, now) ?? last;
209
+ if (!first && !last) return void 0;
210
+ return { ...first ? { first } : {}, ...last ? { last } : {} };
211
+ }
177
212
  function storedCartId() {
178
213
  const v = readLocal("cart_id");
179
214
  if (!v || v === "undefined" || v === "null") return null;
@@ -2202,10 +2237,18 @@ var CheckoutService = class extends BaseService {
2202
2237
  const retry = extras.attempt?.body === `${code}:${serialised}`;
2203
2238
  const key = retry ? extras.attempt.key : newKey();
2204
2239
  const meta = retry ? extras.attempt?.meta : metaBrowser();
2205
- saveCartExtras(id2, { attempt: { key, body: `${code}:${serialised}`, ...meta ? { meta } : {} } });
2240
+ const attribution = retry ? extras.attempt?.attribution : landingAttribution();
2241
+ saveCartExtras(id2, {
2242
+ attempt: { key, body: `${code}:${serialised}`, ...meta ? { meta } : {}, ...attribution ? { attribution } : {} }
2243
+ });
2244
+ const metadata = {
2245
+ ...extra.metadata,
2246
+ ...meta ? { meta_capi: meta } : {},
2247
+ ...attribution ? { attribution } : {}
2248
+ };
2206
2249
  const result = await this.data(`/api/checkout/${encodeURIComponent(code)}`, {
2207
2250
  method: "POST",
2208
- body: meta ? { ...body, metadata: { ...extra.metadata, meta_capi: meta } } : body,
2251
+ body: Object.keys(metadata).length ? { ...body, metadata } : body,
2209
2252
  headers: { "Idempotency-Key": key }
2210
2253
  });
2211
2254
  if (result.order.access_token) {