@aranova/tracking-react 0.7.1 → 0.8.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.mjs CHANGED
@@ -465,6 +465,10 @@ var DEFAULT_FLUSH_INTERVAL_MS = 2e3;
465
465
  var DEFAULT_MAX_QUEUE_SIZE = 10;
466
466
  var HARD_MAX_BATCH = 50;
467
467
  var API_KEY_HEADER = "X-Aranova-Api-Key";
468
+ var SDK_VERSION_HEADER = "X-Aranova-Sdk-Version";
469
+ var SDK_PACKAGE_HEADER = "X-Aranova-Sdk-Package";
470
+ var SDK_SURFACE_HEADER = "X-Aranova-Sdk-Surface";
471
+ var SDK_ENVIRONMENT_HEADER = "X-Aranova-Sdk-Environment";
468
472
  function buildContext(surface, sdkVersion, packageName, environment, activeGtagIds) {
469
473
  return {
470
474
  surface,
@@ -493,7 +497,7 @@ function consentSnapshot() {
493
497
  return null;
494
498
  }
495
499
  }
496
- async function postWithFetch(url, body, apiKey, keepalive) {
500
+ async function postWithFetch(url, body, apiKey, identity, keepalive) {
497
501
  if (typeof fetch !== "function")
498
502
  return;
499
503
  try {
@@ -501,7 +505,11 @@ async function postWithFetch(url, body, apiKey, keepalive) {
501
505
  method: "POST",
502
506
  headers: {
503
507
  "Content-Type": "application/json",
504
- [API_KEY_HEADER]: apiKey
508
+ [API_KEY_HEADER]: apiKey,
509
+ [SDK_VERSION_HEADER]: identity.sdkVersion,
510
+ [SDK_PACKAGE_HEADER]: identity.packageName,
511
+ [SDK_SURFACE_HEADER]: identity.surface,
512
+ [SDK_ENVIRONMENT_HEADER]: identity.environment
505
513
  },
506
514
  body,
507
515
  keepalive,
@@ -538,6 +546,12 @@ function createTrackingClient(config) {
538
546
  const activeGtagIds = config.activeGtagIds ?? null;
539
547
  const endpointBase = config.endpoint.replace(/\/$/, "");
540
548
  const eventsUrl = `${endpointBase}/events`;
549
+ const identityHeaders2 = {
550
+ sdkVersion: sdkVersion ?? "",
551
+ packageName: packageName ?? "",
552
+ surface: config.surface,
553
+ environment
554
+ };
541
555
  let queue = [];
542
556
  let flushTimer = null;
543
557
  let firstPage = null;
@@ -613,7 +627,7 @@ function createTrackingClient(config) {
613
627
  events
614
628
  };
615
629
  const serialized = JSON.stringify(body);
616
- await postWithFetch(eventsUrl, serialized, config.apiKey, false);
630
+ await postWithFetch(eventsUrl, serialized, config.apiKey, identityHeaders2, false);
617
631
  }
618
632
  function trackEvent(input) {
619
633
  if (destroyed)
@@ -644,7 +658,7 @@ function createTrackingClient(config) {
644
658
  events
645
659
  };
646
660
  const serialized = JSON.stringify(body);
647
- void postWithFetch(eventsUrl, serialized, config.apiKey, true);
661
+ void postWithFetch(eventsUrl, serialized, config.apiKey, identityHeaders2, true);
648
662
  }
649
663
  if (typeof window !== "undefined") {
650
664
  window.addEventListener("pagehide", flushOnUnload);
@@ -1239,6 +1253,121 @@ function attachFormStart(client, config) {
1239
1253
  };
1240
1254
  }
1241
1255
 
1256
+ // ../tracking-core/src/resources/sales/errors.ts
1257
+ var AranovaApiError = class extends Error {
1258
+ constructor(message, options) {
1259
+ super(message);
1260
+ this.name = "AranovaApiError";
1261
+ this.status = options.status;
1262
+ this.code = options.code;
1263
+ this.requestId = options.requestId;
1264
+ }
1265
+ };
1266
+
1267
+ // ../tracking-core/src/resources/sales/transport.ts
1268
+ function identityHeaders(config) {
1269
+ const headers = { [API_KEY_HEADER]: config.apiKey };
1270
+ if (config.sdkVersion) headers[SDK_VERSION_HEADER] = config.sdkVersion;
1271
+ if (config.packageName) headers[SDK_PACKAGE_HEADER] = config.packageName;
1272
+ if (config.surface) headers[SDK_SURFACE_HEADER] = config.surface;
1273
+ if (config.environment) headers[SDK_ENVIRONMENT_HEADER] = config.environment;
1274
+ return headers;
1275
+ }
1276
+ function joinUrl(endpoint, path) {
1277
+ return `${endpoint.replace(/\/$/, "")}${path}`;
1278
+ }
1279
+ async function salesRequest(config, method, path, body) {
1280
+ const headers = identityHeaders(config);
1281
+ if (body !== void 0) headers["Content-Type"] = "application/json";
1282
+ const response = await fetch(joinUrl(config.endpoint, path), {
1283
+ method,
1284
+ headers,
1285
+ body: body === void 0 ? void 0 : JSON.stringify(body)
1286
+ });
1287
+ if (!response.ok) {
1288
+ let detail;
1289
+ let code;
1290
+ try {
1291
+ const parsed = await response.json();
1292
+ if (parsed && typeof parsed === "object") {
1293
+ const record = parsed;
1294
+ if (typeof record.detail === "string") detail = record.detail;
1295
+ if (typeof record.code === "string") code = record.code;
1296
+ }
1297
+ } catch {
1298
+ }
1299
+ throw new AranovaApiError(detail ?? response.statusText ?? "Request failed", {
1300
+ status: response.status,
1301
+ code,
1302
+ requestId: response.headers.get("x-request-id") ?? void 0
1303
+ });
1304
+ }
1305
+ if (response.status === 204) return void 0;
1306
+ return await response.json();
1307
+ }
1308
+
1309
+ // ../tracking-core/src/resources/sales/client.ts
1310
+ function createSalesClient(config) {
1311
+ return {
1312
+ async record(input) {
1313
+ const currency = input.currency ?? config.defaultCurrency;
1314
+ if (!currency) {
1315
+ throw new Error(
1316
+ "record: `currency` is required (pass it on the sale or set config.defaultCurrency)"
1317
+ );
1318
+ }
1319
+ const body = {
1320
+ ...input,
1321
+ currency,
1322
+ occurred_at: input.occurred_at ?? (/* @__PURE__ */ new Date()).toISOString()
1323
+ };
1324
+ return salesRequest(config, "POST", "/sales", body);
1325
+ },
1326
+ async list(query) {
1327
+ const { cursor, limit, ...filters } = query ?? {};
1328
+ return salesRequest(config, "POST", "/sales/query", {
1329
+ filters,
1330
+ ...limit !== void 0 ? { limit } : {},
1331
+ ...cursor !== void 0 ? { cursor } : {}
1332
+ });
1333
+ },
1334
+ async get(id) {
1335
+ return salesRequest(config, "GET", `/sales/${id}`);
1336
+ },
1337
+ async update(id, patch) {
1338
+ return salesRequest(config, "PATCH", `/sales/${id}`, patch);
1339
+ },
1340
+ async delete(id) {
1341
+ await salesRequest(config, "DELETE", `/sales/${id}`);
1342
+ }
1343
+ };
1344
+ }
1345
+
1346
+ // ../tracking-core/src/resources/services.ts
1347
+ async function fetchServices(config) {
1348
+ return salesRequest(config, "GET", "/services");
1349
+ }
1350
+
1351
+ // ../tracking-core/src/resources/sales/money.ts
1352
+ var MINOR_UNIT_EXPONENT = {
1353
+ USD: 2,
1354
+ CAD: 2
1355
+ };
1356
+ function exponentFor(currency) {
1357
+ return MINOR_UNIT_EXPONENT[currency] ?? 2;
1358
+ }
1359
+ function toMinor(amount, currency) {
1360
+ return Math.round(amount * 10 ** exponentFor(currency));
1361
+ }
1362
+ function fromMinor(cents, currency) {
1363
+ return cents / 10 ** exponentFor(currency);
1364
+ }
1365
+ function formatMoney(cents, currency, locale) {
1366
+ return new Intl.NumberFormat(locale, { style: "currency", currency }).format(
1367
+ fromMinor(cents, currency)
1368
+ );
1369
+ }
1370
+
1242
1371
  // src/ConsentBanner.tsx
1243
1372
  import { jsx, jsxs } from "react/jsx-runtime";
1244
1373
  function ConsentBanner() {
@@ -1342,7 +1471,7 @@ import {
1342
1471
  } from "react";
1343
1472
 
1344
1473
  // package.json
1345
- var version = "0.7.1";
1474
+ var version = "0.8.0";
1346
1475
 
1347
1476
  // src/factory.tsx
1348
1477
  import { jsx as jsx2 } from "react/jsx-runtime";
@@ -1451,16 +1580,22 @@ function createTracking(options) {
1451
1580
  return { TrackingProvider, useTracking };
1452
1581
  }
1453
1582
  export {
1583
+ AranovaApiError,
1454
1584
  ConsentBanner,
1455
1585
  GoogleAdsTracking,
1456
1586
  TRACKING_PARAM_KEYS,
1457
1587
  captureTrackingParamsFromLocation,
1588
+ createSalesClient,
1458
1589
  createTracking,
1459
1590
  createTrackingClientContext,
1460
1591
  createTrackingEventCreatePayload,
1461
1592
  createTrackingSessionUpsertPayload,
1593
+ fetchServices,
1594
+ formatMoney,
1595
+ fromMinor,
1462
1596
  getConsentState,
1463
1597
  setConsentState,
1598
+ toMinor,
1464
1599
  useConsentState,
1465
1600
  useGclid,
1466
1601
  useTrackingParams