@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/README.md CHANGED
@@ -129,6 +129,53 @@ tracking.trackEvent('phone_click', {
129
129
  });
130
130
  ```
131
131
 
132
+ ## Recording Sales
133
+
134
+ Record a sale / conversion (a first-class, mutable resource — not a fire-and-forget
135
+ event). Money is integer minor units (cents); `currency` is a required ISO-4217 enum.
136
+
137
+ One isomorphic `createSalesClient` (on the root entry) serves both sides — what a
138
+ key may do is enforced by the backend, not by hiding methods. Browser write with
139
+ your **public** key:
140
+
141
+ ```tsx
142
+ import { createSalesClient, toMinor } from '@aranova/tracking-react';
143
+
144
+ const sales = createSalesClient({ apiKey: import.meta.env.VITE_ARANOVA_TRACKING_API_KEY, endpoint });
145
+ await sales.record({ currency: 'CAD', amount_total_cents: toMinor(250, 'CAD'), service: 'tires' });
146
+ ```
147
+
148
+ Reads / full CRUD require a **secret** key and must run server-side — never ship a
149
+ secret key in the browser bundle. In a Vite + Vercel app, hold it in a serverless
150
+ function (same import, secret key):
151
+
152
+ ```ts
153
+ // api/sales.ts (Vercel serverless function — runs on the server)
154
+ import { createSalesClient } from '@aranova/tracking-react';
155
+
156
+ const sales = createSalesClient({
157
+ apiKey: process.env.ARANOVA_TRACKING_SECRET_KEY!,
158
+ endpoint: process.env.ARANOVA_TRACKING_ENDPOINT!,
159
+ });
160
+ export default async function handler(_req, res) {
161
+ res.json(await sales.list({ limit: 50 }));
162
+ }
163
+ ```
164
+
165
+ A public-key client calling `list`/`get`/`update`/`delete` gets a `403` telling it
166
+ to use a secret key server-side.
167
+
168
+ Generate the typed `AranovaService` union from your dashboard services with the CLI
169
+ (install it as a **devDependency**):
170
+
171
+ ```bash
172
+ npm install --save-dev @aranova/tracking-cli
173
+ npx @aranova/tracking-cli gen # reads ARANOVA_TRACKING_SECRET_KEY from .env
174
+ ```
175
+
176
+ See the full guide: [sales-tracking.md](https://github.com/AranovaIO/aranova_internal/blob/master/docs/tracking-package/sales-tracking.md)
177
+ and the CLI reference: [cli.md](https://github.com/AranovaIO/aranova_internal/blob/master/docs/tracking-package/cli.md).
178
+
132
179
  ## Exports
133
180
 
134
181
  - `createTracking()`
@@ -136,5 +183,7 @@ tracking.trackEvent('phone_click', {
136
183
  - `GoogleAdsTracking`
137
184
  - `ConsentBanner`
138
185
  - `useTrackingParams()`, `useGclid()`, `useConsentState()`
186
+ - `createSalesClient()` (isomorphic sales client — public key writes, secret key reads/CRUD) + money helpers (`toMinor`/`fromMinor`/`formatMoney`)
187
+ - Codegen: [`@aranova/tracking-cli`](https://www.npmjs.com/package/@aranova/tracking-cli) — `gen` typed service unions (devDependency)
139
188
  - Event metadata/config types such as `FormSubmitMetadata`, `PhoneClickMetadata`, and `JsonValue`
140
189
 
package/dist/index.d.mts CHANGED
@@ -1398,6 +1398,307 @@ interface TypedTrackingClient<TRegistry extends TriggerRegistryConfig> {
1398
1398
  getVisitorId(): string;
1399
1399
  }
1400
1400
 
1401
+ /**
1402
+ * Sales / Conversions wire schemas — the client-side source of truth.
1403
+ *
1404
+ * `saleCreateSchema` is mirrored by `SaleCreateSchema` in
1405
+ * `apps/api/src/schemas/tracking_sales.py` and enforced by the backend drift
1406
+ * test (the `resources` section of `events.schema.json`). Keep them in lockstep.
1407
+ *
1408
+ * Money is **integer minor units (cents)**; `quantity` is a decimal string;
1409
+ * `currency` is the required `SupportedCurrency` enum.
1410
+ */
1411
+ declare const SUPPORTED_CURRENCIES: readonly ["USD", "CAD"];
1412
+ type SupportedCurrency = (typeof SUPPORTED_CURRENCIES)[number];
1413
+ declare const TRACKING_ENVIRONMENTS: readonly ["production", "development"];
1414
+ declare const saleItemSchema: z.ZodObject<{
1415
+ external_item_id: z.ZodOptional<z.ZodNullable<z.ZodString>>;
1416
+ name: z.ZodOptional<z.ZodNullable<z.ZodString>>;
1417
+ category: z.ZodOptional<z.ZodNullable<z.ZodString>>;
1418
+ quantity: z.ZodString;
1419
+ unit_price_cents: z.ZodNumber;
1420
+ unit_cost_cents: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
1421
+ }, "strict", z.ZodTypeAny, {
1422
+ quantity: string;
1423
+ unit_price_cents: number;
1424
+ name?: string | null | undefined;
1425
+ external_item_id?: string | null | undefined;
1426
+ category?: string | null | undefined;
1427
+ unit_cost_cents?: number | null | undefined;
1428
+ }, {
1429
+ quantity: string;
1430
+ unit_price_cents: number;
1431
+ name?: string | null | undefined;
1432
+ external_item_id?: string | null | undefined;
1433
+ category?: string | null | undefined;
1434
+ unit_cost_cents?: number | null | undefined;
1435
+ }>;
1436
+ declare const saleCreateSchema: z.ZodObject<{
1437
+ external_id: z.ZodOptional<z.ZodNullable<z.ZodString>>;
1438
+ description: z.ZodOptional<z.ZodNullable<z.ZodString>>;
1439
+ service: z.ZodOptional<z.ZodNullable<z.ZodString>>;
1440
+ currency: z.ZodEnum<["USD", "CAD"]>;
1441
+ amount_total_cents: z.ZodNumber;
1442
+ occurred_at: z.ZodString;
1443
+ environment: z.ZodDefault<z.ZodEnum<["production", "development"]>>;
1444
+ items: z.ZodDefault<z.ZodArray<z.ZodObject<{
1445
+ external_item_id: z.ZodOptional<z.ZodNullable<z.ZodString>>;
1446
+ name: z.ZodOptional<z.ZodNullable<z.ZodString>>;
1447
+ category: z.ZodOptional<z.ZodNullable<z.ZodString>>;
1448
+ quantity: z.ZodString;
1449
+ unit_price_cents: z.ZodNumber;
1450
+ unit_cost_cents: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
1451
+ }, "strict", z.ZodTypeAny, {
1452
+ quantity: string;
1453
+ unit_price_cents: number;
1454
+ name?: string | null | undefined;
1455
+ external_item_id?: string | null | undefined;
1456
+ category?: string | null | undefined;
1457
+ unit_cost_cents?: number | null | undefined;
1458
+ }, {
1459
+ quantity: string;
1460
+ unit_price_cents: number;
1461
+ name?: string | null | undefined;
1462
+ external_item_id?: string | null | undefined;
1463
+ category?: string | null | undefined;
1464
+ unit_cost_cents?: number | null | undefined;
1465
+ }>, "many">>;
1466
+ metadata: z.ZodOptional<z.ZodNullable<z.ZodRecord<z.ZodString, z.ZodUnknown>>>;
1467
+ }, "strict", z.ZodTypeAny, {
1468
+ currency: "USD" | "CAD";
1469
+ amount_total_cents: number;
1470
+ occurred_at: string;
1471
+ environment: "production" | "development";
1472
+ items: {
1473
+ quantity: string;
1474
+ unit_price_cents: number;
1475
+ name?: string | null | undefined;
1476
+ external_item_id?: string | null | undefined;
1477
+ category?: string | null | undefined;
1478
+ unit_cost_cents?: number | null | undefined;
1479
+ }[];
1480
+ metadata?: Record<string, unknown> | null | undefined;
1481
+ external_id?: string | null | undefined;
1482
+ description?: string | null | undefined;
1483
+ service?: string | null | undefined;
1484
+ }, {
1485
+ currency: "USD" | "CAD";
1486
+ amount_total_cents: number;
1487
+ occurred_at: string;
1488
+ metadata?: Record<string, unknown> | null | undefined;
1489
+ external_id?: string | null | undefined;
1490
+ description?: string | null | undefined;
1491
+ service?: string | null | undefined;
1492
+ environment?: "production" | "development" | undefined;
1493
+ items?: {
1494
+ quantity: string;
1495
+ unit_price_cents: number;
1496
+ name?: string | null | undefined;
1497
+ external_item_id?: string | null | undefined;
1498
+ category?: string | null | undefined;
1499
+ unit_cost_cents?: number | null | undefined;
1500
+ }[] | undefined;
1501
+ }>;
1502
+ declare const saleUpdateSchema: z.ZodObject<{
1503
+ description: z.ZodOptional<z.ZodNullable<z.ZodString>>;
1504
+ service: z.ZodOptional<z.ZodNullable<z.ZodString>>;
1505
+ currency: z.ZodOptional<z.ZodEnum<["USD", "CAD"]>>;
1506
+ amount_total_cents: z.ZodOptional<z.ZodNumber>;
1507
+ occurred_at: z.ZodOptional<z.ZodString>;
1508
+ items: z.ZodOptional<z.ZodArray<z.ZodObject<{
1509
+ external_item_id: z.ZodOptional<z.ZodNullable<z.ZodString>>;
1510
+ name: z.ZodOptional<z.ZodNullable<z.ZodString>>;
1511
+ category: z.ZodOptional<z.ZodNullable<z.ZodString>>;
1512
+ quantity: z.ZodString;
1513
+ unit_price_cents: z.ZodNumber;
1514
+ unit_cost_cents: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
1515
+ }, "strict", z.ZodTypeAny, {
1516
+ quantity: string;
1517
+ unit_price_cents: number;
1518
+ name?: string | null | undefined;
1519
+ external_item_id?: string | null | undefined;
1520
+ category?: string | null | undefined;
1521
+ unit_cost_cents?: number | null | undefined;
1522
+ }, {
1523
+ quantity: string;
1524
+ unit_price_cents: number;
1525
+ name?: string | null | undefined;
1526
+ external_item_id?: string | null | undefined;
1527
+ category?: string | null | undefined;
1528
+ unit_cost_cents?: number | null | undefined;
1529
+ }>, "many">>;
1530
+ metadata: z.ZodOptional<z.ZodNullable<z.ZodRecord<z.ZodString, z.ZodUnknown>>>;
1531
+ }, "strict", z.ZodTypeAny, {
1532
+ metadata?: Record<string, unknown> | null | undefined;
1533
+ description?: string | null | undefined;
1534
+ service?: string | null | undefined;
1535
+ currency?: "USD" | "CAD" | undefined;
1536
+ amount_total_cents?: number | undefined;
1537
+ occurred_at?: string | undefined;
1538
+ items?: {
1539
+ quantity: string;
1540
+ unit_price_cents: number;
1541
+ name?: string | null | undefined;
1542
+ external_item_id?: string | null | undefined;
1543
+ category?: string | null | undefined;
1544
+ unit_cost_cents?: number | null | undefined;
1545
+ }[] | undefined;
1546
+ }, {
1547
+ metadata?: Record<string, unknown> | null | undefined;
1548
+ description?: string | null | undefined;
1549
+ service?: string | null | undefined;
1550
+ currency?: "USD" | "CAD" | undefined;
1551
+ amount_total_cents?: number | undefined;
1552
+ occurred_at?: string | undefined;
1553
+ items?: {
1554
+ quantity: string;
1555
+ unit_price_cents: number;
1556
+ name?: string | null | undefined;
1557
+ external_item_id?: string | null | undefined;
1558
+ category?: string | null | undefined;
1559
+ unit_cost_cents?: number | null | undefined;
1560
+ }[] | undefined;
1561
+ }>;
1562
+ type SaleItemInput = z.input<typeof saleItemSchema>;
1563
+ type SaleInput = z.input<typeof saleCreateSchema>;
1564
+ type SaleUpdateInput = z.input<typeof saleUpdateSchema>;
1565
+ interface SaleItem {
1566
+ id: string;
1567
+ external_item_id: string | null;
1568
+ name: string | null;
1569
+ category: string | null;
1570
+ quantity: string;
1571
+ unit_price_cents: number;
1572
+ unit_cost_cents: number | null;
1573
+ }
1574
+ interface Sale {
1575
+ id: string;
1576
+ business_id: string;
1577
+ business_name?: string | null;
1578
+ external_id: string | null;
1579
+ currency: SupportedCurrency;
1580
+ amount_total_cents: number;
1581
+ description: string | null;
1582
+ service_id: string | null;
1583
+ service_key?: string | null;
1584
+ service_label?: string | null;
1585
+ occurred_at: string;
1586
+ environment: (typeof TRACKING_ENVIRONMENTS)[number];
1587
+ metadata: Record<string, unknown> | null;
1588
+ created_at: string;
1589
+ updated_at: string;
1590
+ items: SaleItem[];
1591
+ }
1592
+ interface SaleListPage {
1593
+ items: Sale[];
1594
+ total: number;
1595
+ }
1596
+ interface SaleCursorPage {
1597
+ items: Sale[];
1598
+ next_cursor: string | null;
1599
+ }
1600
+ interface SaleListQuery {
1601
+ business_id?: string;
1602
+ external_id?: string;
1603
+ currency?: SupportedCurrency;
1604
+ environment?: (typeof TRACKING_ENVIRONMENTS)[number];
1605
+ since?: string;
1606
+ until?: string;
1607
+ limit?: number;
1608
+ cursor?: string | null;
1609
+ }
1610
+
1611
+ /** Shared config for every sales HTTP helper. */
1612
+ interface SalesTransportConfig {
1613
+ /** Public (`aranv_pk_…`) or secret (`aranv_sk_…`) API key. */
1614
+ apiKey: string;
1615
+ /**
1616
+ * Base tracking endpoint, e.g. `https://aranovainternal-production.up.railway.app/tracking`.
1617
+ * The `/sales` path is appended by the helpers.
1618
+ */
1619
+ endpoint: string;
1620
+ /** Optional SDK identity headers (mirrors the event ingest client). */
1621
+ sdkVersion?: string;
1622
+ packageName?: string;
1623
+ surface?: string;
1624
+ environment?: string;
1625
+ }
1626
+
1627
+ /** Config for {@link createSalesClient}. */
1628
+ interface SalesClientConfig extends SalesTransportConfig {
1629
+ /** Applied when an individual `record()` call omits `currency`. */
1630
+ defaultCurrency?: SupportedCurrency;
1631
+ }
1632
+ /**
1633
+ * One isomorphic sales client — what a key may *do* is enforced by the backend,
1634
+ * not by hiding methods. A **public** key (`aranv_pk_…`) may `record` (the
1635
+ * backend rejects reads/CRUD from it with a `403`); a **secret** key
1636
+ * (`aranv_sk_…`), used **server-side only**, gets full read/list/update/delete.
1637
+ * Never ship a secret key in a browser bundle.
1638
+ *
1639
+ * Generic over the service-key union `TService`: bind the type emitted by
1640
+ * `@aranova/tracking-cli gen` for compile-time-checked `service` values.
1641
+ */
1642
+ interface SalesClient<TService extends string = string> {
1643
+ record(input: Omit<SaleInput, 'currency' | 'occurred_at' | 'service'> & {
1644
+ service?: TService | null;
1645
+ currency?: SupportedCurrency;
1646
+ occurred_at?: string;
1647
+ }): Promise<Sale>;
1648
+ list(query?: SaleListQuery): Promise<SaleCursorPage>;
1649
+ get(id: string): Promise<Sale>;
1650
+ update(id: string, patch: Omit<SaleUpdateInput, 'service'> & {
1651
+ service?: TService | null;
1652
+ }): Promise<Sale>;
1653
+ delete(id: string): Promise<void>;
1654
+ }
1655
+ declare function createSalesClient<TService extends string = string>(config: SalesClientConfig): SalesClient<TService>;
1656
+
1657
+ /** A business's active service, as returned by `GET /tracking/services`. */
1658
+ interface PublicServiceItem {
1659
+ key: string;
1660
+ label: string;
1661
+ }
1662
+ /**
1663
+ * Fetch the caller's business's active service taxonomy. Accepts a public or
1664
+ * secret key (the taxonomy is low-sensitivity category names). Powers the
1665
+ * `@aranova/tracking-cli gen` codegen.
1666
+ */
1667
+ declare function fetchServices(config: SalesTransportConfig): Promise<PublicServiceItem[]>;
1668
+
1669
+ /**
1670
+ * Convert a major amount (dollars `250.5`) to integer minor units (`25050`).
1671
+ *
1672
+ * Convenience only — the wire is always integer cents. Uses float multiply +
1673
+ * `Math.round`, so values that aren't exactly representable in binary float
1674
+ * (e.g. `1.005`) can round to the neighbouring cent. If you already hold an
1675
+ * exact cents integer, pass it straight through and skip this helper.
1676
+ */
1677
+ declare function toMinor(amount: number, currency: SupportedCurrency): number;
1678
+ /** Convert integer minor units (`25050`) to a major amount (`250.5`). */
1679
+ declare function fromMinor(cents: number, currency: SupportedCurrency): number;
1680
+ /**
1681
+ * Format integer minor units as a localized currency string (e.g. `"$250.50"`).
1682
+ * Uses the built-in `Intl.NumberFormat` — no extra dependency.
1683
+ */
1684
+ declare function formatMoney(cents: number, currency: SupportedCurrency, locale?: string): string;
1685
+
1686
+ /**
1687
+ * Error thrown by the sales client (`createSalesClient`) on a
1688
+ * non-2xx response. Unlike the fire-and-forget event queue (which swallows
1689
+ * failures), a sale is a transaction the caller must be able to react to.
1690
+ */
1691
+ declare class AranovaApiError extends Error {
1692
+ readonly status: number;
1693
+ readonly code: string | undefined;
1694
+ readonly requestId: string | undefined;
1695
+ constructor(message: string, options: {
1696
+ status: number;
1697
+ code?: string;
1698
+ requestId?: string;
1699
+ });
1700
+ }
1701
+
1401
1702
  /**
1402
1703
  * Props for the Google Ads tracking component.
1403
1704
  *
@@ -1506,4 +1807,4 @@ interface CreateTrackingResult<TRegistry extends TriggerRegistryConfig> {
1506
1807
  }
1507
1808
  declare function createTracking<TRegistry extends TriggerRegistryConfig>(options: CreateTrackingOptions<TRegistry>): CreateTrackingResult<TRegistry>;
1508
1809
 
1509
- export { type AutomaticEventName, ConsentBanner, type ConsentState, type CreateTrackingOptions, type CreateTrackingResult, type CtaClickConfig, type CtaClickMetadata, type EventConfig, type EventMetadata, type EventName, type FormStartConfig, type FormStartMetadata, type FormSubmitConfig, type FormSubmitMetadata, GoogleAdsTracking, type GoogleAdsTrackingProps, type JsonValue, type ManualEventName, type MultiPageSessionConfig, type MultiPageSessionMetadata, type PageViewConfig, type PageViewMetadata, type PhoneClickConfig, type PhoneClickMetadata, type RegisteredAutomaticEvents, type RegisteredManualEvents, type ScrollDepthConfig, type ScrollDepthMetadata, type SpecificPageName, type SpecificPageVisitConfig, type SpecificPageVisitMetadata, TRACKING_PARAM_KEYS, type TimeOnSiteConfig, type TimeOnSiteMetadata, type TrackingClient, type TrackingClientContext, type TrackingEventCreatePayload, type TrackingInitConfig, type TrackingInstallSurface, type TrackingParams, type TrackingProviderProps, type TrackingSessionUpsertPayload, type TriggerRegistryConfig, type TypedTrackEventOptions, type TypedTrackingClient, captureTrackingParamsFromLocation, createTracking, createTrackingClientContext, createTrackingEventCreatePayload, createTrackingSessionUpsertPayload, getConsentState, setConsentState, useConsentState, useGclid, useTrackingParams };
1810
+ export { AranovaApiError, type AutomaticEventName, ConsentBanner, type ConsentState, type CreateTrackingOptions, type CreateTrackingResult, type CtaClickConfig, type CtaClickMetadata, type EventConfig, type EventMetadata, type EventName, type FormStartConfig, type FormStartMetadata, type FormSubmitConfig, type FormSubmitMetadata, GoogleAdsTracking, type GoogleAdsTrackingProps, type JsonValue, type ManualEventName, type MultiPageSessionConfig, type MultiPageSessionMetadata, type PageViewConfig, type PageViewMetadata, type PhoneClickConfig, type PhoneClickMetadata, type PublicServiceItem, type RegisteredAutomaticEvents, type RegisteredManualEvents, type Sale, type SaleCursorPage, type SaleInput, type SaleItem, type SaleItemInput, type SaleListPage, type SaleListQuery, type SaleUpdateInput, type SalesClient, type SalesClientConfig, type SalesTransportConfig, type ScrollDepthConfig, type ScrollDepthMetadata, type SpecificPageName, type SpecificPageVisitConfig, type SpecificPageVisitMetadata, type SupportedCurrency, TRACKING_PARAM_KEYS, type TimeOnSiteConfig, type TimeOnSiteMetadata, type TrackingClient, type TrackingClientContext, type TrackingEventCreatePayload, type TrackingInitConfig, type TrackingInstallSurface, type TrackingParams, type TrackingProviderProps, type TrackingSessionUpsertPayload, type TriggerRegistryConfig, type TypedTrackEventOptions, type TypedTrackingClient, captureTrackingParamsFromLocation, createSalesClient, createTracking, createTrackingClientContext, createTrackingEventCreatePayload, createTrackingSessionUpsertPayload, fetchServices, formatMoney, fromMinor, getConsentState, setConsentState, toMinor, useConsentState, useGclid, useTrackingParams };