mytart 0.1.2 → 0.1.3

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
@@ -11,6 +11,7 @@
11
11
  - 🔷 **TypeScript-first**: precise typings, object-parameter style for great DX
12
12
  - ðŸ“Ķ **Dual ESM/CJS output**: works with `import` and `require`
13
13
  - ðŸŠķ **Lightweight**: direct HTTP via axios, no SDK overhead
14
+ - ⚡ **SPA-ready**: automatic page view tracking on route changes (Vercel Analytics)
14
15
  - ✅ **Node.js â‰Ĩ 18**
15
16
 
16
17
  ## Installation
@@ -89,11 +90,22 @@ await analytics.page({ url: 'https://example.com/pricing', name: 'Pricing' });
89
90
  ### Vercel Analytics
90
91
 
91
92
  ```typescript
92
- { provider: 'vercel-analytics', analyticsId: 'YOUR_ANALYTICS_ID', apiUrl?: string }
93
+ { provider: 'vercel-analytics', analyticsId?: string, apiUrl?: string }
93
94
  ```
94
95
 
95
- `analyticsId` is your `VERCEL_ANALYTICS_ID` environment variable (or the ID in your Vercel project's Analytics settings).
96
- For on-Vercel deployments you can override `apiUrl` to `/_vercel/insights/event`.
96
+ **Auto-configuration**: When deployed on Vercel with Web Analytics enabled, `analyticsId` is automatically retrieved from `window.__VERCEL_ANALYTICS_ID__`. No configuration needed — just add the provider:
97
+
98
+ ```typescript
99
+ // On Vercel — no config needed
100
+ { provider: 'vercel-analytics', enabled: true }
101
+
102
+ // Self-hosted or offline — provide ID explicitly
103
+ { provider: 'vercel-analytics', analyticsId: 'YOUR_ANALYTICS_ID', enabled: true }
104
+ ```
105
+
106
+ **SPA Support**: Page view tracking via `history.pushState` and `popstate` is enabled by default. Use `analytics.disableSPA()` to opt out.
107
+
108
+ **Endpoints**: By default, events are sent to `https://vitals.vercel-insights.com/v1/vitals`. On Vercel infrastructure, set `apiUrl: '/_vercel/insights/event'` for optimal routing.
97
109
 
98
110
  > **Note**: Vercel Analytics does not support `identify()` — it returns an error result (privacy-first by design).
99
111
 
@@ -171,6 +183,14 @@ Remove a provider by name.
171
183
 
172
184
  Returns the list of active provider names.
173
185
 
186
+ ### `analytics.enableSPA(): void`
187
+
188
+ Enable automatic SPA page view tracking. Automatically called on initialization for providers that support it (Vercel Analytics). Tracks page views on `history.pushState` and `popstate` events.
189
+
190
+ ### `analytics.disableSPA(): void`
191
+
192
+ Disable automatic SPA page view tracking and clean up event listeners. Call this if you handle page views manually or are on a multi-page app.
193
+
174
194
  ### `TrackResult`
175
195
 
176
196
  Every method returns `Promise<TrackResult[]>` — one result per provider:
package/dist/index.d.mts CHANGED
@@ -122,6 +122,15 @@ declare class Mytart {
122
122
  addProvider(config: ProviderConfig): void;
123
123
  removeProvider(name: string): void;
124
124
  getProviders(): string[];
125
+ /**
126
+ * Enable automatic SPA page view tracking for all supported providers.
127
+ * Should be called once in your app's entry point after initialization.
128
+ */
129
+ enableSPA(): void;
130
+ /**
131
+ * Disable SPA page view tracking and clean up event listeners.
132
+ */
133
+ disableSPA(): void;
125
134
  }
126
135
 
127
136
  declare abstract class BaseProvider {
@@ -206,11 +215,21 @@ declare class VercelAnalyticsProvider extends BaseProvider {
206
215
  private readonly config;
207
216
  private readonly http;
208
217
  private readonly endpoint;
218
+ private readonly spaListeners;
219
+ private spaEnabled;
209
220
  constructor(config: VercelAnalyticsConfig);
210
221
  /** Check if Vercel analytics can be used (running on Vercel or analytics ID available). */
211
222
  isAvailable(): boolean;
212
223
  /** Get the analytics ID, attempting to resolve from config or injected global. */
213
224
  private resolveAnalyticsId;
225
+ /**
226
+ * Enable automatic SPA page view tracking by listening to browser history changes.
227
+ * Call this once after initialization in your app's entry point.
228
+ * Uses `window.location.href` for URL and `document.referrer` for referrer.
229
+ */
230
+ enableSPA(): void;
231
+ /** Disable SPA tracking and clean up event listeners. */
232
+ disableSPA(): void;
214
233
  track({ event, properties, timestamp, context }: TrackOptions): Promise<TrackResult>;
215
234
  /**
216
235
  * Vercel Analytics is privacy-focused and does not support traditional user
package/dist/index.d.ts CHANGED
@@ -122,6 +122,15 @@ declare class Mytart {
122
122
  addProvider(config: ProviderConfig): void;
123
123
  removeProvider(name: string): void;
124
124
  getProviders(): string[];
125
+ /**
126
+ * Enable automatic SPA page view tracking for all supported providers.
127
+ * Should be called once in your app's entry point after initialization.
128
+ */
129
+ enableSPA(): void;
130
+ /**
131
+ * Disable SPA page view tracking and clean up event listeners.
132
+ */
133
+ disableSPA(): void;
125
134
  }
126
135
 
127
136
  declare abstract class BaseProvider {
@@ -206,11 +215,21 @@ declare class VercelAnalyticsProvider extends BaseProvider {
206
215
  private readonly config;
207
216
  private readonly http;
208
217
  private readonly endpoint;
218
+ private readonly spaListeners;
219
+ private spaEnabled;
209
220
  constructor(config: VercelAnalyticsConfig);
210
221
  /** Check if Vercel analytics can be used (running on Vercel or analytics ID available). */
211
222
  isAvailable(): boolean;
212
223
  /** Get the analytics ID, attempting to resolve from config or injected global. */
213
224
  private resolveAnalyticsId;
225
+ /**
226
+ * Enable automatic SPA page view tracking by listening to browser history changes.
227
+ * Call this once after initialization in your app's entry point.
228
+ * Uses `window.location.href` for URL and `document.referrer` for referrer.
229
+ */
230
+ enableSPA(): void;
231
+ /** Disable SPA tracking and clean up event listeners. */
232
+ disableSPA(): void;
214
233
  track({ event, properties, timestamp, context }: TrackOptions): Promise<TrackResult>;
215
234
  /**
216
235
  * Vercel Analytics is privacy-focused and does not support traditional user
package/dist/index.js CHANGED
@@ -624,6 +624,8 @@ var VercelAnalyticsProvider = class extends BaseProvider {
624
624
  constructor(config) {
625
625
  super();
626
626
  this.name = "vercel-analytics";
627
+ this.spaListeners = [];
628
+ this.spaEnabled = false;
627
629
  this.config = config;
628
630
  this.http = createHttpClient();
629
631
  this.endpoint = config.apiUrl ?? VERCEL_ENDPOINT;
@@ -642,6 +644,44 @@ var VercelAnalyticsProvider = class extends BaseProvider {
642
644
  resolveAnalyticsId() {
643
645
  return this.config.analyticsId ?? getInjectedAnalyticsId();
644
646
  }
647
+ /**
648
+ * Enable automatic SPA page view tracking by listening to browser history changes.
649
+ * Call this once after initialization in your app's entry point.
650
+ * Uses `window.location.href` for URL and `document.referrer` for referrer.
651
+ */
652
+ enableSPA() {
653
+ if (typeof window === "undefined" || this.spaEnabled) {
654
+ return;
655
+ }
656
+ this.spaEnabled = true;
657
+ const trackPageView = () => {
658
+ const url = window.location.href;
659
+ const referrer = document.referrer || void 0;
660
+ this.page({ name: "pageview", url, referrer }).catch(() => {
661
+ });
662
+ };
663
+ const originalPushState = window.history.pushState.bind(window.history);
664
+ const originalReplaceState = window.history.replaceState.bind(window.history);
665
+ window.history.pushState = function(...args) {
666
+ originalPushState(...args);
667
+ trackPageView();
668
+ };
669
+ window.history.replaceState = function(...args) {
670
+ originalReplaceState(...args);
671
+ };
672
+ window.addEventListener("popstate", trackPageView);
673
+ this.spaListeners.push(() => {
674
+ window.history.pushState = originalPushState;
675
+ window.history.replaceState = originalReplaceState;
676
+ window.removeEventListener("popstate", trackPageView);
677
+ });
678
+ }
679
+ /** Disable SPA tracking and clean up event listeners. */
680
+ disableSPA() {
681
+ this.spaListeners.forEach((cleanup) => cleanup());
682
+ this.spaListeners.length = 0;
683
+ this.spaEnabled = false;
684
+ }
645
685
  async track({ event, properties, timestamp, context }) {
646
686
  const analyticsId = this.resolveAnalyticsId();
647
687
  if (!analyticsId) {
@@ -766,6 +806,7 @@ var Mytart = class {
766
806
  }
767
807
  return true;
768
808
  });
809
+ this.enableSPA();
769
810
  }
770
811
  async track(options) {
771
812
  const enriched = {
@@ -799,6 +840,27 @@ var Mytart = class {
799
840
  getProviders() {
800
841
  return this.providers.map((p) => p.name);
801
842
  }
843
+ /**
844
+ * Enable automatic SPA page view tracking for all supported providers.
845
+ * Should be called once in your app's entry point after initialization.
846
+ */
847
+ enableSPA() {
848
+ for (const provider of this.providers) {
849
+ if (provider.name === "vercel-analytics") {
850
+ provider.enableSPA();
851
+ }
852
+ }
853
+ }
854
+ /**
855
+ * Disable SPA page view tracking and clean up event listeners.
856
+ */
857
+ disableSPA() {
858
+ for (const provider of this.providers) {
859
+ if (provider.name === "vercel-analytics") {
860
+ provider.disableSPA();
861
+ }
862
+ }
863
+ }
802
864
  };
803
865
  // Annotate the CommonJS export names for ESM import in node:
804
866
  0 && (module.exports = {
package/dist/index.mjs CHANGED
@@ -580,6 +580,8 @@ var VercelAnalyticsProvider = class extends BaseProvider {
580
580
  constructor(config) {
581
581
  super();
582
582
  this.name = "vercel-analytics";
583
+ this.spaListeners = [];
584
+ this.spaEnabled = false;
583
585
  this.config = config;
584
586
  this.http = createHttpClient();
585
587
  this.endpoint = config.apiUrl ?? VERCEL_ENDPOINT;
@@ -598,6 +600,44 @@ var VercelAnalyticsProvider = class extends BaseProvider {
598
600
  resolveAnalyticsId() {
599
601
  return this.config.analyticsId ?? getInjectedAnalyticsId();
600
602
  }
603
+ /**
604
+ * Enable automatic SPA page view tracking by listening to browser history changes.
605
+ * Call this once after initialization in your app's entry point.
606
+ * Uses `window.location.href` for URL and `document.referrer` for referrer.
607
+ */
608
+ enableSPA() {
609
+ if (typeof window === "undefined" || this.spaEnabled) {
610
+ return;
611
+ }
612
+ this.spaEnabled = true;
613
+ const trackPageView = () => {
614
+ const url = window.location.href;
615
+ const referrer = document.referrer || void 0;
616
+ this.page({ name: "pageview", url, referrer }).catch(() => {
617
+ });
618
+ };
619
+ const originalPushState = window.history.pushState.bind(window.history);
620
+ const originalReplaceState = window.history.replaceState.bind(window.history);
621
+ window.history.pushState = function(...args) {
622
+ originalPushState(...args);
623
+ trackPageView();
624
+ };
625
+ window.history.replaceState = function(...args) {
626
+ originalReplaceState(...args);
627
+ };
628
+ window.addEventListener("popstate", trackPageView);
629
+ this.spaListeners.push(() => {
630
+ window.history.pushState = originalPushState;
631
+ window.history.replaceState = originalReplaceState;
632
+ window.removeEventListener("popstate", trackPageView);
633
+ });
634
+ }
635
+ /** Disable SPA tracking and clean up event listeners. */
636
+ disableSPA() {
637
+ this.spaListeners.forEach((cleanup) => cleanup());
638
+ this.spaListeners.length = 0;
639
+ this.spaEnabled = false;
640
+ }
601
641
  async track({ event, properties, timestamp, context }) {
602
642
  const analyticsId = this.resolveAnalyticsId();
603
643
  if (!analyticsId) {
@@ -722,6 +762,7 @@ var Mytart = class {
722
762
  }
723
763
  return true;
724
764
  });
765
+ this.enableSPA();
725
766
  }
726
767
  async track(options) {
727
768
  const enriched = {
@@ -755,6 +796,27 @@ var Mytart = class {
755
796
  getProviders() {
756
797
  return this.providers.map((p) => p.name);
757
798
  }
799
+ /**
800
+ * Enable automatic SPA page view tracking for all supported providers.
801
+ * Should be called once in your app's entry point after initialization.
802
+ */
803
+ enableSPA() {
804
+ for (const provider of this.providers) {
805
+ if (provider.name === "vercel-analytics") {
806
+ provider.enableSPA();
807
+ }
808
+ }
809
+ }
810
+ /**
811
+ * Disable SPA page view tracking and clean up event listeners.
812
+ */
813
+ disableSPA() {
814
+ for (const provider of this.providers) {
815
+ if (provider.name === "vercel-analytics") {
816
+ provider.disableSPA();
817
+ }
818
+ }
819
+ }
758
820
  };
759
821
  export {
760
822
  AmplitudeProvider,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mytart",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "Multi-Yield Tracking & Analytics Relay Tool — framework-agnostic analytics for any project",
5
5
  "keywords": [
6
6
  "analytics",