@aranova/tracking-react 0.7.2 → 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
@@ -546,7 +546,7 @@ function createTrackingClient(config) {
546
546
  const activeGtagIds = config.activeGtagIds ?? null;
547
547
  const endpointBase = config.endpoint.replace(/\/$/, "");
548
548
  const eventsUrl = `${endpointBase}/events`;
549
- const identityHeaders = {
549
+ const identityHeaders2 = {
550
550
  sdkVersion: sdkVersion ?? "",
551
551
  packageName: packageName ?? "",
552
552
  surface: config.surface,
@@ -627,7 +627,7 @@ function createTrackingClient(config) {
627
627
  events
628
628
  };
629
629
  const serialized = JSON.stringify(body);
630
- await postWithFetch(eventsUrl, serialized, config.apiKey, identityHeaders, false);
630
+ await postWithFetch(eventsUrl, serialized, config.apiKey, identityHeaders2, false);
631
631
  }
632
632
  function trackEvent(input) {
633
633
  if (destroyed)
@@ -658,7 +658,7 @@ function createTrackingClient(config) {
658
658
  events
659
659
  };
660
660
  const serialized = JSON.stringify(body);
661
- void postWithFetch(eventsUrl, serialized, config.apiKey, identityHeaders, true);
661
+ void postWithFetch(eventsUrl, serialized, config.apiKey, identityHeaders2, true);
662
662
  }
663
663
  if (typeof window !== "undefined") {
664
664
  window.addEventListener("pagehide", flushOnUnload);
@@ -1253,6 +1253,121 @@ function attachFormStart(client, config) {
1253
1253
  };
1254
1254
  }
1255
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
+
1256
1371
  // src/ConsentBanner.tsx
1257
1372
  import { jsx, jsxs } from "react/jsx-runtime";
1258
1373
  function ConsentBanner() {
@@ -1356,7 +1471,7 @@ import {
1356
1471
  } from "react";
1357
1472
 
1358
1473
  // package.json
1359
- var version = "0.7.2";
1474
+ var version = "0.8.0";
1360
1475
 
1361
1476
  // src/factory.tsx
1362
1477
  import { jsx as jsx2 } from "react/jsx-runtime";
@@ -1465,16 +1580,22 @@ function createTracking(options) {
1465
1580
  return { TrackingProvider, useTracking };
1466
1581
  }
1467
1582
  export {
1583
+ AranovaApiError,
1468
1584
  ConsentBanner,
1469
1585
  GoogleAdsTracking,
1470
1586
  TRACKING_PARAM_KEYS,
1471
1587
  captureTrackingParamsFromLocation,
1588
+ createSalesClient,
1472
1589
  createTracking,
1473
1590
  createTrackingClientContext,
1474
1591
  createTrackingEventCreatePayload,
1475
1592
  createTrackingSessionUpsertPayload,
1593
+ fetchServices,
1594
+ formatMoney,
1595
+ fromMinor,
1476
1596
  getConsentState,
1477
1597
  setConsentState,
1598
+ toMinor,
1478
1599
  useConsentState,
1479
1600
  useGclid,
1480
1601
  useTrackingParams