@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.
@@ -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) {
@@ -3168,6 +3211,7 @@ var StoreService = class extends BaseService {
3168
3211
  // one storefront serves every store, and one store's pixel must never
3169
3212
  // load on another's pages.
3170
3213
  ...adTags(plugins),
3214
+ googleTagManager: googleTags(plugins),
3171
3215
  // Discount codes are core: every GoCommerce store takes them, so
3172
3216
  // the code box is shown whatever the static record says.
3173
3217
  isDiscountCoupons: { ...basePlugins.isDiscountCoupons, active: true },
@@ -3209,6 +3253,16 @@ function adTags(plugins) {
3209
3253
  })
3210
3254
  );
3211
3255
  }
3256
+ function googleTags(plugins) {
3257
+ const s = plugins.get("google-analytics")?.settings ?? {};
3258
+ const id2 = (field) => String(s[field] ?? "").trim();
3259
+ const [account, part] = id2("conversion_label").split("/");
3260
+ const label = part === void 0 ? account : part;
3261
+ const ads = id2("google_ads_id") || (part === void 0 ? "" : account);
3262
+ const tags = [id2("ga4_measurement_id"), ads, id2("gtm_container_id")].filter(Boolean);
3263
+ if (!tags.length) return { active: false };
3264
+ return { active: true, tags, conversions: ads && label ? [{ event: "purchase", label }] : [] };
3265
+ }
3212
3266
  function deliverable(base, ships) {
3213
3267
  if (ships.everywhere || !ships.countries.length) return {};
3214
3268
  const known = new Map((base.countries ?? []).map((c) => [String(c.code).toUpperCase(), c]));