@aranova/tracking-next 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
@@ -478,7 +478,7 @@ function createTrackingClient(config) {
478
478
  const activeGtagIds = config.activeGtagIds ?? null;
479
479
  const endpointBase = config.endpoint.replace(/\/$/, "");
480
480
  const eventsUrl = `${endpointBase}/events`;
481
- const identityHeaders = {
481
+ const identityHeaders2 = {
482
482
  sdkVersion: sdkVersion ?? "",
483
483
  packageName: packageName ?? "",
484
484
  surface: config.surface,
@@ -559,7 +559,7 @@ function createTrackingClient(config) {
559
559
  events
560
560
  };
561
561
  const serialized = JSON.stringify(body);
562
- await postWithFetch(eventsUrl, serialized, config.apiKey, identityHeaders, false);
562
+ await postWithFetch(eventsUrl, serialized, config.apiKey, identityHeaders2, false);
563
563
  }
564
564
  function trackEvent(input) {
565
565
  if (destroyed)
@@ -590,7 +590,7 @@ function createTrackingClient(config) {
590
590
  events
591
591
  };
592
592
  const serialized = JSON.stringify(body);
593
- void postWithFetch(eventsUrl, serialized, config.apiKey, identityHeaders, true);
593
+ void postWithFetch(eventsUrl, serialized, config.apiKey, identityHeaders2, true);
594
594
  }
595
595
  if (typeof window !== "undefined") {
596
596
  window.addEventListener("pagehide", flushOnUnload);
@@ -1050,6 +1050,121 @@ function attachFormStart(client, config) {
1050
1050
  };
1051
1051
  }
1052
1052
 
1053
+ // ../tracking-core/src/resources/sales/errors.ts
1054
+ var AranovaApiError = class extends Error {
1055
+ constructor(message, options) {
1056
+ super(message);
1057
+ this.name = "AranovaApiError";
1058
+ this.status = options.status;
1059
+ this.code = options.code;
1060
+ this.requestId = options.requestId;
1061
+ }
1062
+ };
1063
+
1064
+ // ../tracking-core/src/resources/sales/transport.ts
1065
+ function identityHeaders(config) {
1066
+ const headers = { [API_KEY_HEADER]: config.apiKey };
1067
+ if (config.sdkVersion) headers[SDK_VERSION_HEADER] = config.sdkVersion;
1068
+ if (config.packageName) headers[SDK_PACKAGE_HEADER] = config.packageName;
1069
+ if (config.surface) headers[SDK_SURFACE_HEADER] = config.surface;
1070
+ if (config.environment) headers[SDK_ENVIRONMENT_HEADER] = config.environment;
1071
+ return headers;
1072
+ }
1073
+ function joinUrl(endpoint, path) {
1074
+ return `${endpoint.replace(/\/$/, "")}${path}`;
1075
+ }
1076
+ async function salesRequest(config, method, path, body) {
1077
+ const headers = identityHeaders(config);
1078
+ if (body !== void 0) headers["Content-Type"] = "application/json";
1079
+ const response = await fetch(joinUrl(config.endpoint, path), {
1080
+ method,
1081
+ headers,
1082
+ body: body === void 0 ? void 0 : JSON.stringify(body)
1083
+ });
1084
+ if (!response.ok) {
1085
+ let detail;
1086
+ let code;
1087
+ try {
1088
+ const parsed = await response.json();
1089
+ if (parsed && typeof parsed === "object") {
1090
+ const record = parsed;
1091
+ if (typeof record.detail === "string") detail = record.detail;
1092
+ if (typeof record.code === "string") code = record.code;
1093
+ }
1094
+ } catch {
1095
+ }
1096
+ throw new AranovaApiError(detail ?? response.statusText ?? "Request failed", {
1097
+ status: response.status,
1098
+ code,
1099
+ requestId: response.headers.get("x-request-id") ?? void 0
1100
+ });
1101
+ }
1102
+ if (response.status === 204) return void 0;
1103
+ return await response.json();
1104
+ }
1105
+
1106
+ // ../tracking-core/src/resources/sales/client.ts
1107
+ function createSalesClient(config) {
1108
+ return {
1109
+ async record(input) {
1110
+ const currency = input.currency ?? config.defaultCurrency;
1111
+ if (!currency) {
1112
+ throw new Error(
1113
+ "record: `currency` is required (pass it on the sale or set config.defaultCurrency)"
1114
+ );
1115
+ }
1116
+ const body = {
1117
+ ...input,
1118
+ currency,
1119
+ occurred_at: input.occurred_at ?? (/* @__PURE__ */ new Date()).toISOString()
1120
+ };
1121
+ return salesRequest(config, "POST", "/sales", body);
1122
+ },
1123
+ async list(query) {
1124
+ const { cursor, limit, ...filters } = query ?? {};
1125
+ return salesRequest(config, "POST", "/sales/query", {
1126
+ filters,
1127
+ ...limit !== void 0 ? { limit } : {},
1128
+ ...cursor !== void 0 ? { cursor } : {}
1129
+ });
1130
+ },
1131
+ async get(id) {
1132
+ return salesRequest(config, "GET", `/sales/${id}`);
1133
+ },
1134
+ async update(id, patch) {
1135
+ return salesRequest(config, "PATCH", `/sales/${id}`, patch);
1136
+ },
1137
+ async delete(id) {
1138
+ await salesRequest(config, "DELETE", `/sales/${id}`);
1139
+ }
1140
+ };
1141
+ }
1142
+
1143
+ // ../tracking-core/src/resources/services.ts
1144
+ async function fetchServices(config) {
1145
+ return salesRequest(config, "GET", "/services");
1146
+ }
1147
+
1148
+ // ../tracking-core/src/resources/sales/money.ts
1149
+ var MINOR_UNIT_EXPONENT = {
1150
+ USD: 2,
1151
+ CAD: 2
1152
+ };
1153
+ function exponentFor(currency) {
1154
+ return MINOR_UNIT_EXPONENT[currency] ?? 2;
1155
+ }
1156
+ function toMinor(amount, currency) {
1157
+ return Math.round(amount * 10 ** exponentFor(currency));
1158
+ }
1159
+ function fromMinor(cents, currency) {
1160
+ return cents / 10 ** exponentFor(currency);
1161
+ }
1162
+ function formatMoney(cents, currency, locale) {
1163
+ return new Intl.NumberFormat(locale, { style: "currency", currency }).format(
1164
+ fromMinor(cents, currency)
1165
+ );
1166
+ }
1167
+
1053
1168
  // src/ConsentBanner.tsx
1054
1169
  import { useEffect, useState } from "react";
1055
1170
  import { jsx, jsxs } from "react/jsx-runtime";
@@ -1154,7 +1269,7 @@ function GoogleAdsTracking(props) {
1154
1269
  import { usePathname, useSearchParams } from "next/navigation";
1155
1270
 
1156
1271
  // package.json
1157
- var version = "0.7.2";
1272
+ var version = "0.8.0";
1158
1273
 
1159
1274
  // src/factory.tsx
1160
1275
  import {
@@ -1365,16 +1480,22 @@ function createTracking(options) {
1365
1480
  return { TrackingProvider, useTracking };
1366
1481
  }
1367
1482
  export {
1483
+ AranovaApiError,
1368
1484
  ConsentBanner,
1369
1485
  GoogleAdsTracking,
1370
1486
  TRACKING_PARAM_KEYS,
1371
1487
  captureTrackingParamsFromLocation,
1488
+ createSalesClient,
1372
1489
  createTracking,
1373
1490
  createTrackingClientContext,
1374
1491
  createTrackingEventCreatePayload,
1375
1492
  createTrackingSessionUpsertPayload,
1493
+ fetchServices,
1494
+ formatMoney,
1495
+ fromMinor,
1376
1496
  getConsentState,
1377
1497
  setConsentState,
1498
+ toMinor,
1378
1499
  useConsentState,
1379
1500
  useGclid,
1380
1501
  useTrackingParams