mytart 0.3.0 → 0.5.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 +155 -3
- package/dist/index.d.mts +247 -3
- package/dist/index.d.ts +247 -3
- package/dist/index.js +398 -1
- package/dist/index.mjs +397 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
## Features
|
|
8
8
|
|
|
9
|
-
- 🔌 **
|
|
9
|
+
- 🔌 **7 providers out of the box**: Google Analytics 4, Mixpanel, Segment, Amplitude, Plausible, PostHog, Meta Pixel
|
|
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`
|
|
@@ -86,6 +86,64 @@ When `appType: 'browser'` is set:
|
|
|
86
86
|
- SSR-safe: silently succeeds when `window` is undefined (e.g. during server-side rendering)
|
|
87
87
|
- `apiSecret` is not required (and not used)
|
|
88
88
|
|
|
89
|
+
#### Google Signals (demographics)
|
|
90
|
+
|
|
91
|
+
To enable demographic data (age, gender, interests) in GA4 reports, set `signals: true`:
|
|
92
|
+
|
|
93
|
+
```typescript
|
|
94
|
+
{
|
|
95
|
+
provider: 'google-analytics',
|
|
96
|
+
measurementId: 'G-XXXXXXXXXX',
|
|
97
|
+
appType: 'browser',
|
|
98
|
+
enabled: true,
|
|
99
|
+
signals: true,
|
|
100
|
+
}
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
This does two things automatically:
|
|
104
|
+
|
|
105
|
+
1. Passes `allow_google_signals: true` and `allow_ad_personalization_signals: true` to `gtag('config')`
|
|
106
|
+
2. Sets Consent Mode v2 defaults granting `ad_personalization`, `ad_user_data`, `ad_storage`, and `analytics_storage`
|
|
107
|
+
|
|
108
|
+
Set `signals: false` to explicitly disable Google Signals. Omit the flag entirely to use Google's default behaviour.
|
|
109
|
+
|
|
110
|
+
> **Note**: You must also enable Google Signals in the GA4 admin panel (Admin > Data Settings > Data Collection) for demographic data to appear.
|
|
111
|
+
|
|
112
|
+
#### Consent Mode v2
|
|
113
|
+
|
|
114
|
+
For GDPR/privacy compliance you can control Consent Mode v2 directly. Use `defaultConsent` to set the initial consent state (emitted before `gtag('config')`), and `updateConsent()` to change it at runtime when the user interacts with a cookie banner.
|
|
115
|
+
|
|
116
|
+
```typescript
|
|
117
|
+
const analytics = new Mytart({
|
|
118
|
+
providers: [{
|
|
119
|
+
provider: 'google-analytics',
|
|
120
|
+
measurementId: 'G-XXXXXXXXXX',
|
|
121
|
+
appType: 'browser',
|
|
122
|
+
enabled: true,
|
|
123
|
+
signals: true,
|
|
124
|
+
defaultConsent: {
|
|
125
|
+
ad_storage: 'denied',
|
|
126
|
+
analytics_storage: 'denied',
|
|
127
|
+
ad_user_data: 'denied',
|
|
128
|
+
ad_personalization: 'denied',
|
|
129
|
+
},
|
|
130
|
+
consentWaitForUpdate: 500, // wait 500ms for consent banner
|
|
131
|
+
}],
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
// After the user accepts the cookie banner:
|
|
135
|
+
await analytics.updateConsent({
|
|
136
|
+
ad_storage: 'granted',
|
|
137
|
+
analytics_storage: 'granted',
|
|
138
|
+
ad_user_data: 'granted',
|
|
139
|
+
ad_personalization: 'granted',
|
|
140
|
+
});
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
When both `signals: true` and `defaultConsent` are set, the explicit `defaultConsent` takes precedence over the auto-consent that `signals` would generate. This lets you combine `signals: true` (for the config flags) with a GDPR-safe denied-by-default consent flow.
|
|
144
|
+
|
|
145
|
+
Consent Mode is a no-op in server mode (the Measurement Protocol does not support it).
|
|
146
|
+
|
|
89
147
|
### Mixpanel
|
|
90
148
|
|
|
91
149
|
```typescript
|
|
@@ -118,6 +176,83 @@ When `appType: 'browser'` is set:
|
|
|
118
176
|
{ provider: 'posthog', apiKey: 'phc_YOUR_KEY', apiUrl?: string }
|
|
119
177
|
```
|
|
120
178
|
|
|
179
|
+
### Meta Pixel
|
|
180
|
+
|
|
181
|
+
Meta Pixel supports two modes via the `appType` option:
|
|
182
|
+
|
|
183
|
+
#### Server mode (default)
|
|
184
|
+
|
|
185
|
+
Uses the [Meta Conversions API](https://developers.facebook.com/docs/marketing-api/conversions-api) — direct HTTP calls, no browser APIs. Use this for Node.js, API routes, serverless functions, etc.
|
|
186
|
+
|
|
187
|
+
```typescript
|
|
188
|
+
{
|
|
189
|
+
provider: 'meta-pixel',
|
|
190
|
+
pixelId: '123456789',
|
|
191
|
+
accessToken: 'YOUR_ACCESS_TOKEN',
|
|
192
|
+
enabled: true,
|
|
193
|
+
// appType defaults to 'server'
|
|
194
|
+
}
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
PII fields in `user_data` (`em`, `ph`, `fn`, `ln`, `ge`, `db`, `ct`, `st`, `zp`, `country`) are automatically SHA-256 hashed before being sent to the Conversions API. Already-hashed values are not double-hashed.
|
|
198
|
+
|
|
199
|
+
#### Browser mode
|
|
200
|
+
|
|
201
|
+
Injects Meta's official [fbevents.js snippet](https://developers.facebook.com/docs/meta-pixel/get-started) into the page. Use this for client-side tracking in any framework.
|
|
202
|
+
|
|
203
|
+
```typescript
|
|
204
|
+
{
|
|
205
|
+
provider: 'meta-pixel',
|
|
206
|
+
pixelId: '123456789',
|
|
207
|
+
appType: 'browser',
|
|
208
|
+
advancedMatching: { em: 'user@example.com' },
|
|
209
|
+
enabled: true,
|
|
210
|
+
}
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
When `appType: 'browser'` is set:
|
|
214
|
+
|
|
215
|
+
- The fbevents.js script is loaded once on the first `track()`, `identify()`, or `page()` call
|
|
216
|
+
- Standard Meta events (e.g. `Purchase`, `AddToCart`, `ViewContent`) use `fbq('track', ...)` — custom events use `fbq('trackCustom', ...)`
|
|
217
|
+
- SSR-safe: silently succeeds when `window` is undefined
|
|
218
|
+
- `accessToken` is not required
|
|
219
|
+
|
|
220
|
+
#### Event deduplication
|
|
221
|
+
|
|
222
|
+
Pass `eventId` via context to deduplicate browser + server events:
|
|
223
|
+
|
|
224
|
+
```typescript
|
|
225
|
+
await analytics.track({
|
|
226
|
+
event: 'Purchase',
|
|
227
|
+
properties: { currency: 'USD', value: 42 },
|
|
228
|
+
context: { eventId: 'order-abc-123' },
|
|
229
|
+
});
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
In browser mode this passes `{ eventID: 'order-abc-123' }` as the 4th `fbq()` parameter. In server mode it sets the `event_id` field in the Conversions API payload.
|
|
233
|
+
|
|
234
|
+
#### Identify
|
|
235
|
+
|
|
236
|
+
In browser mode, `identify()` re-calls `fbq('init', pixelId, newTraits)` to update Advanced Matching data. In server mode, traits are cached in memory and included as `user_data` in all subsequent `track()` / `page()` calls.
|
|
237
|
+
|
|
238
|
+
#### Consent
|
|
239
|
+
|
|
240
|
+
Meta Pixel uses a simple binary consent model. Access it directly on the provider instance:
|
|
241
|
+
|
|
242
|
+
```typescript
|
|
243
|
+
import { MetaPixelProvider } from 'mytart';
|
|
244
|
+
|
|
245
|
+
// Grant consent
|
|
246
|
+
const metaProvider = analytics.getProviders().includes('meta-pixel')
|
|
247
|
+
? (analytics as any) // or access the provider directly
|
|
248
|
+
: null;
|
|
249
|
+
|
|
250
|
+
// Or use the provider directly:
|
|
251
|
+
const provider = new MetaPixelProvider({ provider: 'meta-pixel', pixelId: '123', appType: 'browser' });
|
|
252
|
+
await provider.updatePixelConsent(true); // fbq('consent', 'grant')
|
|
253
|
+
await provider.updatePixelConsent(false); // fbq('consent', 'revoke')
|
|
254
|
+
```
|
|
255
|
+
|
|
121
256
|
## API Reference
|
|
122
257
|
|
|
123
258
|
### `new Mytart(config: MytartConfig)`
|
|
@@ -180,6 +315,22 @@ interface PageOptions {
|
|
|
180
315
|
}
|
|
181
316
|
```
|
|
182
317
|
|
|
318
|
+
### `analytics.updateConsent(consent: ConsentSettings): Promise<void>`
|
|
319
|
+
|
|
320
|
+
Updates Google Consent Mode v2 state at runtime. Call this when the user interacts with a cookie/consent banner. Only affects Google Analytics in browser mode; all other providers ignore it.
|
|
321
|
+
|
|
322
|
+
```typescript
|
|
323
|
+
interface ConsentSettings {
|
|
324
|
+
ad_storage?: 'granted' | 'denied';
|
|
325
|
+
analytics_storage?: 'granted' | 'denied';
|
|
326
|
+
ad_user_data?: 'granted' | 'denied';
|
|
327
|
+
ad_personalization?: 'granted' | 'denied';
|
|
328
|
+
functionality_storage?: 'granted' | 'denied';
|
|
329
|
+
personalization_storage?: 'granted' | 'denied';
|
|
330
|
+
security_storage?: 'granted' | 'denied';
|
|
331
|
+
}
|
|
332
|
+
```
|
|
333
|
+
|
|
183
334
|
### `analytics.addProvider(config: ProviderConfig): void`
|
|
184
335
|
|
|
185
336
|
Dynamically add a provider at runtime.
|
|
@@ -220,8 +371,9 @@ All types are exported:
|
|
|
220
371
|
import type {
|
|
221
372
|
MytartConfig, BaseProviderConfig, ProviderConfig, TrackOptions, IdentifyOptions, PageOptions,
|
|
222
373
|
TrackResult, MytartError, EventContext, ProviderName, GoogleAnalyticsAppType,
|
|
223
|
-
GoogleAnalyticsConfig, MixpanelConfig, SegmentConfig,
|
|
224
|
-
AmplitudeConfig, PlausibleConfig, PostHogConfig,
|
|
374
|
+
GoogleAnalyticsConfig, ConsentSettings, ConsentState, MixpanelConfig, SegmentConfig,
|
|
375
|
+
AmplitudeConfig, PlausibleConfig, PostHogConfig, MetaPixelConfig, MetaPixelAppType,
|
|
376
|
+
MetaPixelAdvancedMatching,
|
|
225
377
|
} from 'mytart';
|
|
226
378
|
```
|
|
227
379
|
|
package/dist/index.d.mts
CHANGED
|
@@ -8,7 +8,34 @@ interface BaseProviderConfig {
|
|
|
8
8
|
/** Whether this provider is active. Defaults to `false` when omitted. */
|
|
9
9
|
enabled?: boolean;
|
|
10
10
|
}
|
|
11
|
-
type
|
|
11
|
+
type ConsentState = 'granted' | 'denied';
|
|
12
|
+
/**
|
|
13
|
+
* Google Consent Mode v2 settings.
|
|
14
|
+
* Controls how Google tags behave based on user consent.
|
|
15
|
+
*
|
|
16
|
+
* Key fields for Google Signals demographics (age, gender, interests):
|
|
17
|
+
* - `ad_personalization` — must be `'granted'` for Google Signals to attribute
|
|
18
|
+
* demographic data to sessions.
|
|
19
|
+
* - `ad_user_data` — must be `'granted'` for user data to be sent to Google
|
|
20
|
+
* for advertising purposes.
|
|
21
|
+
*/
|
|
22
|
+
interface ConsentSettings {
|
|
23
|
+
/** Controls storage of advertising-related cookies. */
|
|
24
|
+
ad_storage?: ConsentState;
|
|
25
|
+
/** Controls storage of analytics-related cookies. */
|
|
26
|
+
analytics_storage?: ConsentState;
|
|
27
|
+
/** Controls whether user data can be sent to Google for advertising. */
|
|
28
|
+
ad_user_data?: ConsentState;
|
|
29
|
+
/** Controls whether data can be used for personalized advertising. */
|
|
30
|
+
ad_personalization?: ConsentState;
|
|
31
|
+
/** Controls storage for functional purposes (e.g. language settings). */
|
|
32
|
+
functionality_storage?: ConsentState;
|
|
33
|
+
/** Controls storage for personalization (e.g. video recommendations). */
|
|
34
|
+
personalization_storage?: ConsentState;
|
|
35
|
+
/** Controls storage for security purposes (e.g. authentication). */
|
|
36
|
+
security_storage?: ConsentState;
|
|
37
|
+
}
|
|
38
|
+
type ProviderName = 'google-analytics' | 'mixpanel' | 'segment' | 'amplitude' | 'plausible' | 'posthog' | 'meta-pixel';
|
|
12
39
|
type GoogleAnalyticsAppType = 'browser' | 'server';
|
|
13
40
|
interface GoogleAnalyticsConfig extends BaseProviderConfig {
|
|
14
41
|
provider: 'google-analytics';
|
|
@@ -17,6 +44,44 @@ interface GoogleAnalyticsConfig extends BaseProviderConfig {
|
|
|
17
44
|
clientId?: string;
|
|
18
45
|
debug?: boolean;
|
|
19
46
|
appType?: GoogleAnalyticsAppType;
|
|
47
|
+
/**
|
|
48
|
+
* Default consent state set before gtag('config'). In browser mode this
|
|
49
|
+
* emits `gtag('consent', 'default', ...)` so Google tags respect user
|
|
50
|
+
* consent from the very first hit. Use `Mytart.updateConsent()` to change
|
|
51
|
+
* consent at runtime (e.g. after a cookie banner interaction).
|
|
52
|
+
*
|
|
53
|
+
* For Google Signals demographics, `ad_personalization` and `ad_user_data`
|
|
54
|
+
* must eventually be set to `'granted'`.
|
|
55
|
+
*/
|
|
56
|
+
defaultConsent?: ConsentSettings;
|
|
57
|
+
/**
|
|
58
|
+
* When `true`, sets `wait_for_update` (in milliseconds) on the default
|
|
59
|
+
* consent command. This tells Google tags to wait the specified number of
|
|
60
|
+
* milliseconds for a consent update before sending the first hit. Useful
|
|
61
|
+
* when a consent management platform loads asynchronously.
|
|
62
|
+
* Defaults to `undefined` (no wait).
|
|
63
|
+
*/
|
|
64
|
+
consentWaitForUpdate?: number;
|
|
65
|
+
/**
|
|
66
|
+
* Convenience flag to enable or disable Google Signals for demographics
|
|
67
|
+
* (age, gender, interests).
|
|
68
|
+
*
|
|
69
|
+
* - `true` — passes `allow_google_signals: true` and
|
|
70
|
+
* `allow_ad_personalization_signals: true` in the `gtag('config')`
|
|
71
|
+
* call. If `defaultConsent` is not explicitly set, automatically
|
|
72
|
+
* configures Consent Mode v2 to grant `ad_personalization`,
|
|
73
|
+
* `ad_user_data`, `ad_storage`, and `analytics_storage`.
|
|
74
|
+
* - `false` — passes `allow_google_signals: false` and
|
|
75
|
+
* `allow_ad_personalization_signals: false` to explicitly disable
|
|
76
|
+
* Google Signals.
|
|
77
|
+
* - `undefined` (default) — uses Google's default behaviour (Signals
|
|
78
|
+
* enabled when the GA4 property has it turned on in Admin).
|
|
79
|
+
*
|
|
80
|
+
* **Note**: Google Signals must also be enabled in the GA4 admin panel
|
|
81
|
+
* (Admin › Data Settings › Data Collection) for demographic data to
|
|
82
|
+
* appear. This flag controls the client-side consent and config only.
|
|
83
|
+
*/
|
|
84
|
+
signals?: boolean;
|
|
20
85
|
}
|
|
21
86
|
interface MixpanelConfig extends BaseProviderConfig {
|
|
22
87
|
provider: 'mixpanel';
|
|
@@ -45,7 +110,77 @@ interface PostHogConfig extends BaseProviderConfig {
|
|
|
45
110
|
apiKey: string;
|
|
46
111
|
apiUrl?: string;
|
|
47
112
|
}
|
|
48
|
-
|
|
113
|
+
/**
|
|
114
|
+
* Advanced Matching data for the Meta Pixel.
|
|
115
|
+
* In browser mode these fields are passed to `fbq('init', pixelId, matchData)`.
|
|
116
|
+
* In server mode they are included in the `user_data` object sent to the
|
|
117
|
+
* Conversions API — plain-text values are automatically SHA-256 hashed before
|
|
118
|
+
* sending.
|
|
119
|
+
*/
|
|
120
|
+
interface MetaPixelAdvancedMatching {
|
|
121
|
+
/** Email address */
|
|
122
|
+
em?: string;
|
|
123
|
+
/** Phone number */
|
|
124
|
+
ph?: string;
|
|
125
|
+
/** First name */
|
|
126
|
+
fn?: string;
|
|
127
|
+
/** Last name */
|
|
128
|
+
ln?: string;
|
|
129
|
+
/** Gender (m or f) */
|
|
130
|
+
ge?: string;
|
|
131
|
+
/** Date of birth (YYYYMMDD) */
|
|
132
|
+
db?: string;
|
|
133
|
+
/** City */
|
|
134
|
+
ct?: string;
|
|
135
|
+
/** State / province (2-letter code) */
|
|
136
|
+
st?: string;
|
|
137
|
+
/** Zip / postal code */
|
|
138
|
+
zp?: string;
|
|
139
|
+
/** Country (2-letter ISO code) */
|
|
140
|
+
country?: string;
|
|
141
|
+
/** External ID */
|
|
142
|
+
external_id?: string;
|
|
143
|
+
}
|
|
144
|
+
type MetaPixelAppType = 'browser' | 'server';
|
|
145
|
+
interface MetaPixelConfig extends BaseProviderConfig {
|
|
146
|
+
provider: 'meta-pixel';
|
|
147
|
+
/** The Meta Pixel ID (numeric string). */
|
|
148
|
+
pixelId: string;
|
|
149
|
+
/**
|
|
150
|
+
* Access token for the Conversions API. Required when `appType` is
|
|
151
|
+
* `'server'` — ignored in browser mode.
|
|
152
|
+
*/
|
|
153
|
+
accessToken?: string;
|
|
154
|
+
/**
|
|
155
|
+
* `'browser'` injects the official fbevents.js snippet and calls `window.fbq()`.
|
|
156
|
+
* `'server'` (default) sends events via the Conversions API HTTP endpoint.
|
|
157
|
+
*/
|
|
158
|
+
appType?: MetaPixelAppType;
|
|
159
|
+
/**
|
|
160
|
+
* Graph API version to use for the Conversions API.
|
|
161
|
+
* Defaults to `'v21.0'`.
|
|
162
|
+
*/
|
|
163
|
+
apiVersion?: string;
|
|
164
|
+
/**
|
|
165
|
+
* Test Event Code for the Events Manager Test Events tool.
|
|
166
|
+
* Only used in server mode (Conversions API).
|
|
167
|
+
*/
|
|
168
|
+
testEventCode?: string;
|
|
169
|
+
/**
|
|
170
|
+
* Advanced Matching data passed to `fbq('init')` in browser mode.
|
|
171
|
+
* In server mode this populates the initial `user_data` for all events.
|
|
172
|
+
*/
|
|
173
|
+
advancedMatching?: MetaPixelAdvancedMatching;
|
|
174
|
+
/**
|
|
175
|
+
* When `true` (default), the Pixel uses Automatic Configuration
|
|
176
|
+
* (auto-detects button clicks, page metadata, etc.).
|
|
177
|
+
* Set to `false` to disable.
|
|
178
|
+
*/
|
|
179
|
+
autoConfig?: boolean;
|
|
180
|
+
/** Enable Meta Pixel debug mode (`fbq('set', 'debug', true)`). */
|
|
181
|
+
debug?: boolean;
|
|
182
|
+
}
|
|
183
|
+
type ProviderConfig = GoogleAnalyticsConfig | MixpanelConfig | SegmentConfig | AmplitudeConfig | PlausibleConfig | PostHogConfig | MetaPixelConfig;
|
|
49
184
|
interface MytartConfig {
|
|
50
185
|
providers: ProviderConfig[];
|
|
51
186
|
defaultUserId?: string;
|
|
@@ -106,6 +241,16 @@ declare class Mytart {
|
|
|
106
241
|
track(options: TrackOptions): Promise<TrackResult[]>;
|
|
107
242
|
identify(options: IdentifyOptions): Promise<TrackResult[]>;
|
|
108
243
|
page(options: PageOptions): Promise<TrackResult[]>;
|
|
244
|
+
/**
|
|
245
|
+
* Update consent state across all providers that support consent management.
|
|
246
|
+
* Currently this is meaningful for Google Analytics (Consent Mode v2) in
|
|
247
|
+
* browser mode. Call this when the user interacts with a cookie/consent
|
|
248
|
+
* banner.
|
|
249
|
+
*
|
|
250
|
+
* To enable Google Signals demographics (age, gender, interests), grant
|
|
251
|
+
* at least `ad_personalization` and `ad_user_data`.
|
|
252
|
+
*/
|
|
253
|
+
updateConsent(consent: ConsentSettings): Promise<void>;
|
|
109
254
|
addProvider(config: ProviderConfig): void;
|
|
110
255
|
removeProvider(name: string): void;
|
|
111
256
|
getProviders(): string[];
|
|
@@ -116,6 +261,11 @@ declare abstract class BaseProvider {
|
|
|
116
261
|
abstract track(options: TrackOptions): Promise<TrackResult>;
|
|
117
262
|
abstract identify(options: IdentifyOptions): Promise<TrackResult>;
|
|
118
263
|
abstract page(options: PageOptions): Promise<TrackResult>;
|
|
264
|
+
/**
|
|
265
|
+
* Update consent state. Only meaningful for providers that support consent
|
|
266
|
+
* management (e.g. Google Analytics Consent Mode v2). Default is a no-op.
|
|
267
|
+
*/
|
|
268
|
+
updateConsent(_consent: ConsentSettings): Promise<void>;
|
|
119
269
|
protected buildError(message: string, code: string, originalError?: unknown): TrackResult;
|
|
120
270
|
protected buildSuccess(statusCode?: number): TrackResult;
|
|
121
271
|
}
|
|
@@ -147,6 +297,10 @@ declare class GoogleAnalyticsProvider extends BaseProvider {
|
|
|
147
297
|
* gtag('config', 'TAG_ID');
|
|
148
298
|
* </script>
|
|
149
299
|
*
|
|
300
|
+
* When `defaultConsent` is configured, a `gtag('consent', 'default', ...)`
|
|
301
|
+
* call is emitted **before** `gtag('js')` and `gtag('config')`, as required
|
|
302
|
+
* by Google's Consent Mode v2 specification.
|
|
303
|
+
*
|
|
150
304
|
* Key details:
|
|
151
305
|
* - The script URL MUST include ?id=TAG_ID for Google Tag Tester detection
|
|
152
306
|
* - dataLayer and the gtag shim are set up BEFORE the script loads
|
|
@@ -164,6 +318,18 @@ declare class GoogleAnalyticsProvider extends BaseProvider {
|
|
|
164
318
|
private identifyBrowser;
|
|
165
319
|
private pageBrowser;
|
|
166
320
|
private buildGtagResult;
|
|
321
|
+
/**
|
|
322
|
+
* Updates the consent state at runtime. In browser mode this emits
|
|
323
|
+
* `gtag('consent', 'update', ...)`. Call this when the user interacts
|
|
324
|
+
* with a cookie/consent banner.
|
|
325
|
+
*
|
|
326
|
+
* To enable Google Signals demographics (age, gender, interests), grant
|
|
327
|
+
* at least `ad_personalization` and `ad_user_data`.
|
|
328
|
+
*
|
|
329
|
+
* In server mode this is a no-op — the Measurement Protocol does not
|
|
330
|
+
* support Consent Mode.
|
|
331
|
+
*/
|
|
332
|
+
updateConsent(consent: ConsentSettings): Promise<void>;
|
|
167
333
|
track({ event, properties, userId, anonymousId, timestamp }: TrackOptions): Promise<TrackResult>;
|
|
168
334
|
identify({ userId, traits }: IdentifyOptions): Promise<TrackResult>;
|
|
169
335
|
page({ name, url, referrer, userId, anonymousId }: PageOptions): Promise<TrackResult>;
|
|
@@ -226,4 +392,82 @@ declare class PostHogProvider extends BaseProvider {
|
|
|
226
392
|
page({ name, url, userId, anonymousId, properties }: PageOptions): Promise<TrackResult>;
|
|
227
393
|
}
|
|
228
394
|
|
|
229
|
-
|
|
395
|
+
declare global {
|
|
396
|
+
interface Window {
|
|
397
|
+
fbq: FbqFn & {
|
|
398
|
+
callMethod?: FbqFn;
|
|
399
|
+
queue?: unknown[];
|
|
400
|
+
loaded?: boolean;
|
|
401
|
+
version?: string;
|
|
402
|
+
push?: FbqFn;
|
|
403
|
+
};
|
|
404
|
+
_fbq: Window['fbq'];
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
type FbqFn = (...args: unknown[]) => void;
|
|
408
|
+
declare class MetaPixelProvider extends BaseProvider {
|
|
409
|
+
readonly name = "meta-pixel";
|
|
410
|
+
private readonly config;
|
|
411
|
+
private readonly http;
|
|
412
|
+
private readonly isBrowser;
|
|
413
|
+
private readonly apiVersion;
|
|
414
|
+
private fbqReady;
|
|
415
|
+
/**
|
|
416
|
+
* Cached user data for the Conversions API. Updated by `identify()` so
|
|
417
|
+
* that subsequent `track()` and `page()` calls include user information.
|
|
418
|
+
*/
|
|
419
|
+
private cachedUserData;
|
|
420
|
+
constructor(config: MetaPixelConfig);
|
|
421
|
+
/**
|
|
422
|
+
* Initialises the Meta Pixel snippet. This mirrors the official snippet from
|
|
423
|
+
* https://developers.facebook.com/docs/meta-pixel/get-started:
|
|
424
|
+
*
|
|
425
|
+
* !function(f,b,e,v,n,t,s){...}(window, document,'script',
|
|
426
|
+
* 'https://connect.facebook.net/en_US/fbevents.js');
|
|
427
|
+
* fbq('init', 'PIXEL_ID');
|
|
428
|
+
* fbq('track', 'PageView');
|
|
429
|
+
*
|
|
430
|
+
* Key details:
|
|
431
|
+
* - The fbq shim queue is set up synchronously before the script loads
|
|
432
|
+
* - `fbq('init', pixelId, advancedMatching?)` is called immediately
|
|
433
|
+
* - autoConfig is respected via `fbq('set', 'autoConfig', false, pixelId)`
|
|
434
|
+
* - debug mode via `fbq('set', 'debug', true)`
|
|
435
|
+
* - The returned promise resolves when the script finishes loading
|
|
436
|
+
*/
|
|
437
|
+
private initFbq;
|
|
438
|
+
/**
|
|
439
|
+
* Ensures the Pixel is initialised exactly once. Subsequent calls return
|
|
440
|
+
* the same promise so the script is never injected twice.
|
|
441
|
+
*/
|
|
442
|
+
private ensureFbq;
|
|
443
|
+
private trackBrowser;
|
|
444
|
+
private identifyBrowser;
|
|
445
|
+
private pageBrowser;
|
|
446
|
+
private buildFbqResult;
|
|
447
|
+
private get capiEndpoint();
|
|
448
|
+
/**
|
|
449
|
+
* Build the `user_data` object for a CAPI event.
|
|
450
|
+
* Merges cached user data (from `identify()` / config) with any per-event
|
|
451
|
+
* overrides, then SHA-256 hashes known PII fields.
|
|
452
|
+
*/
|
|
453
|
+
private buildUserData;
|
|
454
|
+
private trackServer;
|
|
455
|
+
private identifyServer;
|
|
456
|
+
private pageServer;
|
|
457
|
+
/**
|
|
458
|
+
* Meta Pixel consent is a binary grant/revoke model, not the granular
|
|
459
|
+
* settings of Google Consent Mode v2. This method accepts the standard
|
|
460
|
+
* `ConsentSettings` type but is a no-op — use `updatePixelConsent()` for
|
|
461
|
+
* Meta-specific consent control in browser mode.
|
|
462
|
+
*/
|
|
463
|
+
/**
|
|
464
|
+
* Grant or revoke consent for the Meta Pixel in browser mode.
|
|
465
|
+
* Calls `fbq('consent', 'grant')` or `fbq('consent', 'revoke')`.
|
|
466
|
+
*/
|
|
467
|
+
updatePixelConsent(granted: boolean): Promise<void>;
|
|
468
|
+
track(options: TrackOptions): Promise<TrackResult>;
|
|
469
|
+
identify(options: IdentifyOptions): Promise<TrackResult>;
|
|
470
|
+
page(options: PageOptions): Promise<TrackResult>;
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
export { type AmplitudeConfig, AmplitudeProvider, BaseProvider, type BaseProviderConfig, type ConsentSettings, type ConsentState, type EventContext, type GoogleAnalyticsAppType, type GoogleAnalyticsConfig, GoogleAnalyticsProvider, type IdentifyOptions, type MetaPixelAdvancedMatching, type MetaPixelAppType, type MetaPixelConfig, MetaPixelProvider, 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 };
|