mytart 0.1.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 +234 -0
- package/dist/index.d.mts +218 -0
- package/dist/index.d.ts +218 -0
- package/dist/index.js +771 -0
- package/dist/index.mjs +726 -0
- package/package.json +55 -0
package/README.md
ADDED
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
# mytart
|
|
2
|
+
|
|
3
|
+
**Multi-Yield Tracking & Analytics Relay Tool** â framework-agnostic analytics for any ESM JavaScript/TypeScript project.
|
|
4
|
+
|
|
5
|
+
`mytart` makes direct HTTP calls to analytics provider endpoints via [axios](https://axios-http.com/) â no bloated SDK wrappers, no global state, no lock-in.
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- ð **7 providers out of the box**: Google Analytics 4, Mixpanel, Segment, Amplitude, Plausible, PostHog, Vercel Analytics
|
|
10
|
+
- ð **Universal**: works in Node.js, browsers, and any JS framework (Next.js, Remix, Astro, SvelteKit, etc.)
|
|
11
|
+
- ð· **TypeScript-first**: precise typings, object-parameter style for great DX
|
|
12
|
+
- ðĶ **Dual ESM/CJS output**: works with `import` and `require`
|
|
13
|
+
- ðŠķ **Lightweight**: direct HTTP via axios, no SDK overhead
|
|
14
|
+
- â
**Node.js âĨ 18**
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install mytart
|
|
20
|
+
# or
|
|
21
|
+
pnpm add mytart
|
|
22
|
+
# or
|
|
23
|
+
yarn add mytart
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Quick Start
|
|
27
|
+
|
|
28
|
+
```typescript
|
|
29
|
+
import { Mytart } from 'mytart';
|
|
30
|
+
|
|
31
|
+
const analytics = new Mytart({
|
|
32
|
+
providers: [
|
|
33
|
+
{ provider: 'segment', writeKey: 'YOUR_WRITE_KEY' },
|
|
34
|
+
{ provider: 'posthog', apiKey: 'phc_YOUR_KEY' },
|
|
35
|
+
],
|
|
36
|
+
defaultUserId: 'user_123',
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
// Track an event
|
|
40
|
+
await analytics.track({ event: 'signup', properties: { plan: 'pro' } });
|
|
41
|
+
|
|
42
|
+
// Identify a user
|
|
43
|
+
await analytics.identify({ userId: 'user_123', traits: { name: 'Alice', email: 'alice@example.com' } });
|
|
44
|
+
|
|
45
|
+
// Track a page view
|
|
46
|
+
await analytics.page({ url: 'https://example.com/pricing', name: 'Pricing' });
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Providers
|
|
50
|
+
|
|
51
|
+
### Google Analytics 4
|
|
52
|
+
|
|
53
|
+
```typescript
|
|
54
|
+
{ provider: 'google-analytics', measurementId: 'G-XXXXXXXXXX', apiSecret: 'YOUR_SECRET', debug?: boolean }
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Mixpanel
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
60
|
+
{ provider: 'mixpanel', token: 'YOUR_TOKEN', apiUrl?: string }
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### Segment
|
|
64
|
+
|
|
65
|
+
```typescript
|
|
66
|
+
{ provider: 'segment', writeKey: 'YOUR_WRITE_KEY', apiUrl?: string }
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### Amplitude
|
|
70
|
+
|
|
71
|
+
```typescript
|
|
72
|
+
{ provider: 'amplitude', apiKey: 'YOUR_API_KEY', apiUrl?: string }
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### Plausible
|
|
76
|
+
|
|
77
|
+
```typescript
|
|
78
|
+
{ provider: 'plausible', domain: 'example.com', apiUrl?: string, userAgent?: string, xForwardedFor?: string }
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
> **Note**: Plausible does not support `identify()` â it returns an error result.
|
|
82
|
+
|
|
83
|
+
### PostHog
|
|
84
|
+
|
|
85
|
+
```typescript
|
|
86
|
+
{ provider: 'posthog', apiKey: 'phc_YOUR_KEY', apiUrl?: string }
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### Vercel Analytics
|
|
90
|
+
|
|
91
|
+
```typescript
|
|
92
|
+
{ provider: 'vercel-analytics', analyticsId: 'YOUR_ANALYTICS_ID', apiUrl?: string }
|
|
93
|
+
```
|
|
94
|
+
|
|
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`.
|
|
97
|
+
|
|
98
|
+
> **Note**: Vercel Analytics does not support `identify()` â it returns an error result (privacy-first by design).
|
|
99
|
+
|
|
100
|
+
## API Reference
|
|
101
|
+
|
|
102
|
+
### `new Mytart(config: MytartConfig)`
|
|
103
|
+
|
|
104
|
+
```typescript
|
|
105
|
+
interface MytartConfig {
|
|
106
|
+
providers: ProviderConfig[];
|
|
107
|
+
defaultUserId?: string; // applied to every track/page call if no userId given
|
|
108
|
+
defaultAnonymousId?: string; // applied to every track/page call if no anonymousId given
|
|
109
|
+
debug?: boolean;
|
|
110
|
+
}
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
Every provider config accepts an `enabled` field. Providers are **off by default** â you must set `enabled: true` to activate a provider:
|
|
114
|
+
|
|
115
|
+
```typescript
|
|
116
|
+
const analytics = new Mytart({
|
|
117
|
+
providers: [
|
|
118
|
+
{ provider: 'segment', writeKey: 'YOUR_KEY', enabled: true }, // active
|
|
119
|
+
{ provider: 'mixpanel', token: 'YOUR_TOKEN' }, // skipped (off by default)
|
|
120
|
+
],
|
|
121
|
+
});
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
### `analytics.track(options: TrackOptions): Promise<TrackResult[]>`
|
|
125
|
+
|
|
126
|
+
```typescript
|
|
127
|
+
interface TrackOptions {
|
|
128
|
+
event: string;
|
|
129
|
+
properties?: Record<string, unknown>;
|
|
130
|
+
userId?: string;
|
|
131
|
+
anonymousId?: string;
|
|
132
|
+
timestamp?: Date;
|
|
133
|
+
context?: EventContext;
|
|
134
|
+
}
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
### `analytics.identify(options: IdentifyOptions): Promise<TrackResult[]>`
|
|
138
|
+
|
|
139
|
+
```typescript
|
|
140
|
+
interface IdentifyOptions {
|
|
141
|
+
userId: string;
|
|
142
|
+
traits?: Record<string, unknown>;
|
|
143
|
+
anonymousId?: string;
|
|
144
|
+
timestamp?: Date;
|
|
145
|
+
}
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
### `analytics.page(options: PageOptions): Promise<TrackResult[]>`
|
|
149
|
+
|
|
150
|
+
```typescript
|
|
151
|
+
interface PageOptions {
|
|
152
|
+
url: string;
|
|
153
|
+
name?: string;
|
|
154
|
+
referrer?: string;
|
|
155
|
+
properties?: Record<string, unknown>;
|
|
156
|
+
userId?: string;
|
|
157
|
+
anonymousId?: string;
|
|
158
|
+
timestamp?: Date;
|
|
159
|
+
}
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
### `analytics.addProvider(config: ProviderConfig): void`
|
|
163
|
+
|
|
164
|
+
Dynamically add a provider at runtime.
|
|
165
|
+
|
|
166
|
+
### `analytics.removeProvider(name: string): void`
|
|
167
|
+
|
|
168
|
+
Remove a provider by name.
|
|
169
|
+
|
|
170
|
+
### `analytics.getProviders(): string[]`
|
|
171
|
+
|
|
172
|
+
Returns the list of active provider names.
|
|
173
|
+
|
|
174
|
+
### `TrackResult`
|
|
175
|
+
|
|
176
|
+
Every method returns `Promise<TrackResult[]>` â one result per provider:
|
|
177
|
+
|
|
178
|
+
```typescript
|
|
179
|
+
interface TrackResult {
|
|
180
|
+
provider: string;
|
|
181
|
+
success: boolean;
|
|
182
|
+
statusCode?: number;
|
|
183
|
+
error?: MytartError;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
interface MytartError {
|
|
187
|
+
message: string;
|
|
188
|
+
code: string;
|
|
189
|
+
provider: string;
|
|
190
|
+
originalError?: unknown;
|
|
191
|
+
}
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
## TypeScript
|
|
195
|
+
|
|
196
|
+
All types are exported:
|
|
197
|
+
|
|
198
|
+
```typescript
|
|
199
|
+
import type {
|
|
200
|
+
MytartConfig, BaseProviderConfig, ProviderConfig, TrackOptions, IdentifyOptions, PageOptions,
|
|
201
|
+
TrackResult, MytartError, EventContext, ProviderName,
|
|
202
|
+
GoogleAnalyticsConfig, MixpanelConfig, SegmentConfig,
|
|
203
|
+
AmplitudeConfig, PlausibleConfig, PostHogConfig, VercelAnalyticsConfig,
|
|
204
|
+
} from 'mytart';
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
## Custom Providers
|
|
208
|
+
|
|
209
|
+
Extend `BaseProvider` to create your own:
|
|
210
|
+
|
|
211
|
+
```typescript
|
|
212
|
+
import { BaseProvider, TrackOptions, IdentifyOptions, PageOptions, TrackResult } from 'mytart';
|
|
213
|
+
|
|
214
|
+
export class MyProvider extends BaseProvider {
|
|
215
|
+
readonly name = 'my-provider';
|
|
216
|
+
|
|
217
|
+
async track(options: TrackOptions): Promise<TrackResult> {
|
|
218
|
+
// your HTTP call here
|
|
219
|
+
return this.buildSuccess(200);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
async identify(options: IdentifyOptions): Promise<TrackResult> {
|
|
223
|
+
return this.buildSuccess(200);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
async page(options: PageOptions): Promise<TrackResult> {
|
|
227
|
+
return this.buildSuccess(200);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
## License
|
|
233
|
+
|
|
234
|
+
MIT
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Fields shared by every provider configuration block.
|
|
3
|
+
* Set `enabled: true` to activate a provider for fan-out.
|
|
4
|
+
* Providers are **disabled by default** â omitting `enabled` (or setting it to
|
|
5
|
+
* `false`) means the provider is skipped entirely.
|
|
6
|
+
*/
|
|
7
|
+
interface BaseProviderConfig {
|
|
8
|
+
/** Whether this provider is active. Defaults to `false` when omitted. */
|
|
9
|
+
enabled?: boolean;
|
|
10
|
+
}
|
|
11
|
+
type ProviderName = 'google-analytics' | 'mixpanel' | 'segment' | 'amplitude' | 'plausible' | 'posthog' | 'vercel-analytics';
|
|
12
|
+
interface GoogleAnalyticsConfig extends BaseProviderConfig {
|
|
13
|
+
provider: 'google-analytics';
|
|
14
|
+
measurementId: string;
|
|
15
|
+
apiSecret: string;
|
|
16
|
+
clientId?: string;
|
|
17
|
+
debug?: boolean;
|
|
18
|
+
}
|
|
19
|
+
interface MixpanelConfig extends BaseProviderConfig {
|
|
20
|
+
provider: 'mixpanel';
|
|
21
|
+
token: string;
|
|
22
|
+
apiUrl?: string;
|
|
23
|
+
}
|
|
24
|
+
interface SegmentConfig extends BaseProviderConfig {
|
|
25
|
+
provider: 'segment';
|
|
26
|
+
writeKey: string;
|
|
27
|
+
apiUrl?: string;
|
|
28
|
+
}
|
|
29
|
+
interface AmplitudeConfig extends BaseProviderConfig {
|
|
30
|
+
provider: 'amplitude';
|
|
31
|
+
apiKey: string;
|
|
32
|
+
apiUrl?: string;
|
|
33
|
+
}
|
|
34
|
+
interface PlausibleConfig extends BaseProviderConfig {
|
|
35
|
+
provider: 'plausible';
|
|
36
|
+
domain: string;
|
|
37
|
+
apiUrl?: string;
|
|
38
|
+
userAgent?: string;
|
|
39
|
+
xForwardedFor?: string;
|
|
40
|
+
}
|
|
41
|
+
interface PostHogConfig extends BaseProviderConfig {
|
|
42
|
+
provider: 'posthog';
|
|
43
|
+
apiKey: string;
|
|
44
|
+
apiUrl?: string;
|
|
45
|
+
}
|
|
46
|
+
interface VercelAnalyticsConfig extends BaseProviderConfig {
|
|
47
|
+
provider: 'vercel-analytics';
|
|
48
|
+
/**
|
|
49
|
+
* Your Vercel Analytics ID (the `VERCEL_ANALYTICS_ID` environment variable
|
|
50
|
+
* or the measurement ID shown in your Vercel project's Analytics settings).
|
|
51
|
+
*/
|
|
52
|
+
analyticsId: string;
|
|
53
|
+
/**
|
|
54
|
+
* Override the default Vercel vitals ingestion endpoint.
|
|
55
|
+
* Defaults to `https://vitals.vercel-insights.com/v1/vitals`.
|
|
56
|
+
* On a Vercel deployment you may use `/_vercel/insights/event` instead.
|
|
57
|
+
*/
|
|
58
|
+
apiUrl?: string;
|
|
59
|
+
}
|
|
60
|
+
type ProviderConfig = GoogleAnalyticsConfig | MixpanelConfig | SegmentConfig | AmplitudeConfig | PlausibleConfig | PostHogConfig | VercelAnalyticsConfig;
|
|
61
|
+
interface MytartConfig {
|
|
62
|
+
providers: ProviderConfig[];
|
|
63
|
+
defaultUserId?: string;
|
|
64
|
+
defaultAnonymousId?: string;
|
|
65
|
+
debug?: boolean;
|
|
66
|
+
}
|
|
67
|
+
interface EventContext {
|
|
68
|
+
ip?: string;
|
|
69
|
+
userAgent?: string;
|
|
70
|
+
locale?: string;
|
|
71
|
+
page?: {
|
|
72
|
+
url?: string;
|
|
73
|
+
title?: string;
|
|
74
|
+
referrer?: string;
|
|
75
|
+
};
|
|
76
|
+
[key: string]: unknown;
|
|
77
|
+
}
|
|
78
|
+
interface TrackOptions {
|
|
79
|
+
event: string;
|
|
80
|
+
properties?: Record<string, unknown>;
|
|
81
|
+
userId?: string;
|
|
82
|
+
anonymousId?: string;
|
|
83
|
+
timestamp?: Date;
|
|
84
|
+
context?: EventContext;
|
|
85
|
+
}
|
|
86
|
+
interface IdentifyOptions {
|
|
87
|
+
userId: string;
|
|
88
|
+
traits?: Record<string, unknown>;
|
|
89
|
+
anonymousId?: string;
|
|
90
|
+
timestamp?: Date;
|
|
91
|
+
}
|
|
92
|
+
interface PageOptions {
|
|
93
|
+
name?: string;
|
|
94
|
+
url: string;
|
|
95
|
+
referrer?: string;
|
|
96
|
+
properties?: Record<string, unknown>;
|
|
97
|
+
userId?: string;
|
|
98
|
+
anonymousId?: string;
|
|
99
|
+
timestamp?: Date;
|
|
100
|
+
}
|
|
101
|
+
interface TrackResult {
|
|
102
|
+
provider: string;
|
|
103
|
+
success: boolean;
|
|
104
|
+
statusCode?: number;
|
|
105
|
+
error?: MytartError;
|
|
106
|
+
}
|
|
107
|
+
interface MytartError {
|
|
108
|
+
message: string;
|
|
109
|
+
code: string;
|
|
110
|
+
provider: string;
|
|
111
|
+
originalError?: unknown;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
declare class Mytart {
|
|
115
|
+
private readonly providers;
|
|
116
|
+
private readonly config;
|
|
117
|
+
constructor(config: MytartConfig);
|
|
118
|
+
track(options: TrackOptions): Promise<TrackResult[]>;
|
|
119
|
+
identify(options: IdentifyOptions): Promise<TrackResult[]>;
|
|
120
|
+
page(options: PageOptions): Promise<TrackResult[]>;
|
|
121
|
+
addProvider(config: ProviderConfig): void;
|
|
122
|
+
removeProvider(name: string): void;
|
|
123
|
+
getProviders(): string[];
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
declare abstract class BaseProvider {
|
|
127
|
+
abstract readonly name: string;
|
|
128
|
+
abstract track(options: TrackOptions): Promise<TrackResult>;
|
|
129
|
+
abstract identify(options: IdentifyOptions): Promise<TrackResult>;
|
|
130
|
+
abstract page(options: PageOptions): Promise<TrackResult>;
|
|
131
|
+
protected buildError(message: string, code: string, originalError?: unknown): TrackResult;
|
|
132
|
+
protected buildSuccess(statusCode?: number): TrackResult;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
declare class GoogleAnalyticsProvider extends BaseProvider {
|
|
136
|
+
readonly name = "google-analytics";
|
|
137
|
+
private readonly config;
|
|
138
|
+
private readonly http;
|
|
139
|
+
private readonly endpoint;
|
|
140
|
+
constructor(config: GoogleAnalyticsConfig);
|
|
141
|
+
track({ event, properties, userId, anonymousId, timestamp }: TrackOptions): Promise<TrackResult>;
|
|
142
|
+
identify({ userId, traits }: IdentifyOptions): Promise<TrackResult>;
|
|
143
|
+
page({ name, url, referrer, userId, anonymousId }: PageOptions): Promise<TrackResult>;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
declare class MixpanelProvider extends BaseProvider {
|
|
147
|
+
readonly name = "mixpanel";
|
|
148
|
+
private readonly config;
|
|
149
|
+
private readonly http;
|
|
150
|
+
constructor(config: MixpanelConfig);
|
|
151
|
+
private encodeData;
|
|
152
|
+
track({ event, properties, userId, anonymousId, timestamp }: TrackOptions): Promise<TrackResult>;
|
|
153
|
+
identify({ userId, traits }: IdentifyOptions): Promise<TrackResult>;
|
|
154
|
+
page({ name, url, userId, anonymousId, properties }: PageOptions): Promise<TrackResult>;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
declare class SegmentProvider extends BaseProvider {
|
|
158
|
+
readonly name = "segment";
|
|
159
|
+
private readonly config;
|
|
160
|
+
private readonly http;
|
|
161
|
+
private readonly baseUrl;
|
|
162
|
+
constructor(config: SegmentConfig);
|
|
163
|
+
private getAuth;
|
|
164
|
+
track({ event, properties, userId, anonymousId, timestamp, context }: TrackOptions): Promise<TrackResult>;
|
|
165
|
+
identify({ userId, traits, anonymousId, timestamp }: IdentifyOptions): Promise<TrackResult>;
|
|
166
|
+
page({ name, url, referrer, properties, userId, anonymousId, timestamp }: PageOptions): Promise<TrackResult>;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
declare class AmplitudeProvider extends BaseProvider {
|
|
170
|
+
readonly name = "amplitude";
|
|
171
|
+
private readonly config;
|
|
172
|
+
private readonly http;
|
|
173
|
+
private readonly endpoint;
|
|
174
|
+
constructor(config: AmplitudeConfig);
|
|
175
|
+
track({ event, properties, userId, anonymousId, timestamp }: TrackOptions): Promise<TrackResult>;
|
|
176
|
+
identify({ userId, traits }: IdentifyOptions): Promise<TrackResult>;
|
|
177
|
+
page({ name, url, userId, anonymousId, properties }: PageOptions): Promise<TrackResult>;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
declare class PlausibleProvider extends BaseProvider {
|
|
181
|
+
readonly name = "plausible";
|
|
182
|
+
private readonly config;
|
|
183
|
+
private readonly http;
|
|
184
|
+
private readonly endpoint;
|
|
185
|
+
constructor(config: PlausibleConfig);
|
|
186
|
+
private buildHeaders;
|
|
187
|
+
track({ event, properties, context }: TrackOptions): Promise<TrackResult>;
|
|
188
|
+
identify(_options: IdentifyOptions): Promise<TrackResult>;
|
|
189
|
+
page({ name, url, properties }: PageOptions): Promise<TrackResult>;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
declare class PostHogProvider extends BaseProvider {
|
|
193
|
+
readonly name = "posthog";
|
|
194
|
+
private readonly config;
|
|
195
|
+
private readonly http;
|
|
196
|
+
private readonly endpoint;
|
|
197
|
+
constructor(config: PostHogConfig);
|
|
198
|
+
track({ event, properties, userId, anonymousId, timestamp }: TrackOptions): Promise<TrackResult>;
|
|
199
|
+
identify({ userId, traits }: IdentifyOptions): Promise<TrackResult>;
|
|
200
|
+
page({ name, url, userId, anonymousId, properties }: PageOptions): Promise<TrackResult>;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
declare class VercelAnalyticsProvider extends BaseProvider {
|
|
204
|
+
readonly name = "vercel-analytics";
|
|
205
|
+
private readonly config;
|
|
206
|
+
private readonly http;
|
|
207
|
+
private readonly endpoint;
|
|
208
|
+
constructor(config: VercelAnalyticsConfig);
|
|
209
|
+
track({ event, properties, timestamp, context }: TrackOptions): Promise<TrackResult>;
|
|
210
|
+
/**
|
|
211
|
+
* Vercel Analytics is privacy-focused and does not support traditional user
|
|
212
|
+
* identification. This method returns an unsupported error.
|
|
213
|
+
*/
|
|
214
|
+
identify(_options: IdentifyOptions): Promise<TrackResult>;
|
|
215
|
+
page({ name, url, referrer, properties, timestamp }: PageOptions): Promise<TrackResult>;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
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 };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Fields shared by every provider configuration block.
|
|
3
|
+
* Set `enabled: true` to activate a provider for fan-out.
|
|
4
|
+
* Providers are **disabled by default** â omitting `enabled` (or setting it to
|
|
5
|
+
* `false`) means the provider is skipped entirely.
|
|
6
|
+
*/
|
|
7
|
+
interface BaseProviderConfig {
|
|
8
|
+
/** Whether this provider is active. Defaults to `false` when omitted. */
|
|
9
|
+
enabled?: boolean;
|
|
10
|
+
}
|
|
11
|
+
type ProviderName = 'google-analytics' | 'mixpanel' | 'segment' | 'amplitude' | 'plausible' | 'posthog' | 'vercel-analytics';
|
|
12
|
+
interface GoogleAnalyticsConfig extends BaseProviderConfig {
|
|
13
|
+
provider: 'google-analytics';
|
|
14
|
+
measurementId: string;
|
|
15
|
+
apiSecret: string;
|
|
16
|
+
clientId?: string;
|
|
17
|
+
debug?: boolean;
|
|
18
|
+
}
|
|
19
|
+
interface MixpanelConfig extends BaseProviderConfig {
|
|
20
|
+
provider: 'mixpanel';
|
|
21
|
+
token: string;
|
|
22
|
+
apiUrl?: string;
|
|
23
|
+
}
|
|
24
|
+
interface SegmentConfig extends BaseProviderConfig {
|
|
25
|
+
provider: 'segment';
|
|
26
|
+
writeKey: string;
|
|
27
|
+
apiUrl?: string;
|
|
28
|
+
}
|
|
29
|
+
interface AmplitudeConfig extends BaseProviderConfig {
|
|
30
|
+
provider: 'amplitude';
|
|
31
|
+
apiKey: string;
|
|
32
|
+
apiUrl?: string;
|
|
33
|
+
}
|
|
34
|
+
interface PlausibleConfig extends BaseProviderConfig {
|
|
35
|
+
provider: 'plausible';
|
|
36
|
+
domain: string;
|
|
37
|
+
apiUrl?: string;
|
|
38
|
+
userAgent?: string;
|
|
39
|
+
xForwardedFor?: string;
|
|
40
|
+
}
|
|
41
|
+
interface PostHogConfig extends BaseProviderConfig {
|
|
42
|
+
provider: 'posthog';
|
|
43
|
+
apiKey: string;
|
|
44
|
+
apiUrl?: string;
|
|
45
|
+
}
|
|
46
|
+
interface VercelAnalyticsConfig extends BaseProviderConfig {
|
|
47
|
+
provider: 'vercel-analytics';
|
|
48
|
+
/**
|
|
49
|
+
* Your Vercel Analytics ID (the `VERCEL_ANALYTICS_ID` environment variable
|
|
50
|
+
* or the measurement ID shown in your Vercel project's Analytics settings).
|
|
51
|
+
*/
|
|
52
|
+
analyticsId: string;
|
|
53
|
+
/**
|
|
54
|
+
* Override the default Vercel vitals ingestion endpoint.
|
|
55
|
+
* Defaults to `https://vitals.vercel-insights.com/v1/vitals`.
|
|
56
|
+
* On a Vercel deployment you may use `/_vercel/insights/event` instead.
|
|
57
|
+
*/
|
|
58
|
+
apiUrl?: string;
|
|
59
|
+
}
|
|
60
|
+
type ProviderConfig = GoogleAnalyticsConfig | MixpanelConfig | SegmentConfig | AmplitudeConfig | PlausibleConfig | PostHogConfig | VercelAnalyticsConfig;
|
|
61
|
+
interface MytartConfig {
|
|
62
|
+
providers: ProviderConfig[];
|
|
63
|
+
defaultUserId?: string;
|
|
64
|
+
defaultAnonymousId?: string;
|
|
65
|
+
debug?: boolean;
|
|
66
|
+
}
|
|
67
|
+
interface EventContext {
|
|
68
|
+
ip?: string;
|
|
69
|
+
userAgent?: string;
|
|
70
|
+
locale?: string;
|
|
71
|
+
page?: {
|
|
72
|
+
url?: string;
|
|
73
|
+
title?: string;
|
|
74
|
+
referrer?: string;
|
|
75
|
+
};
|
|
76
|
+
[key: string]: unknown;
|
|
77
|
+
}
|
|
78
|
+
interface TrackOptions {
|
|
79
|
+
event: string;
|
|
80
|
+
properties?: Record<string, unknown>;
|
|
81
|
+
userId?: string;
|
|
82
|
+
anonymousId?: string;
|
|
83
|
+
timestamp?: Date;
|
|
84
|
+
context?: EventContext;
|
|
85
|
+
}
|
|
86
|
+
interface IdentifyOptions {
|
|
87
|
+
userId: string;
|
|
88
|
+
traits?: Record<string, unknown>;
|
|
89
|
+
anonymousId?: string;
|
|
90
|
+
timestamp?: Date;
|
|
91
|
+
}
|
|
92
|
+
interface PageOptions {
|
|
93
|
+
name?: string;
|
|
94
|
+
url: string;
|
|
95
|
+
referrer?: string;
|
|
96
|
+
properties?: Record<string, unknown>;
|
|
97
|
+
userId?: string;
|
|
98
|
+
anonymousId?: string;
|
|
99
|
+
timestamp?: Date;
|
|
100
|
+
}
|
|
101
|
+
interface TrackResult {
|
|
102
|
+
provider: string;
|
|
103
|
+
success: boolean;
|
|
104
|
+
statusCode?: number;
|
|
105
|
+
error?: MytartError;
|
|
106
|
+
}
|
|
107
|
+
interface MytartError {
|
|
108
|
+
message: string;
|
|
109
|
+
code: string;
|
|
110
|
+
provider: string;
|
|
111
|
+
originalError?: unknown;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
declare class Mytart {
|
|
115
|
+
private readonly providers;
|
|
116
|
+
private readonly config;
|
|
117
|
+
constructor(config: MytartConfig);
|
|
118
|
+
track(options: TrackOptions): Promise<TrackResult[]>;
|
|
119
|
+
identify(options: IdentifyOptions): Promise<TrackResult[]>;
|
|
120
|
+
page(options: PageOptions): Promise<TrackResult[]>;
|
|
121
|
+
addProvider(config: ProviderConfig): void;
|
|
122
|
+
removeProvider(name: string): void;
|
|
123
|
+
getProviders(): string[];
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
declare abstract class BaseProvider {
|
|
127
|
+
abstract readonly name: string;
|
|
128
|
+
abstract track(options: TrackOptions): Promise<TrackResult>;
|
|
129
|
+
abstract identify(options: IdentifyOptions): Promise<TrackResult>;
|
|
130
|
+
abstract page(options: PageOptions): Promise<TrackResult>;
|
|
131
|
+
protected buildError(message: string, code: string, originalError?: unknown): TrackResult;
|
|
132
|
+
protected buildSuccess(statusCode?: number): TrackResult;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
declare class GoogleAnalyticsProvider extends BaseProvider {
|
|
136
|
+
readonly name = "google-analytics";
|
|
137
|
+
private readonly config;
|
|
138
|
+
private readonly http;
|
|
139
|
+
private readonly endpoint;
|
|
140
|
+
constructor(config: GoogleAnalyticsConfig);
|
|
141
|
+
track({ event, properties, userId, anonymousId, timestamp }: TrackOptions): Promise<TrackResult>;
|
|
142
|
+
identify({ userId, traits }: IdentifyOptions): Promise<TrackResult>;
|
|
143
|
+
page({ name, url, referrer, userId, anonymousId }: PageOptions): Promise<TrackResult>;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
declare class MixpanelProvider extends BaseProvider {
|
|
147
|
+
readonly name = "mixpanel";
|
|
148
|
+
private readonly config;
|
|
149
|
+
private readonly http;
|
|
150
|
+
constructor(config: MixpanelConfig);
|
|
151
|
+
private encodeData;
|
|
152
|
+
track({ event, properties, userId, anonymousId, timestamp }: TrackOptions): Promise<TrackResult>;
|
|
153
|
+
identify({ userId, traits }: IdentifyOptions): Promise<TrackResult>;
|
|
154
|
+
page({ name, url, userId, anonymousId, properties }: PageOptions): Promise<TrackResult>;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
declare class SegmentProvider extends BaseProvider {
|
|
158
|
+
readonly name = "segment";
|
|
159
|
+
private readonly config;
|
|
160
|
+
private readonly http;
|
|
161
|
+
private readonly baseUrl;
|
|
162
|
+
constructor(config: SegmentConfig);
|
|
163
|
+
private getAuth;
|
|
164
|
+
track({ event, properties, userId, anonymousId, timestamp, context }: TrackOptions): Promise<TrackResult>;
|
|
165
|
+
identify({ userId, traits, anonymousId, timestamp }: IdentifyOptions): Promise<TrackResult>;
|
|
166
|
+
page({ name, url, referrer, properties, userId, anonymousId, timestamp }: PageOptions): Promise<TrackResult>;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
declare class AmplitudeProvider extends BaseProvider {
|
|
170
|
+
readonly name = "amplitude";
|
|
171
|
+
private readonly config;
|
|
172
|
+
private readonly http;
|
|
173
|
+
private readonly endpoint;
|
|
174
|
+
constructor(config: AmplitudeConfig);
|
|
175
|
+
track({ event, properties, userId, anonymousId, timestamp }: TrackOptions): Promise<TrackResult>;
|
|
176
|
+
identify({ userId, traits }: IdentifyOptions): Promise<TrackResult>;
|
|
177
|
+
page({ name, url, userId, anonymousId, properties }: PageOptions): Promise<TrackResult>;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
declare class PlausibleProvider extends BaseProvider {
|
|
181
|
+
readonly name = "plausible";
|
|
182
|
+
private readonly config;
|
|
183
|
+
private readonly http;
|
|
184
|
+
private readonly endpoint;
|
|
185
|
+
constructor(config: PlausibleConfig);
|
|
186
|
+
private buildHeaders;
|
|
187
|
+
track({ event, properties, context }: TrackOptions): Promise<TrackResult>;
|
|
188
|
+
identify(_options: IdentifyOptions): Promise<TrackResult>;
|
|
189
|
+
page({ name, url, properties }: PageOptions): Promise<TrackResult>;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
declare class PostHogProvider extends BaseProvider {
|
|
193
|
+
readonly name = "posthog";
|
|
194
|
+
private readonly config;
|
|
195
|
+
private readonly http;
|
|
196
|
+
private readonly endpoint;
|
|
197
|
+
constructor(config: PostHogConfig);
|
|
198
|
+
track({ event, properties, userId, anonymousId, timestamp }: TrackOptions): Promise<TrackResult>;
|
|
199
|
+
identify({ userId, traits }: IdentifyOptions): Promise<TrackResult>;
|
|
200
|
+
page({ name, url, userId, anonymousId, properties }: PageOptions): Promise<TrackResult>;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
declare class VercelAnalyticsProvider extends BaseProvider {
|
|
204
|
+
readonly name = "vercel-analytics";
|
|
205
|
+
private readonly config;
|
|
206
|
+
private readonly http;
|
|
207
|
+
private readonly endpoint;
|
|
208
|
+
constructor(config: VercelAnalyticsConfig);
|
|
209
|
+
track({ event, properties, timestamp, context }: TrackOptions): Promise<TrackResult>;
|
|
210
|
+
/**
|
|
211
|
+
* Vercel Analytics is privacy-focused and does not support traditional user
|
|
212
|
+
* identification. This method returns an unsupported error.
|
|
213
|
+
*/
|
|
214
|
+
identify(_options: IdentifyOptions): Promise<TrackResult>;
|
|
215
|
+
page({ name, url, referrer, properties, timestamp }: PageOptions): Promise<TrackResult>;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
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 };
|