brainerce 1.37.2 → 1.38.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
@@ -2050,8 +2050,11 @@ const checkout = await client.createCheckout({
2050
2050
  ```
2051
2051
 
2052
2052
  > The region is recorded on the checkout and used for payment-provider scoping.
2053
- > Currency continues to follow the cart until FX price conversion lands, so
2054
- > passing a `regionId` does not re-price the cart.
2053
+ > **FX-at-checkout:** when the region currency differs from the store base and its
2054
+ > provider can settle it (presentment-enabled — Stripe today), the buyer is charged
2055
+ > in the region currency and the checkout response carries a `presentment` overlay
2056
+ > (`presentment.total` / `presentment.currency` = what's charged). Otherwise the
2057
+ > checkout is charged in the store base currency.
2055
2058
 
2056
2059
  #### Region display pricing (`getProducts({ regionId })`)
2057
2060
 
@@ -3358,6 +3361,51 @@ console.log(store.language); // 'en', 'he', etc.
3358
3361
 
3359
3362
  ---
3360
3363
 
3364
+ ### Traffic Analytics (built-in, no GA4 needed)
3365
+
3366
+ Brainerce has a **native cookieless analytics pipeline** — visits, visitors, countries, sources, devices, conversion funnel — visible in the merchant dashboard under **Dashboard → Traffic**. You don't need GA4, Meta Pixel, or any third-party script.
3367
+
3368
+ **Option A — Script tag (recommended, zero JS):**
3369
+
3370
+ Add one line to your root layout `<head>`:
3371
+
3372
+ ```html
3373
+ <!-- Vibe-coded (salesChannelId) mode -->
3374
+ <script defer src="https://api.brainerce.com/t.js" data-channel="vc_abc123"></script>
3375
+
3376
+ <!-- storeId mode -->
3377
+ <script defer src="https://api.brainerce.com/t.js" data-store-id="your_store_id"></script>
3378
+ ```
3379
+
3380
+ The pixel auto-tracks pageviews on load and on every SPA route change, de-dupes consecutive identical paths, and measures active dwell time. Scaffolds from `create-brainerce-store ≥ 1.50` include it automatically.
3381
+
3382
+ **Option B — SDK method (JS import, same endpoint):**
3383
+
3384
+ ```typescript
3385
+ // Basic pageview — call this on route changes if you're not using t.js
3386
+ client.trackEvent({ eventType: 'pageview', path: '/products/shoes' });
3387
+
3388
+ // With UTM attribution (from URL params)
3389
+ const params = new URLSearchParams(window.location.search);
3390
+ client.trackEvent({
3391
+ path: window.location.pathname,
3392
+ utmSource: params.get('utm_source') ?? undefined,
3393
+ utmMedium: params.get('utm_medium') ?? undefined,
3394
+ utmCampaign: params.get('utm_campaign') ?? undefined,
3395
+ screenWidth: window.innerWidth,
3396
+ lang: navigator.language,
3397
+ });
3398
+
3399
+ // Engagement dwell time (send on route change or pagehide)
3400
+ client.trackEvent({ eventType: 'engagement', path: '/products/shoes', engagedMs: 12000 });
3401
+ ```
3402
+
3403
+ > `trackEvent()` is fire-and-forget — errors are silently swallowed so a failed beacon never breaks your storefront. Available in `salesChannelId` and `storeId` modes.
3404
+
3405
+ **Privacy:** cookieless, no PII stored. The visitor IP is resolved to a country server-side and then discarded. No cookie-consent banner required under GDPR/ePrivacy.
3406
+
3407
+ ---
3408
+
3361
3409
  ## Admin API Reference
3362
3410
 
3363
3411
  > ⛔ **Server-side only.** The `apiKey` (`brainerce_*`) is a privileged secret — NEVER put it in browser code, client bundles, or any code that ships to the user's machine. It belongs in an environment variable on your server. For building the customer-facing storefront, use `salesChannelId` instead (see [Quick Start](#quick-start)).
@@ -3638,8 +3686,10 @@ const { region: resolved } = await store.getAutoRegion('DE'); // server-side, on
3638
3686
 
3639
3687
  > **Multi-region checkout:** pass a `regionId` to `createCheckout` to associate the
3640
3688
  > checkout with a region (for reporting + payment-provider scoping). The region
3641
- > must belong to the store. Currency still follows the cart until FX price
3642
- > conversion lands. See the Checkout section.
3689
+ > must belong to the store. **FX-at-checkout:** presentment-enabled regions
3690
+ > (Stripe today) charge in the region currency and return a `presentment` overlay;
3691
+ > otherwise the checkout is charged in the store base currency. See the Checkout
3692
+ > section.
3643
3693
 
3644
3694
  ### Price Lists (per-region pricing)
3645
3695
 
package/dist/index.d.mts CHANGED
@@ -2507,8 +2507,11 @@ interface Checkout {
2507
2507
  currency: string;
2508
2508
  /**
2509
2509
  * Region this checkout is associated with (multi-region commerce), or null.
2510
- * Recorded for reporting + provider scoping; currency follows the cart until
2511
- * FX price conversion lands.
2510
+ * Recorded for reporting + provider scoping. FX-at-checkout: when the region
2511
+ * currency differs from the store base and its provider can settle it
2512
+ * (presentment-enabled — Stripe today), the buyer is charged in the region
2513
+ * currency and the `presentment` overlay below carries the charged amounts;
2514
+ * otherwise the checkout is charged in the store base currency.
2512
2515
  */
2513
2516
  regionId?: string | null;
2514
2517
  /** Subtotal as string - use parseFloat() */
@@ -2560,6 +2563,41 @@ interface Checkout {
2560
2563
  * @see ReservationInfo
2561
2564
  */
2562
2565
  reservation?: ReservationInfo;
2566
+ /**
2567
+ * FX-at-checkout presentment overlay. Present ONLY when the checkout's region
2568
+ * uses a different currency from the store base AND that region charges in its
2569
+ * own currency (presentment). These are the amounts the buyer will actually be
2570
+ * CHARGED, in `presentment.currency` — `presentment.total` equals the payment
2571
+ * intent amount to the cent. Render these (not the base `total`/`currency`
2572
+ * above) when present. Absent = the checkout is charged in the store base
2573
+ * currency (the fields above are authoritative).
2574
+ *
2575
+ * @example
2576
+ * const c = await client.getCheckout(id);
2577
+ * const shown = c.presentment
2578
+ * ? formatPrice(c.presentment.total, { currency: c.presentment.currency })
2579
+ * : formatPrice(c.total, { currency: c.currency });
2580
+ */
2581
+ presentment?: {
2582
+ /** ISO-4217 currency the buyer is charged in (e.g. "EUR"). */
2583
+ currency: string;
2584
+ /** Subtotal in the presentment currency as string - use parseFloat(). */
2585
+ subtotal: string;
2586
+ /** Discount amount in the presentment currency as string. */
2587
+ discountAmount: string;
2588
+ /** Shipping cost in the presentment currency as string. */
2589
+ shippingAmount: string;
2590
+ /** Tax amount in the presentment currency as string. */
2591
+ taxAmount: string;
2592
+ /** Surcharge amount in the presentment currency as string. */
2593
+ surchargeAmount: string;
2594
+ /** Grand total in the presentment currency — equals the charged amount. */
2595
+ total: string;
2596
+ /** Base→presentment rate applied (buffer included), as string. */
2597
+ fxChargingRate: string;
2598
+ /** FX buffer percent applied to the mid-market rate, as string (or null). */
2599
+ fxBufferPercent: string | null;
2600
+ };
2563
2601
  }
2564
2602
  /**
2565
2603
  * Options for creating a checkout from a cart.
@@ -5268,6 +5306,30 @@ interface BlogPostListResponse {
5268
5306
  totalPages: number;
5269
5307
  };
5270
5308
  }
5309
+ /**
5310
+ * Payload for `client.trackEvent()` — the programmatic counterpart to the
5311
+ * `t.js` script-tag pixel. Every field is optional; the server degrades
5312
+ * gracefully on missing values. Deliberately collects no PII: `path` is
5313
+ * pathname-only, `referrer` is reduced to a hostname server-side, and the
5314
+ * visitor IP is resolved to a country then discarded.
5315
+ */
5316
+ interface TrackEventPayload {
5317
+ /** `'pageview'` (default) or `'engagement'` (active dwell-time report). */
5318
+ eventType?: 'pageview' | 'engagement';
5319
+ /** `window.location.pathname` — no query string (intentional, avoids PII). */
5320
+ path?: string;
5321
+ /** `document.referrer` — full URL, reduced to hostname server-side. */
5322
+ referrer?: string;
5323
+ utmSource?: string;
5324
+ utmMedium?: string;
5325
+ utmCampaign?: string;
5326
+ /** Viewport width in px — used for mobile/tablet/desktop classification. */
5327
+ screenWidth?: number;
5328
+ /** `navigator.language` — coarse locale hint for future breakdowns. */
5329
+ lang?: string;
5330
+ /** Active dwell time in ms. Only meaningful for `eventType: 'engagement'`. Max 1 800 000 (30 min). */
5331
+ engagedMs?: number;
5332
+ }
5271
5333
  interface BrainerceApiError {
5272
5334
  statusCode: number;
5273
5335
  message: string;
@@ -5453,6 +5515,39 @@ declare class BrainerceClient {
5453
5515
  * `'ltr'` for everything else (including unknown locales).
5454
5516
  */
5455
5517
  getStoreDirection(locale?: string | null): 'ltr' | 'rtl';
5518
+ /**
5519
+ * Send a storefront analytics beacon (pageview or engagement).
5520
+ *
5521
+ * This is the programmatic counterpart to the `t.js` script-tag pixel —
5522
+ * use it when you prefer a JS import over a `<script>` tag, or when you
5523
+ * need to fire events the auto-tracker doesn't cover.
5524
+ *
5525
+ * **If you're already loading `t.js`, you do NOT need this** — the pixel
5526
+ * handles pageviews and SPA route changes automatically.
5527
+ *
5528
+ * The call is fire-and-forget: errors are swallowed so a failed beacon
5529
+ * never breaks your storefront. Available in `salesChannelId` and `storeId`
5530
+ * modes; silently skipped in admin mode (server-to-server beacons are
5531
+ * meaningless without a real browser session).
5532
+ *
5533
+ * @example
5534
+ * ```typescript
5535
+ * // Basic pageview (equivalent to what t.js sends automatically)
5536
+ * client.trackEvent({ eventType: 'pageview', path: '/products/shoes' });
5537
+ *
5538
+ * // With UTM attribution
5539
+ * client.trackEvent({
5540
+ * path: '/products/shoes',
5541
+ * utmSource: 'newsletter',
5542
+ * utmMedium: 'email',
5543
+ * utmCampaign: 'summer-sale',
5544
+ * });
5545
+ *
5546
+ * // Engagement (active dwell time in ms — send on route change or pagehide)
5547
+ * client.trackEvent({ eventType: 'engagement', path: '/products/shoes', engagedMs: 12000 });
5548
+ * ```
5549
+ */
5550
+ trackEvent(payload?: TrackEventPayload): Promise<void>;
5456
5551
  /**
5457
5552
  * Get a list of products with pagination and filtering
5458
5553
  * Works in vibe-coded, storefront (public), and admin mode
package/dist/index.d.ts CHANGED
@@ -2507,8 +2507,11 @@ interface Checkout {
2507
2507
  currency: string;
2508
2508
  /**
2509
2509
  * Region this checkout is associated with (multi-region commerce), or null.
2510
- * Recorded for reporting + provider scoping; currency follows the cart until
2511
- * FX price conversion lands.
2510
+ * Recorded for reporting + provider scoping. FX-at-checkout: when the region
2511
+ * currency differs from the store base and its provider can settle it
2512
+ * (presentment-enabled — Stripe today), the buyer is charged in the region
2513
+ * currency and the `presentment` overlay below carries the charged amounts;
2514
+ * otherwise the checkout is charged in the store base currency.
2512
2515
  */
2513
2516
  regionId?: string | null;
2514
2517
  /** Subtotal as string - use parseFloat() */
@@ -2560,6 +2563,41 @@ interface Checkout {
2560
2563
  * @see ReservationInfo
2561
2564
  */
2562
2565
  reservation?: ReservationInfo;
2566
+ /**
2567
+ * FX-at-checkout presentment overlay. Present ONLY when the checkout's region
2568
+ * uses a different currency from the store base AND that region charges in its
2569
+ * own currency (presentment). These are the amounts the buyer will actually be
2570
+ * CHARGED, in `presentment.currency` — `presentment.total` equals the payment
2571
+ * intent amount to the cent. Render these (not the base `total`/`currency`
2572
+ * above) when present. Absent = the checkout is charged in the store base
2573
+ * currency (the fields above are authoritative).
2574
+ *
2575
+ * @example
2576
+ * const c = await client.getCheckout(id);
2577
+ * const shown = c.presentment
2578
+ * ? formatPrice(c.presentment.total, { currency: c.presentment.currency })
2579
+ * : formatPrice(c.total, { currency: c.currency });
2580
+ */
2581
+ presentment?: {
2582
+ /** ISO-4217 currency the buyer is charged in (e.g. "EUR"). */
2583
+ currency: string;
2584
+ /** Subtotal in the presentment currency as string - use parseFloat(). */
2585
+ subtotal: string;
2586
+ /** Discount amount in the presentment currency as string. */
2587
+ discountAmount: string;
2588
+ /** Shipping cost in the presentment currency as string. */
2589
+ shippingAmount: string;
2590
+ /** Tax amount in the presentment currency as string. */
2591
+ taxAmount: string;
2592
+ /** Surcharge amount in the presentment currency as string. */
2593
+ surchargeAmount: string;
2594
+ /** Grand total in the presentment currency — equals the charged amount. */
2595
+ total: string;
2596
+ /** Base→presentment rate applied (buffer included), as string. */
2597
+ fxChargingRate: string;
2598
+ /** FX buffer percent applied to the mid-market rate, as string (or null). */
2599
+ fxBufferPercent: string | null;
2600
+ };
2563
2601
  }
2564
2602
  /**
2565
2603
  * Options for creating a checkout from a cart.
@@ -5268,6 +5306,30 @@ interface BlogPostListResponse {
5268
5306
  totalPages: number;
5269
5307
  };
5270
5308
  }
5309
+ /**
5310
+ * Payload for `client.trackEvent()` — the programmatic counterpart to the
5311
+ * `t.js` script-tag pixel. Every field is optional; the server degrades
5312
+ * gracefully on missing values. Deliberately collects no PII: `path` is
5313
+ * pathname-only, `referrer` is reduced to a hostname server-side, and the
5314
+ * visitor IP is resolved to a country then discarded.
5315
+ */
5316
+ interface TrackEventPayload {
5317
+ /** `'pageview'` (default) or `'engagement'` (active dwell-time report). */
5318
+ eventType?: 'pageview' | 'engagement';
5319
+ /** `window.location.pathname` — no query string (intentional, avoids PII). */
5320
+ path?: string;
5321
+ /** `document.referrer` — full URL, reduced to hostname server-side. */
5322
+ referrer?: string;
5323
+ utmSource?: string;
5324
+ utmMedium?: string;
5325
+ utmCampaign?: string;
5326
+ /** Viewport width in px — used for mobile/tablet/desktop classification. */
5327
+ screenWidth?: number;
5328
+ /** `navigator.language` — coarse locale hint for future breakdowns. */
5329
+ lang?: string;
5330
+ /** Active dwell time in ms. Only meaningful for `eventType: 'engagement'`. Max 1 800 000 (30 min). */
5331
+ engagedMs?: number;
5332
+ }
5271
5333
  interface BrainerceApiError {
5272
5334
  statusCode: number;
5273
5335
  message: string;
@@ -5453,6 +5515,39 @@ declare class BrainerceClient {
5453
5515
  * `'ltr'` for everything else (including unknown locales).
5454
5516
  */
5455
5517
  getStoreDirection(locale?: string | null): 'ltr' | 'rtl';
5518
+ /**
5519
+ * Send a storefront analytics beacon (pageview or engagement).
5520
+ *
5521
+ * This is the programmatic counterpart to the `t.js` script-tag pixel —
5522
+ * use it when you prefer a JS import over a `<script>` tag, or when you
5523
+ * need to fire events the auto-tracker doesn't cover.
5524
+ *
5525
+ * **If you're already loading `t.js`, you do NOT need this** — the pixel
5526
+ * handles pageviews and SPA route changes automatically.
5527
+ *
5528
+ * The call is fire-and-forget: errors are swallowed so a failed beacon
5529
+ * never breaks your storefront. Available in `salesChannelId` and `storeId`
5530
+ * modes; silently skipped in admin mode (server-to-server beacons are
5531
+ * meaningless without a real browser session).
5532
+ *
5533
+ * @example
5534
+ * ```typescript
5535
+ * // Basic pageview (equivalent to what t.js sends automatically)
5536
+ * client.trackEvent({ eventType: 'pageview', path: '/products/shoes' });
5537
+ *
5538
+ * // With UTM attribution
5539
+ * client.trackEvent({
5540
+ * path: '/products/shoes',
5541
+ * utmSource: 'newsletter',
5542
+ * utmMedium: 'email',
5543
+ * utmCampaign: 'summer-sale',
5544
+ * });
5545
+ *
5546
+ * // Engagement (active dwell time in ms — send on route change or pagehide)
5547
+ * client.trackEvent({ eventType: 'engagement', path: '/products/shoes', engagedMs: 12000 });
5548
+ * ```
5549
+ */
5550
+ trackEvent(payload?: TrackEventPayload): Promise<void>;
5456
5551
  /**
5457
5552
  * Get a list of products with pagination and filtering
5458
5553
  * Works in vibe-coded, storefront (public), and admin mode
package/dist/index.js CHANGED
@@ -1072,6 +1072,49 @@ var BrainerceClient = class {
1072
1072
  getStoreDirection(locale) {
1073
1073
  return getDirectionForLocale(locale ?? this.locale);
1074
1074
  }
1075
+ // -------------------- Analytics --------------------
1076
+ /**
1077
+ * Send a storefront analytics beacon (pageview or engagement).
1078
+ *
1079
+ * This is the programmatic counterpart to the `t.js` script-tag pixel —
1080
+ * use it when you prefer a JS import over a `<script>` tag, or when you
1081
+ * need to fire events the auto-tracker doesn't cover.
1082
+ *
1083
+ * **If you're already loading `t.js`, you do NOT need this** — the pixel
1084
+ * handles pageviews and SPA route changes automatically.
1085
+ *
1086
+ * The call is fire-and-forget: errors are swallowed so a failed beacon
1087
+ * never breaks your storefront. Available in `salesChannelId` and `storeId`
1088
+ * modes; silently skipped in admin mode (server-to-server beacons are
1089
+ * meaningless without a real browser session).
1090
+ *
1091
+ * @example
1092
+ * ```typescript
1093
+ * // Basic pageview (equivalent to what t.js sends automatically)
1094
+ * client.trackEvent({ eventType: 'pageview', path: '/products/shoes' });
1095
+ *
1096
+ * // With UTM attribution
1097
+ * client.trackEvent({
1098
+ * path: '/products/shoes',
1099
+ * utmSource: 'newsletter',
1100
+ * utmMedium: 'email',
1101
+ * utmCampaign: 'summer-sale',
1102
+ * });
1103
+ *
1104
+ * // Engagement (active dwell time in ms — send on route change or pagehide)
1105
+ * client.trackEvent({ eventType: 'engagement', path: '/products/shoes', engagedMs: 12000 });
1106
+ * ```
1107
+ */
1108
+ async trackEvent(payload = {}) {
1109
+ try {
1110
+ if (this.isVibeCodedMode()) {
1111
+ await this.vibeCodedRequest("POST", "/events", payload);
1112
+ } else if (this.isStorefrontMode()) {
1113
+ await this.storefrontRequest("POST", "/events", payload);
1114
+ }
1115
+ } catch {
1116
+ }
1117
+ }
1075
1118
  // -------------------- Products --------------------
1076
1119
  /**
1077
1120
  * Get a list of products with pagination and filtering
package/dist/index.mjs CHANGED
@@ -1002,6 +1002,49 @@ var BrainerceClient = class {
1002
1002
  getStoreDirection(locale) {
1003
1003
  return getDirectionForLocale(locale ?? this.locale);
1004
1004
  }
1005
+ // -------------------- Analytics --------------------
1006
+ /**
1007
+ * Send a storefront analytics beacon (pageview or engagement).
1008
+ *
1009
+ * This is the programmatic counterpart to the `t.js` script-tag pixel —
1010
+ * use it when you prefer a JS import over a `<script>` tag, or when you
1011
+ * need to fire events the auto-tracker doesn't cover.
1012
+ *
1013
+ * **If you're already loading `t.js`, you do NOT need this** — the pixel
1014
+ * handles pageviews and SPA route changes automatically.
1015
+ *
1016
+ * The call is fire-and-forget: errors are swallowed so a failed beacon
1017
+ * never breaks your storefront. Available in `salesChannelId` and `storeId`
1018
+ * modes; silently skipped in admin mode (server-to-server beacons are
1019
+ * meaningless without a real browser session).
1020
+ *
1021
+ * @example
1022
+ * ```typescript
1023
+ * // Basic pageview (equivalent to what t.js sends automatically)
1024
+ * client.trackEvent({ eventType: 'pageview', path: '/products/shoes' });
1025
+ *
1026
+ * // With UTM attribution
1027
+ * client.trackEvent({
1028
+ * path: '/products/shoes',
1029
+ * utmSource: 'newsletter',
1030
+ * utmMedium: 'email',
1031
+ * utmCampaign: 'summer-sale',
1032
+ * });
1033
+ *
1034
+ * // Engagement (active dwell time in ms — send on route change or pagehide)
1035
+ * client.trackEvent({ eventType: 'engagement', path: '/products/shoes', engagedMs: 12000 });
1036
+ * ```
1037
+ */
1038
+ async trackEvent(payload = {}) {
1039
+ try {
1040
+ if (this.isVibeCodedMode()) {
1041
+ await this.vibeCodedRequest("POST", "/events", payload);
1042
+ } else if (this.isStorefrontMode()) {
1043
+ await this.storefrontRequest("POST", "/events", payload);
1044
+ }
1045
+ } catch {
1046
+ }
1047
+ }
1005
1048
  // -------------------- Products --------------------
1006
1049
  /**
1007
1050
  * Get a list of products with pagination and filtering
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "brainerce",
3
- "version": "1.37.2",
3
+ "version": "1.38.0",
4
4
  "description": "Official SDK for building e-commerce storefronts with Brainerce Platform. Perfect for vibe-coded sites, AI-built stores (Cursor, Lovable, v0), and custom storefronts.",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",