mytart 0.1.2 â 0.1.4
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 +23 -3
- package/dist/index.d.mts +23 -1
- package/dist/index.d.ts +23 -1
- package/dist/index.js +69 -4
- package/dist/index.mjs +69 -4
- package/package.json +1 -1
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
|
|
93
|
+
{ provider: 'vercel-analytics', analyticsId?: string, apiUrl?: string }
|
|
93
94
|
```
|
|
94
95
|
|
|
95
|
-
`analyticsId` is
|
|
96
|
-
|
|
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,24 @@ 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
|
+
/**
|
|
222
|
+
* Check if Vercel analytics can be used.
|
|
223
|
+
* In browsers, checks for the injected global. In Node.js, also checks process.env.VERCEL.
|
|
224
|
+
*/
|
|
211
225
|
isAvailable(): boolean;
|
|
212
226
|
/** Get the analytics ID, attempting to resolve from config or injected global. */
|
|
213
227
|
private resolveAnalyticsId;
|
|
228
|
+
/**
|
|
229
|
+
* Enable automatic SPA page view tracking by listening to browser history changes.
|
|
230
|
+
* Call this once after initialization in your app's entry point.
|
|
231
|
+
* Uses `window.location.href` for URL and `document.referrer` for referrer.
|
|
232
|
+
*/
|
|
233
|
+
enableSPA(): void;
|
|
234
|
+
/** Disable SPA tracking and clean up event listeners. */
|
|
235
|
+
disableSPA(): void;
|
|
214
236
|
track({ event, properties, timestamp, context }: TrackOptions): Promise<TrackResult>;
|
|
215
237
|
/**
|
|
216
238
|
* 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,24 @@ 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
|
+
/**
|
|
222
|
+
* Check if Vercel analytics can be used.
|
|
223
|
+
* In browsers, checks for the injected global. In Node.js, also checks process.env.VERCEL.
|
|
224
|
+
*/
|
|
211
225
|
isAvailable(): boolean;
|
|
212
226
|
/** Get the analytics ID, attempting to resolve from config or injected global. */
|
|
213
227
|
private resolveAnalyticsId;
|
|
228
|
+
/**
|
|
229
|
+
* Enable automatic SPA page view tracking by listening to browser history changes.
|
|
230
|
+
* Call this once after initialization in your app's entry point.
|
|
231
|
+
* Uses `window.location.href` for URL and `document.referrer` for referrer.
|
|
232
|
+
*/
|
|
233
|
+
enableSPA(): void;
|
|
234
|
+
/** Disable SPA tracking and clean up event listeners. */
|
|
235
|
+
disableSPA(): void;
|
|
214
236
|
track({ event, properties, timestamp, context }: TrackOptions): Promise<TrackResult>;
|
|
215
237
|
/**
|
|
216
238
|
* Vercel Analytics is privacy-focused and does not support traditional user
|
package/dist/index.js
CHANGED
|
@@ -624,24 +624,67 @@ 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;
|
|
630
632
|
}
|
|
631
|
-
/**
|
|
633
|
+
/**
|
|
634
|
+
* Check if Vercel analytics can be used.
|
|
635
|
+
* In browsers, checks for the injected global. In Node.js, also checks process.env.VERCEL.
|
|
636
|
+
*/
|
|
632
637
|
isAvailable() {
|
|
633
|
-
if (!isRunningOnVercel()) {
|
|
634
|
-
return false;
|
|
635
|
-
}
|
|
636
638
|
if (this.config.analyticsId) {
|
|
637
639
|
return true;
|
|
638
640
|
}
|
|
641
|
+
if (isRunningOnVercel()) {
|
|
642
|
+
return true;
|
|
643
|
+
}
|
|
639
644
|
return typeof getInjectedAnalyticsId() === "string";
|
|
640
645
|
}
|
|
641
646
|
/** Get the analytics ID, attempting to resolve from config or injected global. */
|
|
642
647
|
resolveAnalyticsId() {
|
|
643
648
|
return this.config.analyticsId ?? getInjectedAnalyticsId();
|
|
644
649
|
}
|
|
650
|
+
/**
|
|
651
|
+
* Enable automatic SPA page view tracking by listening to browser history changes.
|
|
652
|
+
* Call this once after initialization in your app's entry point.
|
|
653
|
+
* Uses `window.location.href` for URL and `document.referrer` for referrer.
|
|
654
|
+
*/
|
|
655
|
+
enableSPA() {
|
|
656
|
+
if (typeof window === "undefined" || this.spaEnabled) {
|
|
657
|
+
return;
|
|
658
|
+
}
|
|
659
|
+
this.spaEnabled = true;
|
|
660
|
+
const trackPageView = () => {
|
|
661
|
+
const url = window.location.href;
|
|
662
|
+
const referrer = document.referrer || void 0;
|
|
663
|
+
this.page({ name: "pageview", url, referrer }).catch(() => {
|
|
664
|
+
});
|
|
665
|
+
};
|
|
666
|
+
const originalPushState = window.history.pushState.bind(window.history);
|
|
667
|
+
const originalReplaceState = window.history.replaceState.bind(window.history);
|
|
668
|
+
window.history.pushState = function(...args) {
|
|
669
|
+
originalPushState(...args);
|
|
670
|
+
trackPageView();
|
|
671
|
+
};
|
|
672
|
+
window.history.replaceState = function(...args) {
|
|
673
|
+
originalReplaceState(...args);
|
|
674
|
+
};
|
|
675
|
+
window.addEventListener("popstate", trackPageView);
|
|
676
|
+
this.spaListeners.push(() => {
|
|
677
|
+
window.history.pushState = originalPushState;
|
|
678
|
+
window.history.replaceState = originalReplaceState;
|
|
679
|
+
window.removeEventListener("popstate", trackPageView);
|
|
680
|
+
});
|
|
681
|
+
}
|
|
682
|
+
/** Disable SPA tracking and clean up event listeners. */
|
|
683
|
+
disableSPA() {
|
|
684
|
+
this.spaListeners.forEach((cleanup) => cleanup());
|
|
685
|
+
this.spaListeners.length = 0;
|
|
686
|
+
this.spaEnabled = false;
|
|
687
|
+
}
|
|
645
688
|
async track({ event, properties, timestamp, context }) {
|
|
646
689
|
const analyticsId = this.resolveAnalyticsId();
|
|
647
690
|
if (!analyticsId) {
|
|
@@ -766,6 +809,7 @@ var Mytart = class {
|
|
|
766
809
|
}
|
|
767
810
|
return true;
|
|
768
811
|
});
|
|
812
|
+
this.enableSPA();
|
|
769
813
|
}
|
|
770
814
|
async track(options) {
|
|
771
815
|
const enriched = {
|
|
@@ -799,6 +843,27 @@ var Mytart = class {
|
|
|
799
843
|
getProviders() {
|
|
800
844
|
return this.providers.map((p) => p.name);
|
|
801
845
|
}
|
|
846
|
+
/**
|
|
847
|
+
* Enable automatic SPA page view tracking for all supported providers.
|
|
848
|
+
* Should be called once in your app's entry point after initialization.
|
|
849
|
+
*/
|
|
850
|
+
enableSPA() {
|
|
851
|
+
for (const provider of this.providers) {
|
|
852
|
+
if (provider.name === "vercel-analytics") {
|
|
853
|
+
provider.enableSPA();
|
|
854
|
+
}
|
|
855
|
+
}
|
|
856
|
+
}
|
|
857
|
+
/**
|
|
858
|
+
* Disable SPA page view tracking and clean up event listeners.
|
|
859
|
+
*/
|
|
860
|
+
disableSPA() {
|
|
861
|
+
for (const provider of this.providers) {
|
|
862
|
+
if (provider.name === "vercel-analytics") {
|
|
863
|
+
provider.disableSPA();
|
|
864
|
+
}
|
|
865
|
+
}
|
|
866
|
+
}
|
|
802
867
|
};
|
|
803
868
|
// Annotate the CommonJS export names for ESM import in node:
|
|
804
869
|
0 && (module.exports = {
|
package/dist/index.mjs
CHANGED
|
@@ -580,24 +580,67 @@ 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;
|
|
586
588
|
}
|
|
587
|
-
/**
|
|
589
|
+
/**
|
|
590
|
+
* Check if Vercel analytics can be used.
|
|
591
|
+
* In browsers, checks for the injected global. In Node.js, also checks process.env.VERCEL.
|
|
592
|
+
*/
|
|
588
593
|
isAvailable() {
|
|
589
|
-
if (!isRunningOnVercel()) {
|
|
590
|
-
return false;
|
|
591
|
-
}
|
|
592
594
|
if (this.config.analyticsId) {
|
|
593
595
|
return true;
|
|
594
596
|
}
|
|
597
|
+
if (isRunningOnVercel()) {
|
|
598
|
+
return true;
|
|
599
|
+
}
|
|
595
600
|
return typeof getInjectedAnalyticsId() === "string";
|
|
596
601
|
}
|
|
597
602
|
/** Get the analytics ID, attempting to resolve from config or injected global. */
|
|
598
603
|
resolveAnalyticsId() {
|
|
599
604
|
return this.config.analyticsId ?? getInjectedAnalyticsId();
|
|
600
605
|
}
|
|
606
|
+
/**
|
|
607
|
+
* Enable automatic SPA page view tracking by listening to browser history changes.
|
|
608
|
+
* Call this once after initialization in your app's entry point.
|
|
609
|
+
* Uses `window.location.href` for URL and `document.referrer` for referrer.
|
|
610
|
+
*/
|
|
611
|
+
enableSPA() {
|
|
612
|
+
if (typeof window === "undefined" || this.spaEnabled) {
|
|
613
|
+
return;
|
|
614
|
+
}
|
|
615
|
+
this.spaEnabled = true;
|
|
616
|
+
const trackPageView = () => {
|
|
617
|
+
const url = window.location.href;
|
|
618
|
+
const referrer = document.referrer || void 0;
|
|
619
|
+
this.page({ name: "pageview", url, referrer }).catch(() => {
|
|
620
|
+
});
|
|
621
|
+
};
|
|
622
|
+
const originalPushState = window.history.pushState.bind(window.history);
|
|
623
|
+
const originalReplaceState = window.history.replaceState.bind(window.history);
|
|
624
|
+
window.history.pushState = function(...args) {
|
|
625
|
+
originalPushState(...args);
|
|
626
|
+
trackPageView();
|
|
627
|
+
};
|
|
628
|
+
window.history.replaceState = function(...args) {
|
|
629
|
+
originalReplaceState(...args);
|
|
630
|
+
};
|
|
631
|
+
window.addEventListener("popstate", trackPageView);
|
|
632
|
+
this.spaListeners.push(() => {
|
|
633
|
+
window.history.pushState = originalPushState;
|
|
634
|
+
window.history.replaceState = originalReplaceState;
|
|
635
|
+
window.removeEventListener("popstate", trackPageView);
|
|
636
|
+
});
|
|
637
|
+
}
|
|
638
|
+
/** Disable SPA tracking and clean up event listeners. */
|
|
639
|
+
disableSPA() {
|
|
640
|
+
this.spaListeners.forEach((cleanup) => cleanup());
|
|
641
|
+
this.spaListeners.length = 0;
|
|
642
|
+
this.spaEnabled = false;
|
|
643
|
+
}
|
|
601
644
|
async track({ event, properties, timestamp, context }) {
|
|
602
645
|
const analyticsId = this.resolveAnalyticsId();
|
|
603
646
|
if (!analyticsId) {
|
|
@@ -722,6 +765,7 @@ var Mytart = class {
|
|
|
722
765
|
}
|
|
723
766
|
return true;
|
|
724
767
|
});
|
|
768
|
+
this.enableSPA();
|
|
725
769
|
}
|
|
726
770
|
async track(options) {
|
|
727
771
|
const enriched = {
|
|
@@ -755,6 +799,27 @@ var Mytart = class {
|
|
|
755
799
|
getProviders() {
|
|
756
800
|
return this.providers.map((p) => p.name);
|
|
757
801
|
}
|
|
802
|
+
/**
|
|
803
|
+
* Enable automatic SPA page view tracking for all supported providers.
|
|
804
|
+
* Should be called once in your app's entry point after initialization.
|
|
805
|
+
*/
|
|
806
|
+
enableSPA() {
|
|
807
|
+
for (const provider of this.providers) {
|
|
808
|
+
if (provider.name === "vercel-analytics") {
|
|
809
|
+
provider.enableSPA();
|
|
810
|
+
}
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
/**
|
|
814
|
+
* Disable SPA page view tracking and clean up event listeners.
|
|
815
|
+
*/
|
|
816
|
+
disableSPA() {
|
|
817
|
+
for (const provider of this.providers) {
|
|
818
|
+
if (provider.name === "vercel-analytics") {
|
|
819
|
+
provider.disableSPA();
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
}
|
|
758
823
|
};
|
|
759
824
|
export {
|
|
760
825
|
AmplitudeProvider,
|