mytart 0.1.4 â 0.1.6
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 +2 -33
- package/dist/index.d.mts +3 -59
- package/dist/index.d.ts +3 -59
- package/dist/index.js +3 -217
- package/dist/index.mjs +2 -215
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -6,12 +6,11 @@
|
|
|
6
6
|
|
|
7
7
|
## Features
|
|
8
8
|
|
|
9
|
-
- ð **
|
|
9
|
+
- ð **6 providers out of the box**: Google Analytics 4, Mixpanel, Segment, Amplitude, Plausible, PostHog
|
|
10
10
|
- ð **Universal**: works in Node.js, browsers, and any JS framework (Next.js, Remix, Astro, SvelteKit, etc.)
|
|
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)
|
|
15
14
|
- â
**Node.js âĨ 18**
|
|
16
15
|
|
|
17
16
|
## Installation
|
|
@@ -87,28 +86,6 @@ await analytics.page({ url: 'https://example.com/pricing', name: 'Pricing' });
|
|
|
87
86
|
{ provider: 'posthog', apiKey: 'phc_YOUR_KEY', apiUrl?: string }
|
|
88
87
|
```
|
|
89
88
|
|
|
90
|
-
### Vercel Analytics
|
|
91
|
-
|
|
92
|
-
```typescript
|
|
93
|
-
{ provider: 'vercel-analytics', analyticsId?: string, apiUrl?: string }
|
|
94
|
-
```
|
|
95
|
-
|
|
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.
|
|
109
|
-
|
|
110
|
-
> **Note**: Vercel Analytics does not support `identify()` â it returns an error result (privacy-first by design).
|
|
111
|
-
|
|
112
89
|
## API Reference
|
|
113
90
|
|
|
114
91
|
### `new Mytart(config: MytartConfig)`
|
|
@@ -183,14 +160,6 @@ Remove a provider by name.
|
|
|
183
160
|
|
|
184
161
|
Returns the list of active provider names.
|
|
185
162
|
|
|
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
|
-
|
|
194
163
|
### `TrackResult`
|
|
195
164
|
|
|
196
165
|
Every method returns `Promise<TrackResult[]>` â one result per provider:
|
|
@@ -220,7 +189,7 @@ import type {
|
|
|
220
189
|
MytartConfig, BaseProviderConfig, ProviderConfig, TrackOptions, IdentifyOptions, PageOptions,
|
|
221
190
|
TrackResult, MytartError, EventContext, ProviderName,
|
|
222
191
|
GoogleAnalyticsConfig, MixpanelConfig, SegmentConfig,
|
|
223
|
-
AmplitudeConfig, PlausibleConfig, PostHogConfig,
|
|
192
|
+
AmplitudeConfig, PlausibleConfig, PostHogConfig,
|
|
224
193
|
} from 'mytart';
|
|
225
194
|
```
|
|
226
195
|
|
package/dist/index.d.mts
CHANGED
|
@@ -8,7 +8,7 @@ interface BaseProviderConfig {
|
|
|
8
8
|
/** Whether this provider is active. Defaults to `false` when omitted. */
|
|
9
9
|
enabled?: boolean;
|
|
10
10
|
}
|
|
11
|
-
type ProviderName = 'google-analytics' | 'mixpanel' | 'segment' | 'amplitude' | 'plausible' | 'posthog'
|
|
11
|
+
type ProviderName = 'google-analytics' | 'mixpanel' | 'segment' | 'amplitude' | 'plausible' | 'posthog';
|
|
12
12
|
interface GoogleAnalyticsConfig extends BaseProviderConfig {
|
|
13
13
|
provider: 'google-analytics';
|
|
14
14
|
measurementId: string;
|
|
@@ -43,22 +43,7 @@ interface PostHogConfig extends BaseProviderConfig {
|
|
|
43
43
|
apiKey: string;
|
|
44
44
|
apiUrl?: string;
|
|
45
45
|
}
|
|
46
|
-
|
|
47
|
-
provider: 'vercel-analytics';
|
|
48
|
-
/**
|
|
49
|
-
* Your Vercel Analytics ID. If not provided, it will be automatically
|
|
50
|
-
* retrieved from `window.__VERCEL_ANALYTICS_ID__` when deployed on Vercel.
|
|
51
|
-
* Set `VERCEL_ANALYTICS_ID` in your Vercel project or provide it here.
|
|
52
|
-
*/
|
|
53
|
-
analyticsId?: string;
|
|
54
|
-
/**
|
|
55
|
-
* Override the default Vercel vitals ingestion endpoint.
|
|
56
|
-
* Defaults to `https://vitals.vercel-insights.com/v1/vitals`.
|
|
57
|
-
* On a Vercel deployment you may use `/_vercel/insights/event` instead.
|
|
58
|
-
*/
|
|
59
|
-
apiUrl?: string;
|
|
60
|
-
}
|
|
61
|
-
type ProviderConfig = GoogleAnalyticsConfig | MixpanelConfig | SegmentConfig | AmplitudeConfig | PlausibleConfig | PostHogConfig | VercelAnalyticsConfig;
|
|
46
|
+
type ProviderConfig = GoogleAnalyticsConfig | MixpanelConfig | SegmentConfig | AmplitudeConfig | PlausibleConfig | PostHogConfig;
|
|
62
47
|
interface MytartConfig {
|
|
63
48
|
providers: ProviderConfig[];
|
|
64
49
|
defaultUserId?: string;
|
|
@@ -122,15 +107,6 @@ declare class Mytart {
|
|
|
122
107
|
addProvider(config: ProviderConfig): void;
|
|
123
108
|
removeProvider(name: string): void;
|
|
124
109
|
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;
|
|
134
110
|
}
|
|
135
111
|
|
|
136
112
|
declare abstract class BaseProvider {
|
|
@@ -210,36 +186,4 @@ declare class PostHogProvider extends BaseProvider {
|
|
|
210
186
|
page({ name, url, userId, anonymousId, properties }: PageOptions): Promise<TrackResult>;
|
|
211
187
|
}
|
|
212
188
|
|
|
213
|
-
|
|
214
|
-
readonly name = "vercel-analytics";
|
|
215
|
-
private readonly config;
|
|
216
|
-
private readonly http;
|
|
217
|
-
private readonly endpoint;
|
|
218
|
-
private readonly spaListeners;
|
|
219
|
-
private spaEnabled;
|
|
220
|
-
constructor(config: VercelAnalyticsConfig);
|
|
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
|
-
*/
|
|
225
|
-
isAvailable(): boolean;
|
|
226
|
-
/** Get the analytics ID, attempting to resolve from config or injected global. */
|
|
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;
|
|
236
|
-
track({ event, properties, timestamp, context }: TrackOptions): Promise<TrackResult>;
|
|
237
|
-
/**
|
|
238
|
-
* Vercel Analytics is privacy-focused and does not support traditional user
|
|
239
|
-
* identification. This method returns an unsupported error.
|
|
240
|
-
*/
|
|
241
|
-
identify(_options: IdentifyOptions): Promise<TrackResult>;
|
|
242
|
-
page({ name, url, referrer, properties, timestamp }: PageOptions): Promise<TrackResult>;
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
export { type AmplitudeConfig, AmplitudeProvider, BaseProvider, type BaseProviderConfig, type EventContext, type GoogleAnalyticsConfig, GoogleAnalyticsProvider, type IdentifyOptions, type MixpanelConfig, MixpanelProvider, Mytart, type MytartConfig, type MytartError, type PageOptions, type PlausibleConfig, PlausibleProvider, type PostHogConfig, PostHogProvider, type ProviderConfig, type ProviderName, type SegmentConfig, SegmentProvider, type TrackOptions, type TrackResult, type VercelAnalyticsConfig, VercelAnalyticsProvider };
|
|
189
|
+
export { type AmplitudeConfig, AmplitudeProvider, BaseProvider, type BaseProviderConfig, type EventContext, type GoogleAnalyticsConfig, GoogleAnalyticsProvider, type IdentifyOptions, type MixpanelConfig, MixpanelProvider, Mytart, type MytartConfig, type MytartError, type PageOptions, type PlausibleConfig, PlausibleProvider, type PostHogConfig, PostHogProvider, type ProviderConfig, type ProviderName, type SegmentConfig, SegmentProvider, type TrackOptions, type TrackResult };
|
package/dist/index.d.ts
CHANGED
|
@@ -8,7 +8,7 @@ interface BaseProviderConfig {
|
|
|
8
8
|
/** Whether this provider is active. Defaults to `false` when omitted. */
|
|
9
9
|
enabled?: boolean;
|
|
10
10
|
}
|
|
11
|
-
type ProviderName = 'google-analytics' | 'mixpanel' | 'segment' | 'amplitude' | 'plausible' | 'posthog'
|
|
11
|
+
type ProviderName = 'google-analytics' | 'mixpanel' | 'segment' | 'amplitude' | 'plausible' | 'posthog';
|
|
12
12
|
interface GoogleAnalyticsConfig extends BaseProviderConfig {
|
|
13
13
|
provider: 'google-analytics';
|
|
14
14
|
measurementId: string;
|
|
@@ -43,22 +43,7 @@ interface PostHogConfig extends BaseProviderConfig {
|
|
|
43
43
|
apiKey: string;
|
|
44
44
|
apiUrl?: string;
|
|
45
45
|
}
|
|
46
|
-
|
|
47
|
-
provider: 'vercel-analytics';
|
|
48
|
-
/**
|
|
49
|
-
* Your Vercel Analytics ID. If not provided, it will be automatically
|
|
50
|
-
* retrieved from `window.__VERCEL_ANALYTICS_ID__` when deployed on Vercel.
|
|
51
|
-
* Set `VERCEL_ANALYTICS_ID` in your Vercel project or provide it here.
|
|
52
|
-
*/
|
|
53
|
-
analyticsId?: string;
|
|
54
|
-
/**
|
|
55
|
-
* Override the default Vercel vitals ingestion endpoint.
|
|
56
|
-
* Defaults to `https://vitals.vercel-insights.com/v1/vitals`.
|
|
57
|
-
* On a Vercel deployment you may use `/_vercel/insights/event` instead.
|
|
58
|
-
*/
|
|
59
|
-
apiUrl?: string;
|
|
60
|
-
}
|
|
61
|
-
type ProviderConfig = GoogleAnalyticsConfig | MixpanelConfig | SegmentConfig | AmplitudeConfig | PlausibleConfig | PostHogConfig | VercelAnalyticsConfig;
|
|
46
|
+
type ProviderConfig = GoogleAnalyticsConfig | MixpanelConfig | SegmentConfig | AmplitudeConfig | PlausibleConfig | PostHogConfig;
|
|
62
47
|
interface MytartConfig {
|
|
63
48
|
providers: ProviderConfig[];
|
|
64
49
|
defaultUserId?: string;
|
|
@@ -122,15 +107,6 @@ declare class Mytart {
|
|
|
122
107
|
addProvider(config: ProviderConfig): void;
|
|
123
108
|
removeProvider(name: string): void;
|
|
124
109
|
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;
|
|
134
110
|
}
|
|
135
111
|
|
|
136
112
|
declare abstract class BaseProvider {
|
|
@@ -210,36 +186,4 @@ declare class PostHogProvider extends BaseProvider {
|
|
|
210
186
|
page({ name, url, userId, anonymousId, properties }: PageOptions): Promise<TrackResult>;
|
|
211
187
|
}
|
|
212
188
|
|
|
213
|
-
|
|
214
|
-
readonly name = "vercel-analytics";
|
|
215
|
-
private readonly config;
|
|
216
|
-
private readonly http;
|
|
217
|
-
private readonly endpoint;
|
|
218
|
-
private readonly spaListeners;
|
|
219
|
-
private spaEnabled;
|
|
220
|
-
constructor(config: VercelAnalyticsConfig);
|
|
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
|
-
*/
|
|
225
|
-
isAvailable(): boolean;
|
|
226
|
-
/** Get the analytics ID, attempting to resolve from config or injected global. */
|
|
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;
|
|
236
|
-
track({ event, properties, timestamp, context }: TrackOptions): Promise<TrackResult>;
|
|
237
|
-
/**
|
|
238
|
-
* Vercel Analytics is privacy-focused and does not support traditional user
|
|
239
|
-
* identification. This method returns an unsupported error.
|
|
240
|
-
*/
|
|
241
|
-
identify(_options: IdentifyOptions): Promise<TrackResult>;
|
|
242
|
-
page({ name, url, referrer, properties, timestamp }: PageOptions): Promise<TrackResult>;
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
export { type AmplitudeConfig, AmplitudeProvider, BaseProvider, type BaseProviderConfig, type EventContext, type GoogleAnalyticsConfig, GoogleAnalyticsProvider, type IdentifyOptions, type MixpanelConfig, MixpanelProvider, Mytart, type MytartConfig, type MytartError, type PageOptions, type PlausibleConfig, PlausibleProvider, type PostHogConfig, PostHogProvider, type ProviderConfig, type ProviderName, type SegmentConfig, SegmentProvider, type TrackOptions, type TrackResult, type VercelAnalyticsConfig, VercelAnalyticsProvider };
|
|
189
|
+
export { type AmplitudeConfig, AmplitudeProvider, BaseProvider, type BaseProviderConfig, type EventContext, type GoogleAnalyticsConfig, GoogleAnalyticsProvider, type IdentifyOptions, type MixpanelConfig, MixpanelProvider, Mytart, type MytartConfig, type MytartError, type PageOptions, type PlausibleConfig, PlausibleProvider, type PostHogConfig, PostHogProvider, type ProviderConfig, type ProviderName, type SegmentConfig, SegmentProvider, type TrackOptions, type TrackResult };
|
package/dist/index.js
CHANGED
|
@@ -37,8 +37,7 @@ __export(index_exports, {
|
|
|
37
37
|
Mytart: () => Mytart,
|
|
38
38
|
PlausibleProvider: () => PlausibleProvider,
|
|
39
39
|
PostHogProvider: () => PostHogProvider,
|
|
40
|
-
SegmentProvider: () => SegmentProvider
|
|
41
|
-
VercelAnalyticsProvider: () => VercelAnalyticsProvider
|
|
40
|
+
SegmentProvider: () => SegmentProvider
|
|
42
41
|
});
|
|
43
42
|
module.exports = __toCommonJS(index_exports);
|
|
44
43
|
|
|
@@ -594,189 +593,6 @@ var PostHogProvider = class extends BaseProvider {
|
|
|
594
593
|
}
|
|
595
594
|
};
|
|
596
595
|
|
|
597
|
-
// src/providers/vercel-analytics.ts
|
|
598
|
-
var VERCEL_ENDPOINT = "https://vitals.vercel-insights.com/v1/vitals";
|
|
599
|
-
function isRunningOnVercel() {
|
|
600
|
-
return typeof process !== "undefined" && process.env?.VERCEL === "1";
|
|
601
|
-
}
|
|
602
|
-
function getInjectedAnalyticsId() {
|
|
603
|
-
if (typeof window !== "undefined") {
|
|
604
|
-
const w = window;
|
|
605
|
-
return w.__VERCEL_ANALYTICS_ID__;
|
|
606
|
-
}
|
|
607
|
-
return void 0;
|
|
608
|
-
}
|
|
609
|
-
function generateId() {
|
|
610
|
-
return `${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 9)}`;
|
|
611
|
-
}
|
|
612
|
-
function flattenProps(props) {
|
|
613
|
-
const result = {};
|
|
614
|
-
for (const [key, value] of Object.entries(props)) {
|
|
615
|
-
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean" || value === null) {
|
|
616
|
-
result[key] = value;
|
|
617
|
-
} else {
|
|
618
|
-
result[key] = String(value);
|
|
619
|
-
}
|
|
620
|
-
}
|
|
621
|
-
return result;
|
|
622
|
-
}
|
|
623
|
-
var VercelAnalyticsProvider = class extends BaseProvider {
|
|
624
|
-
constructor(config) {
|
|
625
|
-
super();
|
|
626
|
-
this.name = "vercel-analytics";
|
|
627
|
-
this.spaListeners = [];
|
|
628
|
-
this.spaEnabled = false;
|
|
629
|
-
this.config = config;
|
|
630
|
-
this.http = createHttpClient();
|
|
631
|
-
this.endpoint = config.apiUrl ?? VERCEL_ENDPOINT;
|
|
632
|
-
}
|
|
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
|
-
*/
|
|
637
|
-
isAvailable() {
|
|
638
|
-
if (this.config.analyticsId) {
|
|
639
|
-
return true;
|
|
640
|
-
}
|
|
641
|
-
if (isRunningOnVercel()) {
|
|
642
|
-
return true;
|
|
643
|
-
}
|
|
644
|
-
return typeof getInjectedAnalyticsId() === "string";
|
|
645
|
-
}
|
|
646
|
-
/** Get the analytics ID, attempting to resolve from config or injected global. */
|
|
647
|
-
resolveAnalyticsId() {
|
|
648
|
-
return this.config.analyticsId ?? getInjectedAnalyticsId();
|
|
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
|
-
}
|
|
688
|
-
async track({ event, properties, timestamp, context }) {
|
|
689
|
-
const analyticsId = this.resolveAnalyticsId();
|
|
690
|
-
if (!analyticsId) {
|
|
691
|
-
return this.buildError(
|
|
692
|
-
"Vercel Analytics ID not found. Set it in config or deploy on Vercel with Analytics enabled.",
|
|
693
|
-
"VERCEL_ANALYTICS_ID_MISSING"
|
|
694
|
-
);
|
|
695
|
-
}
|
|
696
|
-
try {
|
|
697
|
-
const url = context?.page?.url ?? "";
|
|
698
|
-
const payload = {
|
|
699
|
-
dsn: analyticsId,
|
|
700
|
-
id: generateId(),
|
|
701
|
-
type: "custom",
|
|
702
|
-
name: event,
|
|
703
|
-
url,
|
|
704
|
-
ts: timestamp ? timestamp.getTime() : Date.now()
|
|
705
|
-
};
|
|
706
|
-
if (properties && Object.keys(properties).length > 0) {
|
|
707
|
-
payload.data = flattenProps(properties);
|
|
708
|
-
}
|
|
709
|
-
const response = await this.http.post(this.endpoint, payload);
|
|
710
|
-
return this.buildSuccess(response.status);
|
|
711
|
-
} catch (error) {
|
|
712
|
-
if (isAxiosError(error)) {
|
|
713
|
-
return this.buildError(
|
|
714
|
-
error.message,
|
|
715
|
-
`VERCEL_ANALYTICS_HTTP_${error.response?.status ?? "ERROR"}`,
|
|
716
|
-
error
|
|
717
|
-
);
|
|
718
|
-
}
|
|
719
|
-
return this.buildError(String(error), "VERCEL_ANALYTICS_UNKNOWN_ERROR", error);
|
|
720
|
-
}
|
|
721
|
-
}
|
|
722
|
-
/**
|
|
723
|
-
* Vercel Analytics is privacy-focused and does not support traditional user
|
|
724
|
-
* identification. This method returns an unsupported error.
|
|
725
|
-
*/
|
|
726
|
-
async identify(_options) {
|
|
727
|
-
return {
|
|
728
|
-
provider: this.name,
|
|
729
|
-
success: false,
|
|
730
|
-
error: {
|
|
731
|
-
message: "Vercel Analytics does not support user identification",
|
|
732
|
-
code: "VERCEL_ANALYTICS_NOT_SUPPORTED",
|
|
733
|
-
provider: this.name
|
|
734
|
-
}
|
|
735
|
-
};
|
|
736
|
-
}
|
|
737
|
-
async page({ name, url, referrer, properties, timestamp }) {
|
|
738
|
-
const analyticsId = this.resolveAnalyticsId();
|
|
739
|
-
if (!analyticsId) {
|
|
740
|
-
return this.buildError(
|
|
741
|
-
"Vercel Analytics ID not found. Set it in config or deploy on Vercel with Analytics enabled.",
|
|
742
|
-
"VERCEL_ANALYTICS_ID_MISSING"
|
|
743
|
-
);
|
|
744
|
-
}
|
|
745
|
-
try {
|
|
746
|
-
const payload = {
|
|
747
|
-
dsn: analyticsId,
|
|
748
|
-
id: generateId(),
|
|
749
|
-
type: "pageview",
|
|
750
|
-
name: name ?? "pageview",
|
|
751
|
-
url,
|
|
752
|
-
ts: timestamp ? timestamp.getTime() : Date.now()
|
|
753
|
-
};
|
|
754
|
-
if (referrer) {
|
|
755
|
-
payload.referrer = referrer;
|
|
756
|
-
}
|
|
757
|
-
if (properties && Object.keys(properties).length > 0) {
|
|
758
|
-
return this.track({
|
|
759
|
-
event: name ?? "pageview",
|
|
760
|
-
properties,
|
|
761
|
-
context: { page: { url, referrer } },
|
|
762
|
-
timestamp
|
|
763
|
-
});
|
|
764
|
-
}
|
|
765
|
-
const response = await this.http.post(this.endpoint, payload);
|
|
766
|
-
return this.buildSuccess(response.status);
|
|
767
|
-
} catch (error) {
|
|
768
|
-
if (isAxiosError(error)) {
|
|
769
|
-
return this.buildError(
|
|
770
|
-
error.message,
|
|
771
|
-
`VERCEL_ANALYTICS_HTTP_${error.response?.status ?? "ERROR"}`,
|
|
772
|
-
error
|
|
773
|
-
);
|
|
774
|
-
}
|
|
775
|
-
return this.buildError(String(error), "VERCEL_ANALYTICS_UNKNOWN_ERROR", error);
|
|
776
|
-
}
|
|
777
|
-
}
|
|
778
|
-
};
|
|
779
|
-
|
|
780
596
|
// src/mytart.ts
|
|
781
597
|
function createProvider(config) {
|
|
782
598
|
switch (config.provider) {
|
|
@@ -792,8 +608,6 @@ function createProvider(config) {
|
|
|
792
608
|
return new PlausibleProvider(config);
|
|
793
609
|
case "posthog":
|
|
794
610
|
return new PostHogProvider(config);
|
|
795
|
-
case "vercel-analytics":
|
|
796
|
-
return new VercelAnalyticsProvider(config);
|
|
797
611
|
default: {
|
|
798
612
|
const exhaustive = config;
|
|
799
613
|
throw new Error(`Unknown provider: ${exhaustive.provider}`);
|
|
@@ -803,13 +617,7 @@ function createProvider(config) {
|
|
|
803
617
|
var Mytart = class {
|
|
804
618
|
constructor(config) {
|
|
805
619
|
this.config = config;
|
|
806
|
-
this.providers = config.providers.filter((c) => c.enabled === true).map(createProvider)
|
|
807
|
-
if (p.name === "vercel-analytics") {
|
|
808
|
-
return p.isAvailable();
|
|
809
|
-
}
|
|
810
|
-
return true;
|
|
811
|
-
});
|
|
812
|
-
this.enableSPA();
|
|
620
|
+
this.providers = config.providers.filter((c) => c.enabled === true).map(createProvider);
|
|
813
621
|
}
|
|
814
622
|
async track(options) {
|
|
815
623
|
const enriched = {
|
|
@@ -843,27 +651,6 @@ var Mytart = class {
|
|
|
843
651
|
getProviders() {
|
|
844
652
|
return this.providers.map((p) => p.name);
|
|
845
653
|
}
|
|
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
|
-
}
|
|
867
654
|
};
|
|
868
655
|
// Annotate the CommonJS export names for ESM import in node:
|
|
869
656
|
0 && (module.exports = {
|
|
@@ -874,6 +661,5 @@ var Mytart = class {
|
|
|
874
661
|
Mytart,
|
|
875
662
|
PlausibleProvider,
|
|
876
663
|
PostHogProvider,
|
|
877
|
-
SegmentProvider
|
|
878
|
-
VercelAnalyticsProvider
|
|
664
|
+
SegmentProvider
|
|
879
665
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -550,189 +550,6 @@ var PostHogProvider = class extends BaseProvider {
|
|
|
550
550
|
}
|
|
551
551
|
};
|
|
552
552
|
|
|
553
|
-
// src/providers/vercel-analytics.ts
|
|
554
|
-
var VERCEL_ENDPOINT = "https://vitals.vercel-insights.com/v1/vitals";
|
|
555
|
-
function isRunningOnVercel() {
|
|
556
|
-
return typeof process !== "undefined" && process.env?.VERCEL === "1";
|
|
557
|
-
}
|
|
558
|
-
function getInjectedAnalyticsId() {
|
|
559
|
-
if (typeof window !== "undefined") {
|
|
560
|
-
const w = window;
|
|
561
|
-
return w.__VERCEL_ANALYTICS_ID__;
|
|
562
|
-
}
|
|
563
|
-
return void 0;
|
|
564
|
-
}
|
|
565
|
-
function generateId() {
|
|
566
|
-
return `${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 9)}`;
|
|
567
|
-
}
|
|
568
|
-
function flattenProps(props) {
|
|
569
|
-
const result = {};
|
|
570
|
-
for (const [key, value] of Object.entries(props)) {
|
|
571
|
-
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean" || value === null) {
|
|
572
|
-
result[key] = value;
|
|
573
|
-
} else {
|
|
574
|
-
result[key] = String(value);
|
|
575
|
-
}
|
|
576
|
-
}
|
|
577
|
-
return result;
|
|
578
|
-
}
|
|
579
|
-
var VercelAnalyticsProvider = class extends BaseProvider {
|
|
580
|
-
constructor(config) {
|
|
581
|
-
super();
|
|
582
|
-
this.name = "vercel-analytics";
|
|
583
|
-
this.spaListeners = [];
|
|
584
|
-
this.spaEnabled = false;
|
|
585
|
-
this.config = config;
|
|
586
|
-
this.http = createHttpClient();
|
|
587
|
-
this.endpoint = config.apiUrl ?? VERCEL_ENDPOINT;
|
|
588
|
-
}
|
|
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
|
-
*/
|
|
593
|
-
isAvailable() {
|
|
594
|
-
if (this.config.analyticsId) {
|
|
595
|
-
return true;
|
|
596
|
-
}
|
|
597
|
-
if (isRunningOnVercel()) {
|
|
598
|
-
return true;
|
|
599
|
-
}
|
|
600
|
-
return typeof getInjectedAnalyticsId() === "string";
|
|
601
|
-
}
|
|
602
|
-
/** Get the analytics ID, attempting to resolve from config or injected global. */
|
|
603
|
-
resolveAnalyticsId() {
|
|
604
|
-
return this.config.analyticsId ?? getInjectedAnalyticsId();
|
|
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
|
-
}
|
|
644
|
-
async track({ event, properties, timestamp, context }) {
|
|
645
|
-
const analyticsId = this.resolveAnalyticsId();
|
|
646
|
-
if (!analyticsId) {
|
|
647
|
-
return this.buildError(
|
|
648
|
-
"Vercel Analytics ID not found. Set it in config or deploy on Vercel with Analytics enabled.",
|
|
649
|
-
"VERCEL_ANALYTICS_ID_MISSING"
|
|
650
|
-
);
|
|
651
|
-
}
|
|
652
|
-
try {
|
|
653
|
-
const url = context?.page?.url ?? "";
|
|
654
|
-
const payload = {
|
|
655
|
-
dsn: analyticsId,
|
|
656
|
-
id: generateId(),
|
|
657
|
-
type: "custom",
|
|
658
|
-
name: event,
|
|
659
|
-
url,
|
|
660
|
-
ts: timestamp ? timestamp.getTime() : Date.now()
|
|
661
|
-
};
|
|
662
|
-
if (properties && Object.keys(properties).length > 0) {
|
|
663
|
-
payload.data = flattenProps(properties);
|
|
664
|
-
}
|
|
665
|
-
const response = await this.http.post(this.endpoint, payload);
|
|
666
|
-
return this.buildSuccess(response.status);
|
|
667
|
-
} catch (error) {
|
|
668
|
-
if (isAxiosError(error)) {
|
|
669
|
-
return this.buildError(
|
|
670
|
-
error.message,
|
|
671
|
-
`VERCEL_ANALYTICS_HTTP_${error.response?.status ?? "ERROR"}`,
|
|
672
|
-
error
|
|
673
|
-
);
|
|
674
|
-
}
|
|
675
|
-
return this.buildError(String(error), "VERCEL_ANALYTICS_UNKNOWN_ERROR", error);
|
|
676
|
-
}
|
|
677
|
-
}
|
|
678
|
-
/**
|
|
679
|
-
* Vercel Analytics is privacy-focused and does not support traditional user
|
|
680
|
-
* identification. This method returns an unsupported error.
|
|
681
|
-
*/
|
|
682
|
-
async identify(_options) {
|
|
683
|
-
return {
|
|
684
|
-
provider: this.name,
|
|
685
|
-
success: false,
|
|
686
|
-
error: {
|
|
687
|
-
message: "Vercel Analytics does not support user identification",
|
|
688
|
-
code: "VERCEL_ANALYTICS_NOT_SUPPORTED",
|
|
689
|
-
provider: this.name
|
|
690
|
-
}
|
|
691
|
-
};
|
|
692
|
-
}
|
|
693
|
-
async page({ name, url, referrer, properties, timestamp }) {
|
|
694
|
-
const analyticsId = this.resolveAnalyticsId();
|
|
695
|
-
if (!analyticsId) {
|
|
696
|
-
return this.buildError(
|
|
697
|
-
"Vercel Analytics ID not found. Set it in config or deploy on Vercel with Analytics enabled.",
|
|
698
|
-
"VERCEL_ANALYTICS_ID_MISSING"
|
|
699
|
-
);
|
|
700
|
-
}
|
|
701
|
-
try {
|
|
702
|
-
const payload = {
|
|
703
|
-
dsn: analyticsId,
|
|
704
|
-
id: generateId(),
|
|
705
|
-
type: "pageview",
|
|
706
|
-
name: name ?? "pageview",
|
|
707
|
-
url,
|
|
708
|
-
ts: timestamp ? timestamp.getTime() : Date.now()
|
|
709
|
-
};
|
|
710
|
-
if (referrer) {
|
|
711
|
-
payload.referrer = referrer;
|
|
712
|
-
}
|
|
713
|
-
if (properties && Object.keys(properties).length > 0) {
|
|
714
|
-
return this.track({
|
|
715
|
-
event: name ?? "pageview",
|
|
716
|
-
properties,
|
|
717
|
-
context: { page: { url, referrer } },
|
|
718
|
-
timestamp
|
|
719
|
-
});
|
|
720
|
-
}
|
|
721
|
-
const response = await this.http.post(this.endpoint, payload);
|
|
722
|
-
return this.buildSuccess(response.status);
|
|
723
|
-
} catch (error) {
|
|
724
|
-
if (isAxiosError(error)) {
|
|
725
|
-
return this.buildError(
|
|
726
|
-
error.message,
|
|
727
|
-
`VERCEL_ANALYTICS_HTTP_${error.response?.status ?? "ERROR"}`,
|
|
728
|
-
error
|
|
729
|
-
);
|
|
730
|
-
}
|
|
731
|
-
return this.buildError(String(error), "VERCEL_ANALYTICS_UNKNOWN_ERROR", error);
|
|
732
|
-
}
|
|
733
|
-
}
|
|
734
|
-
};
|
|
735
|
-
|
|
736
553
|
// src/mytart.ts
|
|
737
554
|
function createProvider(config) {
|
|
738
555
|
switch (config.provider) {
|
|
@@ -748,8 +565,6 @@ function createProvider(config) {
|
|
|
748
565
|
return new PlausibleProvider(config);
|
|
749
566
|
case "posthog":
|
|
750
567
|
return new PostHogProvider(config);
|
|
751
|
-
case "vercel-analytics":
|
|
752
|
-
return new VercelAnalyticsProvider(config);
|
|
753
568
|
default: {
|
|
754
569
|
const exhaustive = config;
|
|
755
570
|
throw new Error(`Unknown provider: ${exhaustive.provider}`);
|
|
@@ -759,13 +574,7 @@ function createProvider(config) {
|
|
|
759
574
|
var Mytart = class {
|
|
760
575
|
constructor(config) {
|
|
761
576
|
this.config = config;
|
|
762
|
-
this.providers = config.providers.filter((c) => c.enabled === true).map(createProvider)
|
|
763
|
-
if (p.name === "vercel-analytics") {
|
|
764
|
-
return p.isAvailable();
|
|
765
|
-
}
|
|
766
|
-
return true;
|
|
767
|
-
});
|
|
768
|
-
this.enableSPA();
|
|
577
|
+
this.providers = config.providers.filter((c) => c.enabled === true).map(createProvider);
|
|
769
578
|
}
|
|
770
579
|
async track(options) {
|
|
771
580
|
const enriched = {
|
|
@@ -799,27 +608,6 @@ var Mytart = class {
|
|
|
799
608
|
getProviders() {
|
|
800
609
|
return this.providers.map((p) => p.name);
|
|
801
610
|
}
|
|
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
|
-
}
|
|
823
611
|
};
|
|
824
612
|
export {
|
|
825
613
|
AmplitudeProvider,
|
|
@@ -829,6 +617,5 @@ export {
|
|
|
829
617
|
Mytart,
|
|
830
618
|
PlausibleProvider,
|
|
831
619
|
PostHogProvider,
|
|
832
|
-
SegmentProvider
|
|
833
|
-
VercelAnalyticsProvider
|
|
620
|
+
SegmentProvider
|
|
834
621
|
};
|