brainerce 1.37.3 → 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
@@ -3361,6 +3361,51 @@ console.log(store.language); // 'en', 'he', etc.
3361
3361
 
3362
3362
  ---
3363
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
+
3364
3409
  ## Admin API Reference
3365
3410
 
3366
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)).
package/dist/index.d.mts CHANGED
@@ -5306,6 +5306,30 @@ interface BlogPostListResponse {
5306
5306
  totalPages: number;
5307
5307
  };
5308
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
+ }
5309
5333
  interface BrainerceApiError {
5310
5334
  statusCode: number;
5311
5335
  message: string;
@@ -5491,6 +5515,39 @@ declare class BrainerceClient {
5491
5515
  * `'ltr'` for everything else (including unknown locales).
5492
5516
  */
5493
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>;
5494
5551
  /**
5495
5552
  * Get a list of products with pagination and filtering
5496
5553
  * Works in vibe-coded, storefront (public), and admin mode
package/dist/index.d.ts CHANGED
@@ -5306,6 +5306,30 @@ interface BlogPostListResponse {
5306
5306
  totalPages: number;
5307
5307
  };
5308
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
+ }
5309
5333
  interface BrainerceApiError {
5310
5334
  statusCode: number;
5311
5335
  message: string;
@@ -5491,6 +5515,39 @@ declare class BrainerceClient {
5491
5515
  * `'ltr'` for everything else (including unknown locales).
5492
5516
  */
5493
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>;
5494
5551
  /**
5495
5552
  * Get a list of products with pagination and filtering
5496
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.3",
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",