@konfiqo/storefront-react 0.5.0 → 0.6.1

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,828 @@ 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
+ var configured = false;
1478
+ var MAX_EARLY = 20;
1479
+ var early = [];
1480
+ function resetPixels() {
1481
+ pixels = void 0;
1482
+ currency = DEFAULT_CURRENCY;
1483
+ granted = false;
1484
+ optedOut = false;
1485
+ loaded.clear();
1486
+ configured = false;
1487
+ early.length = 0;
1488
+ }
1489
+ var major = (minor) => minor / 100;
1490
+ function addScript(src) {
1491
+ const el = document.createElement("script");
1492
+ el.async = true;
1493
+ el.src = src;
1494
+ document.head.appendChild(el);
1495
+ }
1496
+ function loadMeta(id) {
1497
+ const w = win();
1498
+ if (!w.fbq) {
1499
+ const fbq = function(...args) {
1500
+ const f = fbq;
1501
+ if (f.callMethod) f.callMethod(...args);
1502
+ else f.queue.push(args);
1503
+ };
1504
+ Object.assign(fbq, {
1505
+ push: fbq,
1506
+ loaded: true,
1507
+ version: "2.0",
1508
+ queue: []
1509
+ });
1510
+ w.fbq = fbq;
1511
+ w._fbq = fbq;
1512
+ }
1513
+ w.fbq.disablePushState = true;
1514
+ addScript("https://connect.facebook.net/en_US/fbevents.js");
1515
+ w.fbq("init", id);
1516
+ }
1517
+ var TT_METHODS = [
1518
+ "page",
1519
+ "track",
1520
+ "identify",
1521
+ "instances",
1522
+ "debug",
1523
+ "on",
1524
+ "off",
1525
+ "once",
1526
+ "ready",
1527
+ "alias",
1528
+ "group",
1529
+ "enableCookie",
1530
+ "disableCookie",
1531
+ "holdConsent",
1532
+ "revokeConsent",
1533
+ "grantConsent"
1534
+ ];
1535
+ function loadTikTok(id) {
1536
+ const w = win();
1537
+ w.TiktokAnalyticsObject = "ttq";
1538
+ const ttq = w.ttq = w.ttq ?? [];
1539
+ ttq.methods = TT_METHODS;
1540
+ ttq.setAndDefer = (t, m) => {
1541
+ t[m] = (...args) => {
1542
+ t.push([m, ...args]);
1543
+ };
1544
+ };
1545
+ for (const m of ttq.methods) ttq.setAndDefer(ttq, m);
1546
+ ttq.instance = (name) => {
1547
+ const inst = ttq._i[name] ?? [];
1548
+ for (const m of ttq.methods) ttq.setAndDefer(inst, m);
1549
+ return inst;
1550
+ };
1551
+ ttq.load = (name, opts) => {
1552
+ const src = "https://analytics.tiktok.com/i18n/pixel/events.js";
1553
+ ttq._i = ttq._i ?? {};
1554
+ ttq._i[name] = [];
1555
+ ttq._i[name]._u = src;
1556
+ ttq._t = ttq._t ?? {};
1557
+ ttq._t[name] = Date.now();
1558
+ ttq._o = ttq._o ?? {};
1559
+ ttq._o[name] = opts ?? {};
1560
+ addScript(`${src}?sdkid=${encodeURIComponent(name)}&lib=ttq`);
1561
+ };
1562
+ ttq.load(id);
1563
+ }
1564
+ function loadGoogle(g) {
1565
+ const w = win();
1566
+ const ids = [g.ga4Id, g.adsId].filter((v) => !!v);
1567
+ if (ids.length === 0) return;
1568
+ w.dataLayer = w.dataLayer ?? [];
1569
+ if (!w.gtag) {
1570
+ w.gtag = function() {
1571
+ w.dataLayer.push(arguments);
1572
+ };
1573
+ }
1574
+ addScript(
1575
+ `https://www.googletagmanager.com/gtag/js?id=${encodeURIComponent(ids[0])}`
1576
+ );
1577
+ w.gtag("js", /* @__PURE__ */ new Date());
1578
+ const cookieDomain = location.hostname;
1579
+ w.gtag("set", { cookie_domain: cookieDomain });
1580
+ for (const id of ids) {
1581
+ w.gtag("config", id, {
1582
+ send_page_view: false,
1583
+ cookie_domain: cookieDomain
1584
+ });
1585
+ }
1586
+ }
1587
+ function onSecretPage(path = typeof location === "undefined" ? "" : location.pathname) {
1588
+ return path.startsWith("/orders/") || path.startsWith("/cart/recover");
1589
+ }
1590
+ function loadAll(p) {
1591
+ if (p.meta && !loaded.has("meta")) {
1592
+ loaded.add("meta");
1593
+ loadMeta(p.meta.pixelId);
1594
+ }
1595
+ if (p.tiktok && !loaded.has("tiktok")) {
1596
+ loaded.add("tiktok");
1597
+ loadTikTok(p.tiktok.pixelId);
1598
+ }
1599
+ if (p.google && !loaded.has("google")) {
1600
+ loaded.add("google");
1601
+ loadGoogle(p.google);
1602
+ }
1603
+ }
1604
+ var PLATFORM_COOKIES = {
1605
+ meta: { names: ["_fbp", "_fbc"], prefixes: [] },
1606
+ tiktok: {
1607
+ names: ["_ttp", "_tt_enable_cookie", "ttcsid"],
1608
+ prefixes: ["ttcsid_"]
1609
+ },
1610
+ google: { names: ["_ga", "_gcl_au", "_gcl_aw"], prefixes: ["_ga_"] }
1611
+ };
1612
+ function platformsOf(p) {
1613
+ return ["meta", "tiktok", "google"].filter((k) => !!p[k]);
1614
+ }
1615
+ function readMarker() {
1616
+ try {
1617
+ return localStorage.getItem(GRANT_MARKER_KEY) !== null;
1618
+ } catch {
1619
+ return false;
1620
+ }
1621
+ }
1622
+ function writeMarker(on) {
1623
+ try {
1624
+ if (on) localStorage.setItem(GRANT_MARKER_KEY, "1");
1625
+ else localStorage.removeItem(GRANT_MARKER_KEY);
1626
+ } catch {
1627
+ }
1628
+ }
1629
+ var PROBE = "_px_probe";
1630
+ function registrableDomain(host) {
1631
+ const parts = host.split(".");
1632
+ for (let i = parts.length - 2; i > 0; i--) {
1633
+ const candidate = parts.slice(i).join(".");
1634
+ document.cookie = `${PROBE}=1; Path=/; Domain=${candidate}`;
1635
+ if (document.cookie.split(";").some((c) => c.trim() === `${PROBE}=1`)) {
1636
+ document.cookie = `${PROBE}=; Max-Age=0; Path=/; Domain=${candidate}`;
1637
+ return candidate;
1638
+ }
1639
+ }
1640
+ return host;
1641
+ }
1642
+ function clearCookies(p, onlyHost) {
1643
+ try {
1644
+ const spec = platformsOf(p).map((k) => PLATFORM_COOKIES[k]);
1645
+ const present = document.cookie.split(";").map((c) => c.split("=")[0].trim()).filter(Boolean);
1646
+ const names = present.filter(
1647
+ (n) => spec.some(
1648
+ (s) => s.names.includes(n) || s.prefixes.some((x) => n.startsWith(x))
1649
+ )
1650
+ );
1651
+ if (names.length === 0) return;
1652
+ const host = location.hostname;
1653
+ const domains = [void 0, host];
1654
+ if (!onlyHost && !/^[\d.]+$/.test(host) && !host.includes(":")) {
1655
+ const site = registrableDomain(host);
1656
+ if (site !== host) domains.push(site);
1657
+ }
1658
+ for (const name of names) {
1659
+ for (const domain of domains) {
1660
+ document.cookie = `${name}=; Max-Age=0; Path=/${domain ? `; Domain=${domain}` : ""}`;
1661
+ }
1662
+ }
1663
+ } catch {
1664
+ }
1665
+ }
1666
+ var optedOut = false;
1667
+ var GOOGLE_CONSENT = (v) => ({
1668
+ ad_storage: v,
1669
+ analytics_storage: v,
1670
+ ad_user_data: v,
1671
+ ad_personalization: v
1672
+ });
1673
+ function googleIds(g) {
1674
+ return [g?.ga4Id, g?.adsId].filter((v) => !!v);
1675
+ }
1676
+ function optOut(p) {
1677
+ optedOut = true;
1678
+ const w = win();
1679
+ for (const id of googleIds(p.google)) w[`ga-disable-${id}`] = true;
1680
+ if (loaded.has("google") && typeof w.gtag === "function") {
1681
+ w.gtag("consent", "update", GOOGLE_CONSENT("denied"));
1682
+ }
1683
+ if (loaded.has("meta") && typeof w.fbq === "function") {
1684
+ w.fbq("consent", "revoke");
1685
+ }
1686
+ const ttq = w.ttq;
1687
+ if (loaded.has("tiktok") && typeof ttq?.disableCookie === "function") {
1688
+ ttq.disableCookie();
1689
+ }
1690
+ }
1691
+ function optIn(p) {
1692
+ optedOut = false;
1693
+ const w = win();
1694
+ for (const id of googleIds(p.google)) w[`ga-disable-${id}`] = false;
1695
+ if (loaded.has("google") && typeof w.gtag === "function") {
1696
+ w.gtag("consent", "update", GOOGLE_CONSENT("granted"));
1697
+ }
1698
+ if (loaded.has("meta") && typeof w.fbq === "function") {
1699
+ w.fbq("consent", "grant");
1700
+ }
1701
+ const ttq = w.ttq;
1702
+ if (loaded.has("tiktok") && typeof ttq?.enableCookie === "function") {
1703
+ ttq.enableCookie();
1704
+ }
1705
+ }
1706
+ function configurePixels(next, isGranted, opts = {}) {
1707
+ const held = configured ? [] : early.splice(0);
1708
+ configured = true;
1709
+ try {
1710
+ pixels = next;
1711
+ currency = opts.currency ?? DEFAULT_CURRENCY;
1712
+ const was = granted;
1713
+ granted = isGranted && !!next;
1714
+ if (granted && next) {
1715
+ if (optedOut) optIn(next);
1716
+ if (!onSecretPage()) loadAll(next);
1717
+ if (platformsOf(next).length > 0) writeMarker(true);
1718
+ } else if (next) {
1719
+ if (was) optOut(next);
1720
+ if (was || readMarker()) {
1721
+ clearCookies(next, opts.hostOnly ?? false);
1722
+ writeMarker(false);
1723
+ }
1724
+ }
1725
+ for (const e of held) send2(e);
1726
+ } catch {
1727
+ }
1728
+ }
1729
+ function purchased(orderId) {
1730
+ try {
1731
+ return localStorage.getItem(PURCHASE_KEY + orderId) !== null;
1732
+ } catch {
1733
+ return false;
1734
+ }
1735
+ }
1736
+ function markPurchased(orderId) {
1737
+ try {
1738
+ localStorage.setItem(PURCHASE_KEY + orderId, "1");
1739
+ } catch {
1740
+ }
1741
+ }
1742
+ function firePixel(e) {
1743
+ if (!configured) {
1744
+ if (typeof window === "undefined" || onSecretPage()) return;
1745
+ early.push(e);
1746
+ if (early.length > MAX_EARLY) early.shift();
1747
+ return;
1748
+ }
1749
+ send2(e);
1750
+ }
1751
+ function send2(e) {
1752
+ try {
1753
+ if (!granted || !pixels || onSecretPage()) return;
1754
+ loadAll(pixels);
1755
+ if (e.name === "purchase") {
1756
+ if (purchased(e.orderId)) return;
1757
+ markPurchased(e.orderId);
1758
+ }
1759
+ const w = win();
1760
+ if (pixels.meta && w.fbq) meta(w.fbq, e);
1761
+ if (pixels.tiktok && w.ttq) tiktok(w.ttq, e);
1762
+ if (pixels.google && w.gtag) google(w.gtag, pixels.google, e);
1763
+ } catch {
1764
+ }
1765
+ }
1766
+ function meta(fbq, e) {
1767
+ switch (e.name) {
1768
+ case "page_view":
1769
+ fbq("track", "PageView");
1770
+ return;
1771
+ case "view_item": {
1772
+ const it = itemOf(e);
1773
+ fbq("track", "ViewContent", {
1774
+ content_ids: [it.id],
1775
+ content_type: it.type,
1776
+ value: major(e.priceMinor),
1777
+ currency
1778
+ });
1779
+ return;
1780
+ }
1781
+ case "add_to_cart":
1782
+ fbq("track", "AddToCart", {
1783
+ content_ids: [e.variantId],
1784
+ content_type: "product",
1785
+ contents: [
1786
+ {
1787
+ id: e.variantId,
1788
+ quantity: e.quantity,
1789
+ item_price: major(e.priceMinor)
1790
+ }
1791
+ ],
1792
+ value: major(e.priceMinor * e.quantity),
1793
+ currency
1794
+ });
1795
+ return;
1796
+ case "begin_checkout":
1797
+ fbq("track", "InitiateCheckout", {
1798
+ value: major(e.valueMinor),
1799
+ currency
1800
+ });
1801
+ return;
1802
+ case "purchase":
1803
+ fbq(
1804
+ "track",
1805
+ "Purchase",
1806
+ {
1807
+ content_ids: e.items.map((i) => i.id),
1808
+ content_type: "product",
1809
+ contents: e.items.map((i) => ({
1810
+ id: i.id,
1811
+ quantity: i.quantity,
1812
+ item_price: major(i.priceMinor)
1813
+ })),
1814
+ value: major(e.valueMinor),
1815
+ currency
1816
+ },
1817
+ { eventID: e.orderId }
1818
+ );
1819
+ }
1820
+ }
1821
+ function tiktok(ttq, e) {
1822
+ const content = (id, quantity, priceMinor, type = "product") => ({
1823
+ content_id: id,
1824
+ content_type: type,
1825
+ quantity,
1826
+ price: major(priceMinor)
1827
+ });
1828
+ switch (e.name) {
1829
+ case "page_view":
1830
+ ttq.page();
1831
+ return;
1832
+ case "view_item": {
1833
+ const it = itemOf(e);
1834
+ ttq.track("ViewContent", {
1835
+ contents: [content(it.id, 1, e.priceMinor, it.type)],
1836
+ value: major(e.priceMinor),
1837
+ currency
1838
+ });
1839
+ return;
1840
+ }
1841
+ case "add_to_cart":
1842
+ ttq.track("AddToCart", {
1843
+ contents: [content(e.variantId, e.quantity, e.priceMinor)],
1844
+ value: major(e.priceMinor * e.quantity),
1845
+ currency
1846
+ });
1847
+ return;
1848
+ case "begin_checkout":
1849
+ ttq.track("InitiateCheckout", {
1850
+ value: major(e.valueMinor),
1851
+ currency
1852
+ });
1853
+ return;
1854
+ case "purchase":
1855
+ ttq.track(
1856
+ "CompletePayment",
1857
+ {
1858
+ contents: e.items.map((i) => content(i.id, i.quantity, i.priceMinor)),
1859
+ value: major(e.valueMinor),
1860
+ currency
1861
+ },
1862
+ { event_id: e.orderId }
1863
+ );
1864
+ }
1865
+ }
1866
+ function google(gtag, g, e) {
1867
+ const item = (id, quantity, priceMinor) => ({
1868
+ item_id: id,
1869
+ quantity,
1870
+ price: major(priceMinor)
1871
+ });
1872
+ switch (e.name) {
1873
+ case "page_view":
1874
+ gtag("event", "page_view");
1875
+ return;
1876
+ case "view_item":
1877
+ gtag("event", "view_item", {
1878
+ currency,
1879
+ value: major(e.priceMinor),
1880
+ items: [item(itemOf(e).id, 1, e.priceMinor)]
1881
+ });
1882
+ return;
1883
+ case "add_to_cart":
1884
+ gtag("event", "add_to_cart", {
1885
+ currency,
1886
+ value: major(e.priceMinor * e.quantity),
1887
+ items: [item(e.variantId, e.quantity, e.priceMinor)]
1888
+ });
1889
+ return;
1890
+ case "begin_checkout":
1891
+ gtag("event", "begin_checkout", {
1892
+ currency,
1893
+ value: major(e.valueMinor)
1894
+ });
1895
+ return;
1896
+ case "purchase":
1897
+ gtag("event", "purchase", {
1898
+ transaction_id: e.orderId,
1899
+ currency,
1900
+ value: major(e.valueMinor),
1901
+ items: e.items.map((i) => item(i.id, i.quantity, i.priceMinor))
1902
+ });
1903
+ if (g.adsId && g.adsConversionLabel) {
1904
+ gtag("event", "conversion", {
1905
+ send_to: `${g.adsId}/${g.adsConversionLabel}`,
1906
+ value: major(e.valueMinor),
1907
+ currency,
1908
+ transaction_id: e.orderId
1909
+ });
1910
+ }
1911
+ }
1912
+ }
1913
+
1914
+ // src/ad-pixels-loader.tsx
1915
+ var import_react12 = require("react");
1916
+ function AdPixelsLoader({
1917
+ pixels: pixels2,
1918
+ hostOnly = false,
1919
+ currency: currency2 = "TRY",
1920
+ pathname
1921
+ }) {
1922
+ const { stored, known } = useConsent();
1923
+ const granted2 = stored?.granted.includes("marketing") ?? false;
1924
+ (0, import_react12.useEffect)(() => {
1925
+ if (!known) return;
1926
+ configurePixels(pixels2, granted2, { hostOnly, currency: currency2 });
1927
+ }, [pixels2, granted2, known, hostOnly, currency2]);
1928
+ (0, import_react12.useEffect)(() => {
1929
+ if (granted2) firePixel({ name: "page_view" });
1930
+ }, [granted2, pathname]);
1931
+ return null;
1932
+ }
1129
1933
  // Annotate the CommonJS export names for ESM import in node:
1130
1934
  0 && (module.exports = {
1935
+ AdPixelsLoader,
1936
+ AnalyticsTracker,
1131
1937
  CheckoutChallenge,
1938
+ ConsentProvider,
1132
1939
  Content,
1940
+ OPTIONAL_CATEGORIES,
1133
1941
  ORDER_POLL_MAX_MS,
1134
1942
  ORDER_POLL_MIN_MS,
1135
1943
  PaymentForm,
1136
1944
  StorefrontProvider,
1945
+ configureAnalytics,
1946
+ configurePixels,
1137
1947
  defaultCartStorage,
1948
+ firePixel,
1138
1949
  isDeadCartError,
1139
1950
  isOrderSettled,
1951
+ onSecretPage,
1140
1952
  orderPollDelay,
1953
+ recordConsent,
1141
1954
  renderContent,
1955
+ resetAnalytics,
1956
+ resetPixels,
1957
+ track,
1142
1958
  useAccount,
1143
1959
  useCart,
1144
1960
  useCartStorage,
1145
1961
  useCategories,
1146
1962
  useCheckout,
1963
+ useConsent,
1147
1964
  useOrder,
1148
1965
  useProduct,
1149
1966
  useProducts,