@aranova/tracking-next 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
@@ -397,6 +397,10 @@ var DEFAULT_FLUSH_INTERVAL_MS = 2e3;
397
397
  var DEFAULT_MAX_QUEUE_SIZE = 10;
398
398
  var HARD_MAX_BATCH = 50;
399
399
  var API_KEY_HEADER = "X-Aranova-Api-Key";
400
+ var SDK_VERSION_HEADER = "X-Aranova-Sdk-Version";
401
+ var SDK_PACKAGE_HEADER = "X-Aranova-Sdk-Package";
402
+ var SDK_SURFACE_HEADER = "X-Aranova-Sdk-Surface";
403
+ var SDK_ENVIRONMENT_HEADER = "X-Aranova-Sdk-Environment";
400
404
  function buildContext(surface, sdkVersion, packageName, environment, activeGtagIds) {
401
405
  return {
402
406
  surface,
@@ -425,7 +429,7 @@ function consentSnapshot() {
425
429
  return null;
426
430
  }
427
431
  }
428
- async function postWithFetch(url, body, apiKey, keepalive) {
432
+ async function postWithFetch(url, body, apiKey, identity, keepalive) {
429
433
  if (typeof fetch !== "function")
430
434
  return;
431
435
  try {
@@ -433,7 +437,11 @@ async function postWithFetch(url, body, apiKey, keepalive) {
433
437
  method: "POST",
434
438
  headers: {
435
439
  "Content-Type": "application/json",
436
- [API_KEY_HEADER]: apiKey
440
+ [API_KEY_HEADER]: apiKey,
441
+ [SDK_VERSION_HEADER]: identity.sdkVersion,
442
+ [SDK_PACKAGE_HEADER]: identity.packageName,
443
+ [SDK_SURFACE_HEADER]: identity.surface,
444
+ [SDK_ENVIRONMENT_HEADER]: identity.environment
437
445
  },
438
446
  body,
439
447
  keepalive,
@@ -470,6 +478,12 @@ function createTrackingClient(config) {
470
478
  const activeGtagIds = config.activeGtagIds ?? null;
471
479
  const endpointBase = config.endpoint.replace(/\/$/, "");
472
480
  const eventsUrl = `${endpointBase}/events`;
481
+ const identityHeaders2 = {
482
+ sdkVersion: sdkVersion ?? "",
483
+ packageName: packageName ?? "",
484
+ surface: config.surface,
485
+ environment
486
+ };
473
487
  let queue = [];
474
488
  let flushTimer = null;
475
489
  let firstPage = null;
@@ -545,7 +559,7 @@ function createTrackingClient(config) {
545
559
  events
546
560
  };
547
561
  const serialized = JSON.stringify(body);
548
- await postWithFetch(eventsUrl, serialized, config.apiKey, false);
562
+ await postWithFetch(eventsUrl, serialized, config.apiKey, identityHeaders2, false);
549
563
  }
550
564
  function trackEvent(input) {
551
565
  if (destroyed)
@@ -576,7 +590,7 @@ function createTrackingClient(config) {
576
590
  events
577
591
  };
578
592
  const serialized = JSON.stringify(body);
579
- void postWithFetch(eventsUrl, serialized, config.apiKey, true);
593
+ void postWithFetch(eventsUrl, serialized, config.apiKey, identityHeaders2, true);
580
594
  }
581
595
  if (typeof window !== "undefined") {
582
596
  window.addEventListener("pagehide", flushOnUnload);
@@ -1036,6 +1050,121 @@ function attachFormStart(client, config) {
1036
1050
  };
1037
1051
  }
1038
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
+
1039
1168
  // src/ConsentBanner.tsx
1040
1169
  import { useEffect, useState } from "react";
1041
1170
  import { jsx, jsxs } from "react/jsx-runtime";
@@ -1140,7 +1269,7 @@ function GoogleAdsTracking(props) {
1140
1269
  import { usePathname, useSearchParams } from "next/navigation";
1141
1270
 
1142
1271
  // package.json
1143
- var version = "0.7.1";
1272
+ var version = "0.8.0";
1144
1273
 
1145
1274
  // src/factory.tsx
1146
1275
  import {
@@ -1351,16 +1480,22 @@ function createTracking(options) {
1351
1480
  return { TrackingProvider, useTracking };
1352
1481
  }
1353
1482
  export {
1483
+ AranovaApiError,
1354
1484
  ConsentBanner,
1355
1485
  GoogleAdsTracking,
1356
1486
  TRACKING_PARAM_KEYS,
1357
1487
  captureTrackingParamsFromLocation,
1488
+ createSalesClient,
1358
1489
  createTracking,
1359
1490
  createTrackingClientContext,
1360
1491
  createTrackingEventCreatePayload,
1361
1492
  createTrackingSessionUpsertPayload,
1493
+ fetchServices,
1494
+ formatMoney,
1495
+ fromMinor,
1362
1496
  getConsentState,
1363
1497
  setConsentState,
1498
+ toMinor,
1364
1499
  useConsentState,
1365
1500
  useGclid,
1366
1501
  useTrackingParams