@konfiqo/storefront-react 0.5.0 → 0.6.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.
package/dist/index.cjs CHANGED
@@ -31,22 +31,35 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
31
31
  // src/index.ts
32
32
  var index_exports = {};
33
33
  __export(index_exports, {
34
+ AdPixelsLoader: () => AdPixelsLoader,
35
+ AnalyticsTracker: () => AnalyticsTracker,
34
36
  CheckoutChallenge: () => CheckoutChallenge,
37
+ ConsentProvider: () => ConsentProvider,
35
38
  Content: () => Content,
39
+ OPTIONAL_CATEGORIES: () => OPTIONAL_CATEGORIES,
36
40
  ORDER_POLL_MAX_MS: () => ORDER_POLL_MAX_MS,
37
41
  ORDER_POLL_MIN_MS: () => ORDER_POLL_MIN_MS,
38
42
  PaymentForm: () => PaymentForm,
39
43
  StorefrontProvider: () => StorefrontProvider,
44
+ configureAnalytics: () => configureAnalytics,
45
+ configurePixels: () => configurePixels,
40
46
  defaultCartStorage: () => defaultCartStorage,
47
+ firePixel: () => firePixel,
41
48
  isDeadCartError: () => isDeadCartError,
42
49
  isOrderSettled: () => isOrderSettled,
50
+ onSecretPage: () => onSecretPage,
43
51
  orderPollDelay: () => orderPollDelay,
52
+ recordConsent: () => recordConsent,
44
53
  renderContent: () => renderContent,
54
+ resetAnalytics: () => resetAnalytics,
55
+ resetPixels: () => resetPixels,
56
+ track: () => track,
45
57
  useAccount: () => useAccount,
46
58
  useCart: () => useCart,
47
59
  useCartStorage: () => useCartStorage,
48
60
  useCategories: () => useCategories,
49
61
  useCheckout: () => useCheckout,
62
+ useConsent: () => useConsent,
50
63
  useOrder: () => useOrder,
51
64
  useProduct: () => useProduct,
52
65
  useProducts: () => useProducts,
@@ -452,22 +465,22 @@ var StorefrontContext = (0, import_react.createContext)(null);
452
465
  function StorefrontProvider({
453
466
  storeId,
454
467
  client,
455
- config,
468
+ config: config2,
456
469
  cartStorage,
457
470
  children
458
471
  }) {
459
472
  const forbidden = ["serverKey", "trustedStorefrontKey", "buyerIp"].filter(
460
- (k) => config !== void 0 && k in config
473
+ (k) => config2 !== void 0 && k in config2
461
474
  );
462
475
  if (forbidden.length > 0) {
463
476
  throw new Error(
464
477
  `StorefrontProvider: config.${forbidden.join(", config.")} must not be passed to a client component (it would reach the browser). Create a server-side client instead.`
465
478
  );
466
479
  }
467
- const baseUrl = config?.baseUrl;
468
- const publishableKey = config?.publishableKey;
469
- const timeoutMs = config?.timeoutMs;
470
- const customFetch = config?.fetch;
480
+ const baseUrl = config2?.baseUrl;
481
+ const publishableKey = config2?.publishableKey;
482
+ const timeoutMs = config2?.timeoutMs;
483
+ const customFetch = config2?.fetch;
471
484
  const resolved = (0, import_react.useMemo)(() => {
472
485
  if (client) return client;
473
486
  if (baseUrl === void 0 || publishableKey === void 0) {
@@ -1126,24 +1139,811 @@ function Content({ contentKey, components }) {
1126
1139
  if (!data) return null;
1127
1140
  return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(import_jsx_runtime4.Fragment, { children: renderContent(data.tree, components) });
1128
1141
  }
1142
+
1143
+ // src/consent-provider.tsx
1144
+ var import_react10 = require("react");
1145
+
1146
+ // src/consent.ts
1147
+ var COOKIE = "_consent";
1148
+ var MAX_AGE = 31536e3;
1149
+ var ID = /^[0-9a-f]{32}$/;
1150
+ var OPTIONAL_CATEGORIES = [
1151
+ "analytics",
1152
+ "marketing"
1153
+ ];
1154
+ var currentVersion = "";
1155
+ var listeners = /* @__PURE__ */ new Set();
1156
+ function configureConsent(version) {
1157
+ if (typeof window === "undefined") return;
1158
+ currentVersion = version;
1159
+ }
1160
+ function subscribeConsent(listener) {
1161
+ listeners.add(listener);
1162
+ return () => listeners.delete(listener);
1163
+ }
1164
+ function rawConsent() {
1165
+ try {
1166
+ for (const part of document.cookie.split(";")) {
1167
+ const eq = part.indexOf("=");
1168
+ if (eq < 0 || part.slice(0, eq).trim() !== COOKIE) continue;
1169
+ return part.slice(eq + 1).trim();
1170
+ }
1171
+ } catch {
1172
+ }
1173
+ return "";
1174
+ }
1175
+ function parse(raw) {
1176
+ const parts = raw.split(".");
1177
+ if (parts.length !== 3) return null;
1178
+ const [id, version, list] = parts;
1179
+ if (!ID.test(id)) return null;
1180
+ const items = list === "" ? [] : list.split(",");
1181
+ if (!items.every((c) => OPTIONAL_CATEGORIES.includes(c)))
1182
+ return null;
1183
+ return { id, version, granted: items };
1184
+ }
1185
+ function readConsent(version) {
1186
+ return consentFor(rawConsent(), version);
1187
+ }
1188
+ function consentFor(raw, version) {
1189
+ const parsed = parse(raw);
1190
+ if (!parsed || parsed.version !== version) return null;
1191
+ return { id: parsed.id, granted: parsed.granted };
1192
+ }
1193
+ function newConsentId() {
1194
+ const bytes = crypto.getRandomValues(new Uint8Array(16));
1195
+ return Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
1196
+ }
1197
+ function writeConsent(version, granted2) {
1198
+ const id = parse(rawConsent())?.id ?? newConsentId();
1199
+ const list = OPTIONAL_CATEGORIES.filter((c) => granted2.includes(c)).join(",");
1200
+ try {
1201
+ document.cookie = `${COOKIE}=${id}.${version}.${list}; Max-Age=${MAX_AGE}; Path=/; SameSite=Lax; Secure`;
1202
+ } catch {
1203
+ }
1204
+ for (const listener of listeners) listener();
1205
+ return id;
1206
+ }
1207
+ function analyticsConsent() {
1208
+ if (!currentVersion) return false;
1209
+ return readConsent(currentVersion)?.granted.includes("analytics") ?? false;
1210
+ }
1211
+
1212
+ // src/consent-provider.tsx
1213
+ var import_jsx_runtime5 = require("react/jsx-runtime");
1214
+ var noop = () => void 0;
1215
+ var ConsentContext = (0, import_react10.createContext)({
1216
+ needed: false,
1217
+ open: noop,
1218
+ cookies: [],
1219
+ categories: [],
1220
+ known: false,
1221
+ stored: null,
1222
+ showBar: false,
1223
+ panelOpen: false,
1224
+ close: noop,
1225
+ decide: noop
1226
+ });
1227
+ var serverSnapshot = () => void 0;
1228
+ function ConsentProvider({
1229
+ cookies,
1230
+ version,
1231
+ onDecision,
1232
+ children
1233
+ }) {
1234
+ configureConsent(version);
1235
+ const raw = (0, import_react10.useSyncExternalStore)(
1236
+ subscribeConsent,
1237
+ rawConsent,
1238
+ serverSnapshot
1239
+ );
1240
+ const [panelOpen, setPanelOpen] = (0, import_react10.useState)(false);
1241
+ const [decidedVersion, setDecidedVersion] = (0, import_react10.useState)(null);
1242
+ const opener = (0, import_react10.useRef)(null);
1243
+ const onDecisionRef = (0, import_react10.useRef)(onDecision);
1244
+ onDecisionRef.current = onDecision;
1245
+ const categories = (0, import_react10.useMemo)(
1246
+ () => OPTIONAL_CATEGORIES.filter((c) => cookies.some((k) => k.category === c)),
1247
+ [cookies]
1248
+ );
1249
+ const needed = categories.length > 0;
1250
+ const stored = (0, import_react10.useMemo)(
1251
+ () => raw === void 0 ? null : consentFor(raw, version),
1252
+ [raw, version]
1253
+ );
1254
+ const known = raw !== void 0;
1255
+ const showBar = needed && known && !stored && decidedVersion !== version;
1256
+ const restorePending = (0, import_react10.useRef)(false);
1257
+ (0, import_react10.useEffect)(() => {
1258
+ if (panelOpen || !restorePending.current) return;
1259
+ restorePending.current = false;
1260
+ const target = opener.current;
1261
+ opener.current = null;
1262
+ if (target?.isConnected) target.focus();
1263
+ else document.getElementById("main")?.focus({ preventScroll: true });
1264
+ }, [panelOpen, showBar]);
1265
+ const open = (0, import_react10.useCallback)((from) => {
1266
+ const active = from ?? document.activeElement;
1267
+ opener.current = active instanceof HTMLElement && active !== document.body ? active : null;
1268
+ setPanelOpen(true);
1269
+ }, []);
1270
+ const close = (0, import_react10.useCallback)(() => {
1271
+ if (panelOpen) restorePending.current = true;
1272
+ setPanelOpen(false);
1273
+ }, [panelOpen]);
1274
+ const decide = (0, import_react10.useCallback)(
1275
+ (granted2) => {
1276
+ const allowed = categories.filter((c) => granted2.includes(c));
1277
+ const consentId = writeConsent(version, allowed);
1278
+ try {
1279
+ onDecisionRef.current?.({ consentId, granted: allowed, version });
1280
+ } catch {
1281
+ }
1282
+ setDecidedVersion(version);
1283
+ if (!panelOpen) opener.current = null;
1284
+ restorePending.current = true;
1285
+ setPanelOpen(false);
1286
+ },
1287
+ [categories, version, panelOpen]
1288
+ );
1289
+ const value = (0, import_react10.useMemo)(
1290
+ () => ({
1291
+ needed,
1292
+ open,
1293
+ cookies,
1294
+ categories,
1295
+ stored,
1296
+ known,
1297
+ showBar,
1298
+ panelOpen,
1299
+ close,
1300
+ decide
1301
+ }),
1302
+ [
1303
+ needed,
1304
+ open,
1305
+ cookies,
1306
+ categories,
1307
+ stored,
1308
+ known,
1309
+ showBar,
1310
+ panelOpen,
1311
+ close,
1312
+ decide
1313
+ ]
1314
+ );
1315
+ return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(ConsentContext.Provider, { value, children });
1316
+ }
1317
+ function useConsent() {
1318
+ return (0, import_react10.useContext)(ConsentContext);
1319
+ }
1320
+
1321
+ // src/analytics.ts
1322
+ var QUERY_ALLOWLIST = [
1323
+ "utm_source",
1324
+ "utm_medium",
1325
+ "utm_campaign",
1326
+ "utm_content",
1327
+ "utm_term",
1328
+ "fbclid",
1329
+ "ttclid",
1330
+ "gclid",
1331
+ "gbraid",
1332
+ "wbraid"
1333
+ ];
1334
+ var MAX_URL = 2e3;
1335
+ var MAX_BATCH = 20;
1336
+ function safePath() {
1337
+ const path = location.pathname;
1338
+ return path.startsWith("/orders/") ? "/orders/:id" : path;
1339
+ }
1340
+ function safeQuery() {
1341
+ const path = location.pathname;
1342
+ if (path.startsWith("/orders/") || path.startsWith("/cart/recover"))
1343
+ return "";
1344
+ const source = new URLSearchParams(location.search);
1345
+ const kept = new URLSearchParams();
1346
+ for (const [key, value] of source) {
1347
+ if (QUERY_ALLOWLIST.includes(key)) kept.append(key, value);
1348
+ }
1349
+ const query = kept.toString();
1350
+ return query ? `?${query}` : "";
1351
+ }
1352
+ function referrerOrigin() {
1353
+ try {
1354
+ return document.referrer ? new URL(document.referrer).origin : null;
1355
+ } catch {
1356
+ return null;
1357
+ }
1358
+ }
1359
+ var PREVIEW_KEY = "_preview";
1360
+ var config = null;
1361
+ var first = true;
1362
+ var landingChecked = false;
1363
+ var queue = [];
1364
+ var queuedViews = [];
1365
+ function configureAnalytics(next) {
1366
+ config = next;
1367
+ }
1368
+ function resetAnalytics() {
1369
+ config = null;
1370
+ first = true;
1371
+ landingChecked = false;
1372
+ queue = [];
1373
+ queuedViews = [];
1374
+ }
1375
+ function previewQuery() {
1376
+ return new URLSearchParams(location.search).get(PREVIEW_KEY) === "1";
1377
+ }
1378
+ function readLanding() {
1379
+ if (landingChecked) return;
1380
+ landingChecked = true;
1381
+ try {
1382
+ if (previewQuery()) sessionStorage.setItem(PREVIEW_KEY, "1");
1383
+ } catch {
1384
+ }
1385
+ }
1386
+ function isPreview() {
1387
+ if (!landingChecked && previewQuery()) return true;
1388
+ try {
1389
+ return sessionStorage.getItem(PREVIEW_KEY) === "1";
1390
+ } catch {
1391
+ return false;
1392
+ }
1393
+ }
1394
+ function track(type, extra = {}) {
1395
+ try {
1396
+ if (!config) return;
1397
+ readLanding();
1398
+ if (isPreview()) return;
1399
+ if (type === "page_view") {
1400
+ const path = safePath();
1401
+ if (queuedViews.includes(path)) return;
1402
+ queuedViews.push(path);
1403
+ }
1404
+ const url = (first ? safePath() + safeQuery() : safePath()).slice(
1405
+ 0,
1406
+ MAX_URL
1407
+ );
1408
+ const referrer = first ? referrerOrigin() : null;
1409
+ first = false;
1410
+ const { productId } = extra;
1411
+ if (queue.length === 0) queueMicrotask(flush);
1412
+ queue.push({ type, url, referrer, ...productId ? { productId } : {} });
1413
+ } catch {
1414
+ }
1415
+ }
1416
+ function send(cfg, path, body) {
1417
+ void fetch(`${cfg.baseUrl}${path}`, {
1418
+ method: "POST",
1419
+ keepalive: true,
1420
+ headers: {
1421
+ "content-type": "application/json",
1422
+ "x-konfiqo-publishable-key": cfg.publishableKey
1423
+ },
1424
+ body: JSON.stringify(body)
1425
+ }).catch(() => void 0);
1426
+ }
1427
+ function flush() {
1428
+ try {
1429
+ const events = queue;
1430
+ queue = [];
1431
+ queuedViews = [];
1432
+ if (!config) return;
1433
+ const consent = { analytics: analyticsConsent() };
1434
+ for (let i = 0; i < events.length; i += MAX_BATCH) {
1435
+ send(config, "/v1/events", {
1436
+ events: events.slice(i, i + MAX_BATCH),
1437
+ consent
1438
+ });
1439
+ }
1440
+ } catch {
1441
+ }
1442
+ }
1443
+ function recordConsent(record) {
1444
+ try {
1445
+ if (!config || isPreview()) return;
1446
+ send(config, "/v1/consents", record);
1447
+ } catch {
1448
+ }
1449
+ }
1450
+
1451
+ // src/analytics-tracker.tsx
1452
+ var import_react11 = require("react");
1453
+ function AnalyticsTracker({
1454
+ baseUrl,
1455
+ publishableKey,
1456
+ pathname
1457
+ }) {
1458
+ configureAnalytics({ baseUrl, publishableKey });
1459
+ (0, import_react11.useEffect)(() => {
1460
+ track("page_view");
1461
+ }, [pathname]);
1462
+ return null;
1463
+ }
1464
+
1465
+ // src/ad-pixels.ts
1466
+ function itemOf(e) {
1467
+ return e.variantId ? { id: e.variantId, type: "product" } : { id: e.productId, type: "product_group" };
1468
+ }
1469
+ var DEFAULT_CURRENCY = "TRY";
1470
+ var PURCHASE_KEY = "px_purchase_";
1471
+ var GRANT_MARKER_KEY = "px_granted";
1472
+ var win = () => window;
1473
+ var pixels;
1474
+ var currency = DEFAULT_CURRENCY;
1475
+ var granted = false;
1476
+ var loaded = /* @__PURE__ */ new Set();
1477
+ function resetPixels() {
1478
+ pixels = void 0;
1479
+ currency = DEFAULT_CURRENCY;
1480
+ granted = false;
1481
+ optedOut = false;
1482
+ loaded.clear();
1483
+ }
1484
+ var major = (minor) => minor / 100;
1485
+ function addScript(src) {
1486
+ const el = document.createElement("script");
1487
+ el.async = true;
1488
+ el.src = src;
1489
+ document.head.appendChild(el);
1490
+ }
1491
+ function loadMeta(id) {
1492
+ const w = win();
1493
+ if (!w.fbq) {
1494
+ const fbq = function(...args) {
1495
+ const f = fbq;
1496
+ if (f.callMethod) f.callMethod(...args);
1497
+ else f.queue.push(args);
1498
+ };
1499
+ Object.assign(fbq, {
1500
+ push: fbq,
1501
+ loaded: true,
1502
+ version: "2.0",
1503
+ queue: []
1504
+ });
1505
+ w.fbq = fbq;
1506
+ w._fbq = fbq;
1507
+ }
1508
+ w.fbq.disablePushState = true;
1509
+ addScript("https://connect.facebook.net/en_US/fbevents.js");
1510
+ w.fbq("init", id);
1511
+ }
1512
+ var TT_METHODS = [
1513
+ "page",
1514
+ "track",
1515
+ "identify",
1516
+ "instances",
1517
+ "debug",
1518
+ "on",
1519
+ "off",
1520
+ "once",
1521
+ "ready",
1522
+ "alias",
1523
+ "group",
1524
+ "enableCookie",
1525
+ "disableCookie",
1526
+ "holdConsent",
1527
+ "revokeConsent",
1528
+ "grantConsent"
1529
+ ];
1530
+ function loadTikTok(id) {
1531
+ const w = win();
1532
+ w.TiktokAnalyticsObject = "ttq";
1533
+ const ttq = w.ttq = w.ttq ?? [];
1534
+ ttq.methods = TT_METHODS;
1535
+ ttq.setAndDefer = (t, m) => {
1536
+ t[m] = (...args) => {
1537
+ t.push([m, ...args]);
1538
+ };
1539
+ };
1540
+ for (const m of ttq.methods) ttq.setAndDefer(ttq, m);
1541
+ ttq.instance = (name) => {
1542
+ const inst = ttq._i[name] ?? [];
1543
+ for (const m of ttq.methods) ttq.setAndDefer(inst, m);
1544
+ return inst;
1545
+ };
1546
+ ttq.load = (name, opts) => {
1547
+ const src = "https://analytics.tiktok.com/i18n/pixel/events.js";
1548
+ ttq._i = ttq._i ?? {};
1549
+ ttq._i[name] = [];
1550
+ ttq._i[name]._u = src;
1551
+ ttq._t = ttq._t ?? {};
1552
+ ttq._t[name] = Date.now();
1553
+ ttq._o = ttq._o ?? {};
1554
+ ttq._o[name] = opts ?? {};
1555
+ addScript(`${src}?sdkid=${encodeURIComponent(name)}&lib=ttq`);
1556
+ };
1557
+ ttq.load(id);
1558
+ }
1559
+ function loadGoogle(g) {
1560
+ const w = win();
1561
+ const ids = [g.ga4Id, g.adsId].filter((v) => !!v);
1562
+ if (ids.length === 0) return;
1563
+ w.dataLayer = w.dataLayer ?? [];
1564
+ if (!w.gtag) {
1565
+ w.gtag = function() {
1566
+ w.dataLayer.push(arguments);
1567
+ };
1568
+ }
1569
+ addScript(
1570
+ `https://www.googletagmanager.com/gtag/js?id=${encodeURIComponent(ids[0])}`
1571
+ );
1572
+ w.gtag("js", /* @__PURE__ */ new Date());
1573
+ const cookieDomain = location.hostname;
1574
+ w.gtag("set", { cookie_domain: cookieDomain });
1575
+ for (const id of ids) {
1576
+ w.gtag("config", id, {
1577
+ send_page_view: false,
1578
+ cookie_domain: cookieDomain
1579
+ });
1580
+ }
1581
+ }
1582
+ function onSecretPage(path = typeof location === "undefined" ? "" : location.pathname) {
1583
+ return path.startsWith("/orders/") || path.startsWith("/cart/recover");
1584
+ }
1585
+ function loadAll(p) {
1586
+ if (p.meta && !loaded.has("meta")) {
1587
+ loaded.add("meta");
1588
+ loadMeta(p.meta.pixelId);
1589
+ }
1590
+ if (p.tiktok && !loaded.has("tiktok")) {
1591
+ loaded.add("tiktok");
1592
+ loadTikTok(p.tiktok.pixelId);
1593
+ }
1594
+ if (p.google && !loaded.has("google")) {
1595
+ loaded.add("google");
1596
+ loadGoogle(p.google);
1597
+ }
1598
+ }
1599
+ var PLATFORM_COOKIES = {
1600
+ meta: { names: ["_fbp", "_fbc"], prefixes: [] },
1601
+ tiktok: {
1602
+ names: ["_ttp", "_tt_enable_cookie", "ttcsid"],
1603
+ prefixes: ["ttcsid_"]
1604
+ },
1605
+ google: { names: ["_ga", "_gcl_au", "_gcl_aw"], prefixes: ["_ga_"] }
1606
+ };
1607
+ function platformsOf(p) {
1608
+ return ["meta", "tiktok", "google"].filter((k) => !!p[k]);
1609
+ }
1610
+ function readMarker() {
1611
+ try {
1612
+ return localStorage.getItem(GRANT_MARKER_KEY) !== null;
1613
+ } catch {
1614
+ return false;
1615
+ }
1616
+ }
1617
+ function writeMarker(on) {
1618
+ try {
1619
+ if (on) localStorage.setItem(GRANT_MARKER_KEY, "1");
1620
+ else localStorage.removeItem(GRANT_MARKER_KEY);
1621
+ } catch {
1622
+ }
1623
+ }
1624
+ var PROBE = "_px_probe";
1625
+ function registrableDomain(host) {
1626
+ const parts = host.split(".");
1627
+ for (let i = parts.length - 2; i > 0; i--) {
1628
+ const candidate = parts.slice(i).join(".");
1629
+ document.cookie = `${PROBE}=1; Path=/; Domain=${candidate}`;
1630
+ if (document.cookie.split(";").some((c) => c.trim() === `${PROBE}=1`)) {
1631
+ document.cookie = `${PROBE}=; Max-Age=0; Path=/; Domain=${candidate}`;
1632
+ return candidate;
1633
+ }
1634
+ }
1635
+ return host;
1636
+ }
1637
+ function clearCookies(p, onlyHost) {
1638
+ try {
1639
+ const spec = platformsOf(p).map((k) => PLATFORM_COOKIES[k]);
1640
+ const present = document.cookie.split(";").map((c) => c.split("=")[0].trim()).filter(Boolean);
1641
+ const names = present.filter(
1642
+ (n) => spec.some(
1643
+ (s) => s.names.includes(n) || s.prefixes.some((x) => n.startsWith(x))
1644
+ )
1645
+ );
1646
+ if (names.length === 0) return;
1647
+ const host = location.hostname;
1648
+ const domains = [void 0, host];
1649
+ if (!onlyHost && !/^[\d.]+$/.test(host) && !host.includes(":")) {
1650
+ const site = registrableDomain(host);
1651
+ if (site !== host) domains.push(site);
1652
+ }
1653
+ for (const name of names) {
1654
+ for (const domain of domains) {
1655
+ document.cookie = `${name}=; Max-Age=0; Path=/${domain ? `; Domain=${domain}` : ""}`;
1656
+ }
1657
+ }
1658
+ } catch {
1659
+ }
1660
+ }
1661
+ var optedOut = false;
1662
+ var GOOGLE_CONSENT = (v) => ({
1663
+ ad_storage: v,
1664
+ analytics_storage: v,
1665
+ ad_user_data: v,
1666
+ ad_personalization: v
1667
+ });
1668
+ function googleIds(g) {
1669
+ return [g?.ga4Id, g?.adsId].filter((v) => !!v);
1670
+ }
1671
+ function optOut(p) {
1672
+ optedOut = true;
1673
+ const w = win();
1674
+ for (const id of googleIds(p.google)) w[`ga-disable-${id}`] = true;
1675
+ if (loaded.has("google") && typeof w.gtag === "function") {
1676
+ w.gtag("consent", "update", GOOGLE_CONSENT("denied"));
1677
+ }
1678
+ if (loaded.has("meta") && typeof w.fbq === "function") {
1679
+ w.fbq("consent", "revoke");
1680
+ }
1681
+ const ttq = w.ttq;
1682
+ if (loaded.has("tiktok") && typeof ttq?.disableCookie === "function") {
1683
+ ttq.disableCookie();
1684
+ }
1685
+ }
1686
+ function optIn(p) {
1687
+ optedOut = false;
1688
+ const w = win();
1689
+ for (const id of googleIds(p.google)) w[`ga-disable-${id}`] = false;
1690
+ if (loaded.has("google") && typeof w.gtag === "function") {
1691
+ w.gtag("consent", "update", GOOGLE_CONSENT("granted"));
1692
+ }
1693
+ if (loaded.has("meta") && typeof w.fbq === "function") {
1694
+ w.fbq("consent", "grant");
1695
+ }
1696
+ const ttq = w.ttq;
1697
+ if (loaded.has("tiktok") && typeof ttq?.enableCookie === "function") {
1698
+ ttq.enableCookie();
1699
+ }
1700
+ }
1701
+ function configurePixels(next, isGranted, opts = {}) {
1702
+ try {
1703
+ pixels = next;
1704
+ currency = opts.currency ?? DEFAULT_CURRENCY;
1705
+ const was = granted;
1706
+ granted = isGranted && !!next;
1707
+ if (granted && next) {
1708
+ if (optedOut) optIn(next);
1709
+ if (!onSecretPage()) loadAll(next);
1710
+ if (platformsOf(next).length > 0) writeMarker(true);
1711
+ } else if (next) {
1712
+ if (was) optOut(next);
1713
+ if (was || readMarker()) {
1714
+ clearCookies(next, opts.hostOnly ?? false);
1715
+ writeMarker(false);
1716
+ }
1717
+ }
1718
+ } catch {
1719
+ }
1720
+ }
1721
+ function purchased(orderId) {
1722
+ try {
1723
+ return localStorage.getItem(PURCHASE_KEY + orderId) !== null;
1724
+ } catch {
1725
+ return false;
1726
+ }
1727
+ }
1728
+ function markPurchased(orderId) {
1729
+ try {
1730
+ localStorage.setItem(PURCHASE_KEY + orderId, "1");
1731
+ } catch {
1732
+ }
1733
+ }
1734
+ function firePixel(e) {
1735
+ try {
1736
+ if (!granted || !pixels || onSecretPage()) return;
1737
+ loadAll(pixels);
1738
+ if (e.name === "purchase") {
1739
+ if (purchased(e.orderId)) return;
1740
+ markPurchased(e.orderId);
1741
+ }
1742
+ const w = win();
1743
+ if (pixels.meta && w.fbq) meta(w.fbq, e);
1744
+ if (pixels.tiktok && w.ttq) tiktok(w.ttq, e);
1745
+ if (pixels.google && w.gtag) google(w.gtag, pixels.google, e);
1746
+ } catch {
1747
+ }
1748
+ }
1749
+ function meta(fbq, e) {
1750
+ switch (e.name) {
1751
+ case "page_view":
1752
+ fbq("track", "PageView");
1753
+ return;
1754
+ case "view_item": {
1755
+ const it = itemOf(e);
1756
+ fbq("track", "ViewContent", {
1757
+ content_ids: [it.id],
1758
+ content_type: it.type,
1759
+ value: major(e.priceMinor),
1760
+ currency
1761
+ });
1762
+ return;
1763
+ }
1764
+ case "add_to_cart":
1765
+ fbq("track", "AddToCart", {
1766
+ content_ids: [e.variantId],
1767
+ content_type: "product",
1768
+ contents: [
1769
+ {
1770
+ id: e.variantId,
1771
+ quantity: e.quantity,
1772
+ item_price: major(e.priceMinor)
1773
+ }
1774
+ ],
1775
+ value: major(e.priceMinor * e.quantity),
1776
+ currency
1777
+ });
1778
+ return;
1779
+ case "begin_checkout":
1780
+ fbq("track", "InitiateCheckout", {
1781
+ value: major(e.valueMinor),
1782
+ currency
1783
+ });
1784
+ return;
1785
+ case "purchase":
1786
+ fbq(
1787
+ "track",
1788
+ "Purchase",
1789
+ {
1790
+ content_ids: e.items.map((i) => i.id),
1791
+ content_type: "product",
1792
+ contents: e.items.map((i) => ({
1793
+ id: i.id,
1794
+ quantity: i.quantity,
1795
+ item_price: major(i.priceMinor)
1796
+ })),
1797
+ value: major(e.valueMinor),
1798
+ currency
1799
+ },
1800
+ { eventID: e.orderId }
1801
+ );
1802
+ }
1803
+ }
1804
+ function tiktok(ttq, e) {
1805
+ const content = (id, quantity, priceMinor, type = "product") => ({
1806
+ content_id: id,
1807
+ content_type: type,
1808
+ quantity,
1809
+ price: major(priceMinor)
1810
+ });
1811
+ switch (e.name) {
1812
+ case "page_view":
1813
+ ttq.page();
1814
+ return;
1815
+ case "view_item": {
1816
+ const it = itemOf(e);
1817
+ ttq.track("ViewContent", {
1818
+ contents: [content(it.id, 1, e.priceMinor, it.type)],
1819
+ value: major(e.priceMinor),
1820
+ currency
1821
+ });
1822
+ return;
1823
+ }
1824
+ case "add_to_cart":
1825
+ ttq.track("AddToCart", {
1826
+ contents: [content(e.variantId, e.quantity, e.priceMinor)],
1827
+ value: major(e.priceMinor * e.quantity),
1828
+ currency
1829
+ });
1830
+ return;
1831
+ case "begin_checkout":
1832
+ ttq.track("InitiateCheckout", {
1833
+ value: major(e.valueMinor),
1834
+ currency
1835
+ });
1836
+ return;
1837
+ case "purchase":
1838
+ ttq.track(
1839
+ "CompletePayment",
1840
+ {
1841
+ contents: e.items.map((i) => content(i.id, i.quantity, i.priceMinor)),
1842
+ value: major(e.valueMinor),
1843
+ currency
1844
+ },
1845
+ { event_id: e.orderId }
1846
+ );
1847
+ }
1848
+ }
1849
+ function google(gtag, g, e) {
1850
+ const item = (id, quantity, priceMinor) => ({
1851
+ item_id: id,
1852
+ quantity,
1853
+ price: major(priceMinor)
1854
+ });
1855
+ switch (e.name) {
1856
+ case "page_view":
1857
+ gtag("event", "page_view");
1858
+ return;
1859
+ case "view_item":
1860
+ gtag("event", "view_item", {
1861
+ currency,
1862
+ value: major(e.priceMinor),
1863
+ items: [item(itemOf(e).id, 1, e.priceMinor)]
1864
+ });
1865
+ return;
1866
+ case "add_to_cart":
1867
+ gtag("event", "add_to_cart", {
1868
+ currency,
1869
+ value: major(e.priceMinor * e.quantity),
1870
+ items: [item(e.variantId, e.quantity, e.priceMinor)]
1871
+ });
1872
+ return;
1873
+ case "begin_checkout":
1874
+ gtag("event", "begin_checkout", {
1875
+ currency,
1876
+ value: major(e.valueMinor)
1877
+ });
1878
+ return;
1879
+ case "purchase":
1880
+ gtag("event", "purchase", {
1881
+ transaction_id: e.orderId,
1882
+ currency,
1883
+ value: major(e.valueMinor),
1884
+ items: e.items.map((i) => item(i.id, i.quantity, i.priceMinor))
1885
+ });
1886
+ if (g.adsId && g.adsConversionLabel) {
1887
+ gtag("event", "conversion", {
1888
+ send_to: `${g.adsId}/${g.adsConversionLabel}`,
1889
+ value: major(e.valueMinor),
1890
+ currency,
1891
+ transaction_id: e.orderId
1892
+ });
1893
+ }
1894
+ }
1895
+ }
1896
+
1897
+ // src/ad-pixels-loader.tsx
1898
+ var import_react12 = require("react");
1899
+ function AdPixelsLoader({
1900
+ pixels: pixels2,
1901
+ hostOnly = false,
1902
+ currency: currency2 = "TRY",
1903
+ pathname
1904
+ }) {
1905
+ const { stored, known } = useConsent();
1906
+ const granted2 = stored?.granted.includes("marketing") ?? false;
1907
+ (0, import_react12.useEffect)(() => {
1908
+ if (!known) return;
1909
+ configurePixels(pixels2, granted2, { hostOnly, currency: currency2 });
1910
+ }, [pixels2, granted2, known, hostOnly, currency2]);
1911
+ (0, import_react12.useEffect)(() => {
1912
+ if (granted2) firePixel({ name: "page_view" });
1913
+ }, [granted2, pathname]);
1914
+ return null;
1915
+ }
1129
1916
  // Annotate the CommonJS export names for ESM import in node:
1130
1917
  0 && (module.exports = {
1918
+ AdPixelsLoader,
1919
+ AnalyticsTracker,
1131
1920
  CheckoutChallenge,
1921
+ ConsentProvider,
1132
1922
  Content,
1923
+ OPTIONAL_CATEGORIES,
1133
1924
  ORDER_POLL_MAX_MS,
1134
1925
  ORDER_POLL_MIN_MS,
1135
1926
  PaymentForm,
1136
1927
  StorefrontProvider,
1928
+ configureAnalytics,
1929
+ configurePixels,
1137
1930
  defaultCartStorage,
1931
+ firePixel,
1138
1932
  isDeadCartError,
1139
1933
  isOrderSettled,
1934
+ onSecretPage,
1140
1935
  orderPollDelay,
1936
+ recordConsent,
1141
1937
  renderContent,
1938
+ resetAnalytics,
1939
+ resetPixels,
1940
+ track,
1142
1941
  useAccount,
1143
1942
  useCart,
1144
1943
  useCartStorage,
1145
1944
  useCategories,
1146
1945
  useCheckout,
1946
+ useConsent,
1147
1947
  useOrder,
1148
1948
  useProduct,
1149
1949
  useProducts,