@shware/analytics 15.0.1 → 16.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 +164 -36
- package/dist/native/fbsdk.cjs +44 -1
- package/dist/native/fbsdk.cjs.map +1 -1
- package/dist/native/fbsdk.d.cts +23 -1
- package/dist/native/fbsdk.d.cts.map +1 -1
- package/dist/native/fbsdk.d.mts +23 -1
- package/dist/native/fbsdk.d.mts.map +1 -1
- package/dist/native/fbsdk.mjs +44 -3
- package/dist/native/fbsdk.mjs.map +1 -1
- package/dist/native/firebase.cjs +27 -0
- package/dist/native/firebase.cjs.map +1 -1
- package/dist/native/firebase.d.cts +11 -0
- package/dist/native/firebase.d.cts.map +1 -1
- package/dist/native/firebase.d.mts +11 -0
- package/dist/native/firebase.d.mts.map +1 -1
- package/dist/native/firebase.mjs +27 -2
- package/dist/native/firebase.mjs.map +1 -1
- package/dist/next/index.cjs +10 -123
- package/dist/next/index.cjs.map +1 -1
- package/dist/next/index.d.cts +2 -31
- package/dist/next/index.d.cts.map +1 -1
- package/dist/next/index.d.mts +2 -31
- package/dist/next/index.d.mts.map +1 -1
- package/dist/next/index.mjs +10 -123
- package/dist/next/index.mjs.map +1 -1
- package/dist/react/index.cjs +45 -0
- package/dist/react/index.cjs.map +1 -0
- package/dist/react/index.d.cts +15 -0
- package/dist/react/index.d.cts.map +1 -0
- package/dist/react/index.d.mts +15 -0
- package/dist/react/index.d.mts.map +1 -0
- package/dist/react/index.mjs +43 -0
- package/dist/react/index.mjs.map +1 -0
- package/dist/react/use-feature-flag.cjs +23 -0
- package/dist/react/use-feature-flag.cjs.map +1 -0
- package/dist/react/use-feature-flag.d.cts +10 -0
- package/dist/react/use-feature-flag.d.cts.map +1 -0
- package/dist/react/use-feature-flag.d.mts +10 -0
- package/dist/react/use-feature-flag.d.mts.map +1 -0
- package/dist/react/use-feature-flag.mjs +22 -0
- package/dist/react/use-feature-flag.mjs.map +1 -0
- package/dist/server/conversions-api-error.cjs +20 -0
- package/dist/server/conversions-api-error.cjs.map +1 -0
- package/dist/server/conversions-api-error.d.cts +17 -0
- package/dist/server/conversions-api-error.d.cts.map +1 -0
- package/dist/server/conversions-api-error.d.mts +17 -0
- package/dist/server/conversions-api-error.d.mts.map +1 -0
- package/dist/server/conversions-api-error.mjs +19 -0
- package/dist/server/conversions-api-error.mjs.map +1 -0
- package/dist/server/google-data-manager.cjs +24 -26
- package/dist/server/google-data-manager.cjs.map +1 -1
- package/dist/server/google-data-manager.d.cts +13 -5
- package/dist/server/google-data-manager.d.cts.map +1 -1
- package/dist/server/google-data-manager.d.mts +13 -5
- package/dist/server/google-data-manager.d.mts.map +1 -1
- package/dist/server/google-data-manager.mjs +24 -27
- package/dist/server/google-data-manager.mjs.map +1 -1
- package/dist/server/index.cjs +11 -0
- package/dist/server/index.d.cts +10 -7
- package/dist/server/index.d.mts +10 -7
- package/dist/server/index.mjs +9 -7
- package/dist/server/linkedin-conversions-api.cjs +29 -23
- package/dist/server/linkedin-conversions-api.cjs.map +1 -1
- package/dist/server/linkedin-conversions-api.d.cts +7 -2
- package/dist/server/linkedin-conversions-api.d.cts.map +1 -1
- package/dist/server/linkedin-conversions-api.d.mts +7 -2
- package/dist/server/linkedin-conversions-api.d.mts.map +1 -1
- package/dist/server/linkedin-conversions-api.mjs +29 -24
- package/dist/server/linkedin-conversions-api.mjs.map +1 -1
- package/dist/server/meta-capi.cjs +32 -31
- package/dist/server/meta-capi.cjs.map +1 -1
- package/dist/server/meta-capi.d.cts +12 -3
- package/dist/server/meta-capi.d.cts.map +1 -1
- package/dist/server/meta-capi.d.mts +12 -3
- package/dist/server/meta-capi.d.mts.map +1 -1
- package/dist/server/meta-capi.mjs +33 -33
- package/dist/server/meta-capi.mjs.map +1 -1
- package/dist/server/microsoft-conversions-api.cjs +33 -28
- package/dist/server/microsoft-conversions-api.cjs.map +1 -1
- package/dist/server/microsoft-conversions-api.d.cts +14 -2
- package/dist/server/microsoft-conversions-api.d.cts.map +1 -1
- package/dist/server/microsoft-conversions-api.d.mts +14 -2
- package/dist/server/microsoft-conversions-api.d.mts.map +1 -1
- package/dist/server/microsoft-conversions-api.mjs +33 -29
- package/dist/server/microsoft-conversions-api.mjs.map +1 -1
- package/dist/server/openai-conversions-api.cjs +20 -18
- package/dist/server/openai-conversions-api.cjs.map +1 -1
- package/dist/server/openai-conversions-api.d.cts +7 -0
- package/dist/server/openai-conversions-api.d.cts.map +1 -1
- package/dist/server/openai-conversions-api.d.mts +7 -0
- package/dist/server/openai-conversions-api.d.mts.map +1 -1
- package/dist/server/openai-conversions-api.mjs +21 -20
- package/dist/server/openai-conversions-api.mjs.map +1 -1
- package/dist/server/reddit-conversions-api.cjs +20 -18
- package/dist/server/reddit-conversions-api.cjs.map +1 -1
- package/dist/server/reddit-conversions-api.d.cts +8 -1
- package/dist/server/reddit-conversions-api.d.cts.map +1 -1
- package/dist/server/reddit-conversions-api.d.mts +8 -1
- package/dist/server/reddit-conversions-api.d.mts.map +1 -1
- package/dist/server/reddit-conversions-api.mjs +21 -20
- package/dist/server/reddit-conversions-api.mjs.map +1 -1
- package/dist/server/setup-conversions.cjs +62 -0
- package/dist/server/setup-conversions.cjs.map +1 -0
- package/dist/server/setup-conversions.d.cts +97 -0
- package/dist/server/setup-conversions.d.cts.map +1 -0
- package/dist/server/setup-conversions.d.mts +97 -0
- package/dist/server/setup-conversions.d.mts.map +1 -0
- package/dist/server/setup-conversions.mjs +60 -0
- package/dist/server/setup-conversions.mjs.map +1 -0
- package/dist/setup/index.cjs +4 -4
- package/dist/setup/index.cjs.map +1 -1
- package/dist/setup/index.d.cts +16 -6
- package/dist/setup/index.d.cts.map +1 -1
- package/dist/setup/index.d.mts +16 -6
- package/dist/setup/index.d.mts.map +1 -1
- package/dist/setup/index.mjs +4 -4
- package/dist/setup/index.mjs.map +1 -1
- package/dist/tanstack/index.cjs +0 -2
- package/dist/tanstack/index.d.cts +1 -2
- package/dist/tanstack/index.d.mts +1 -2
- package/dist/tanstack/index.mjs +1 -3
- package/dist/third-parties/analytics-props.cjs +28 -0
- package/dist/third-parties/analytics-props.cjs.map +1 -0
- package/dist/third-parties/analytics-props.d.cts +23 -0
- package/dist/third-parties/analytics-props.d.cts.map +1 -0
- package/dist/third-parties/analytics-props.d.mts +23 -0
- package/dist/third-parties/analytics-props.d.mts.map +1 -0
- package/dist/third-parties/analytics-props.mjs +26 -0
- package/dist/third-parties/analytics-props.mjs.map +1 -0
- package/dist/third-parties/google-analytics.cjs +32 -0
- package/dist/third-parties/google-analytics.cjs.map +1 -1
- package/dist/third-parties/google-analytics.d.cts +11 -1
- package/dist/third-parties/google-analytics.d.cts.map +1 -1
- package/dist/third-parties/google-analytics.d.mts +11 -1
- package/dist/third-parties/google-analytics.d.mts.map +1 -1
- package/dist/third-parties/google-analytics.mjs +32 -1
- package/dist/third-parties/google-analytics.mjs.map +1 -1
- package/dist/third-parties/index.cjs +16 -0
- package/dist/third-parties/index.d.cts +9 -7
- package/dist/third-parties/index.d.mts +9 -7
- package/dist/third-parties/index.mjs +9 -7
- package/dist/third-parties/integration.cjs +46 -0
- package/dist/third-parties/integration.cjs.map +1 -0
- package/dist/third-parties/integration.d.cts +61 -0
- package/dist/third-parties/integration.d.cts.map +1 -0
- package/dist/third-parties/integration.d.mts +61 -0
- package/dist/third-parties/integration.d.mts.map +1 -0
- package/dist/third-parties/integration.mjs +43 -0
- package/dist/third-parties/integration.mjs.map +1 -0
- package/dist/third-parties/linkedin-insight-tag.cjs +29 -0
- package/dist/third-parties/linkedin-insight-tag.cjs.map +1 -1
- package/dist/third-parties/linkedin-insight-tag.d.cts +14 -8
- package/dist/third-parties/linkedin-insight-tag.d.cts.map +1 -1
- package/dist/third-parties/linkedin-insight-tag.d.mts +14 -8
- package/dist/third-parties/linkedin-insight-tag.d.mts.map +1 -1
- package/dist/third-parties/linkedin-insight-tag.mjs +29 -1
- package/dist/third-parties/linkedin-insight-tag.mjs.map +1 -1
- package/dist/third-parties/meta-pixel.cjs +32 -0
- package/dist/third-parties/meta-pixel.cjs.map +1 -1
- package/dist/third-parties/meta-pixel.d.cts +7 -0
- package/dist/third-parties/meta-pixel.d.cts.map +1 -1
- package/dist/third-parties/meta-pixel.d.mts +7 -0
- package/dist/third-parties/meta-pixel.d.mts.map +1 -1
- package/dist/third-parties/meta-pixel.mjs +33 -2
- package/dist/third-parties/meta-pixel.mjs.map +1 -1
- package/dist/third-parties/microsoft-uet.cjs +54 -0
- package/dist/third-parties/microsoft-uet.cjs.map +1 -1
- package/dist/third-parties/microsoft-uet.d.cts +36 -0
- package/dist/third-parties/microsoft-uet.d.cts.map +1 -1
- package/dist/third-parties/microsoft-uet.d.mts +36 -0
- package/dist/third-parties/microsoft-uet.d.mts.map +1 -1
- package/dist/third-parties/microsoft-uet.mjs +52 -1
- package/dist/third-parties/microsoft-uet.mjs.map +1 -1
- package/dist/third-parties/openai-pixel.cjs +29 -0
- package/dist/third-parties/openai-pixel.cjs.map +1 -1
- package/dist/third-parties/openai-pixel.d.cts +7 -0
- package/dist/third-parties/openai-pixel.d.cts.map +1 -1
- package/dist/third-parties/openai-pixel.d.mts +7 -0
- package/dist/third-parties/openai-pixel.d.mts.map +1 -1
- package/dist/third-parties/openai-pixel.mjs +30 -2
- package/dist/third-parties/openai-pixel.mjs.map +1 -1
- package/dist/third-parties/posthog.cjs +97 -4
- package/dist/third-parties/posthog.cjs.map +1 -1
- package/dist/third-parties/posthog.d.cts +56 -1
- package/dist/third-parties/posthog.d.cts.map +1 -1
- package/dist/third-parties/posthog.d.mts +56 -1
- package/dist/third-parties/posthog.d.mts.map +1 -1
- package/dist/third-parties/posthog.mjs +91 -2
- package/dist/third-parties/posthog.mjs.map +1 -1
- package/dist/third-parties/reddit-pixel.cjs +30 -0
- package/dist/third-parties/reddit-pixel.cjs.map +1 -1
- package/dist/third-parties/reddit-pixel.d.cts +7 -0
- package/dist/third-parties/reddit-pixel.d.cts.map +1 -1
- package/dist/third-parties/reddit-pixel.d.mts +7 -0
- package/dist/third-parties/reddit-pixel.d.mts.map +1 -1
- package/dist/third-parties/reddit-pixel.mjs +31 -2
- package/dist/third-parties/reddit-pixel.mjs.map +1 -1
- package/dist/track/fbq.cjs +6 -1
- package/dist/track/fbq.cjs.map +1 -1
- package/dist/track/fbq.d.cts +2 -0
- package/dist/track/fbq.d.cts.map +1 -1
- package/dist/track/fbq.d.mts +2 -0
- package/dist/track/fbq.d.mts.map +1 -1
- package/dist/track/fbq.mjs +6 -2
- package/dist/track/fbq.mjs.map +1 -1
- package/dist/track/index.cjs +7 -7
- package/dist/track/index.cjs.map +1 -1
- package/dist/track/index.d.cts.map +1 -1
- package/dist/track/index.d.mts.map +1 -1
- package/dist/track/index.mjs +7 -7
- package/dist/track/index.mjs.map +1 -1
- package/dist/track/oaiq.cjs +5 -0
- package/dist/track/oaiq.cjs.map +1 -1
- package/dist/track/oaiq.d.cts +2 -0
- package/dist/track/oaiq.d.cts.map +1 -1
- package/dist/track/oaiq.d.mts +2 -0
- package/dist/track/oaiq.d.mts.map +1 -1
- package/dist/track/oaiq.mjs +5 -1
- package/dist/track/oaiq.mjs.map +1 -1
- package/dist/track/rdt.cjs +5 -0
- package/dist/track/rdt.cjs.map +1 -1
- package/dist/track/rdt.d.cts +2 -0
- package/dist/track/rdt.d.cts.map +1 -1
- package/dist/track/rdt.d.mts +2 -0
- package/dist/track/rdt.d.mts.map +1 -1
- package/dist/track/rdt.mjs +5 -1
- package/dist/track/rdt.mjs.map +1 -1
- package/dist/visitor/index.cjs +3 -3
- package/dist/visitor/index.cjs.map +1 -1
- package/dist/visitor/index.mjs +3 -3
- package/dist/visitor/index.mjs.map +1 -1
- package/package.json +11 -39
- package/dist/react-router/index.cjs +0 -160
- package/dist/react-router/index.cjs.map +0 -1
- package/dist/react-router/index.d.cts +0 -22
- package/dist/react-router/index.d.cts.map +0 -1
- package/dist/react-router/index.d.mts +0 -22
- package/dist/react-router/index.d.mts.map +0 -1
- package/dist/react-router/index.mjs +0 -159
- package/dist/react-router/index.mjs.map +0 -1
- package/dist/server/legacy.cjs +0 -3
- package/dist/server/legacy.d.cts +0 -2
- package/dist/server/legacy.d.mts +0 -2
- package/dist/server/legacy.mjs +0 -1
- package/dist/server/meta-conversions-api.cjs +0 -205
- package/dist/server/meta-conversions-api.cjs.map +0 -1
- package/dist/server/meta-conversions-api.d.cts +0 -8
- package/dist/server/meta-conversions-api.d.cts.map +0 -1
- package/dist/server/meta-conversions-api.d.mts +0 -8
- package/dist/server/meta-conversions-api.d.mts.map +0 -1
- package/dist/server/meta-conversions-api.mjs +0 -201
- package/dist/server/meta-conversions-api.mjs.map +0 -1
- package/dist/tanstack/analytics.cjs +0 -184
- package/dist/tanstack/analytics.cjs.map +0 -1
- package/dist/tanstack/analytics.d.cts +0 -33
- package/dist/tanstack/analytics.d.cts.map +0 -1
- package/dist/tanstack/analytics.d.mts +0 -33
- package/dist/tanstack/analytics.d.mts.map +0 -1
- package/dist/tanstack/analytics.mjs +0 -183
- package/dist/tanstack/analytics.mjs.map +0 -1
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { Environment, EventName, Platform, TrackEvent, TrackName, TrackProperties, TrackTags, UserProvidedData } from "../track/types.mjs";
|
|
2
|
+
type AnyTrackEvent = TrackEvent<any>;
|
|
3
|
+
/**
|
|
4
|
+
* Where `setupConversions` sends events: an ad platform's conversions API, from its factory
|
|
5
|
+
* (`metaConversions`, … next to each sender), or anything else the host forwards events to (a
|
|
6
|
+
* CRM, its own workflows).
|
|
7
|
+
*
|
|
8
|
+
* `send` rejects when it fails, as the SDK's senders do: `setupConversions` logs the failure,
|
|
9
|
+
* with the events' ids, and still sends to the others.
|
|
10
|
+
*/
|
|
11
|
+
export interface ConversionDestination {
|
|
12
|
+
readonly send: (events: AnyTrackEvent[], data: UserProvidedData) => Promise<unknown>;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* A destination that sends only the events `accepts` lets through, and makes no request when none
|
|
16
|
+
* is left: what each conversions API's factory (`metaConversions`, …) is built on.
|
|
17
|
+
*/
|
|
18
|
+
export declare function conversionDestination(accepts: (name: string) => boolean, send: (events: AnyTrackEvent[], data: UserProvidedData) => Promise<unknown>): ConversionDestination;
|
|
19
|
+
/**
|
|
20
|
+
* Who a server-only event is from, as the host knows them: what the ad platforms match a client
|
|
21
|
+
* event on, read from the host's own records.
|
|
22
|
+
*
|
|
23
|
+
* - `tags`: the user's latest session's tags (their cookies — `_fbp`, `_rdt_uuid`, … — page
|
|
24
|
+
* and user agent), with the click ids of the touch the conversion is attributed to over them.
|
|
25
|
+
* - `visitor_id`, `session_id`, `platform`, `environment`: from that session. `platform` decides
|
|
26
|
+
* each platform's action source (website or app), as for a client event.
|
|
27
|
+
* - `data`: the user's identity (`email`, `user_id`, …), hashed by each sender.
|
|
28
|
+
*/
|
|
29
|
+
export interface ConversionUser {
|
|
30
|
+
tags: TrackTags;
|
|
31
|
+
visitor_id: string;
|
|
32
|
+
session_id: string;
|
|
33
|
+
platform: Platform;
|
|
34
|
+
environment: Environment;
|
|
35
|
+
data: UserProvidedData;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* An event the server reports on its own, with no client behind it: a webhook's purchase,
|
|
39
|
+
* renewal or refund. `id` is the transaction id: `track.store` keeps a redelivered webhook from
|
|
40
|
+
* being reported twice, and the platforms pair it with a browser event under the same id.
|
|
41
|
+
*/
|
|
42
|
+
export interface ServerEvent<T extends EventName = EventName> {
|
|
43
|
+
id: string;
|
|
44
|
+
name: TrackName<T>;
|
|
45
|
+
/** The user it belongs to, handed to `track.resolveUser`. */
|
|
46
|
+
userId: string;
|
|
47
|
+
properties?: TrackProperties<T>;
|
|
48
|
+
/** When it happened, as an ISO 8601 string. */
|
|
49
|
+
createdAt: string;
|
|
50
|
+
}
|
|
51
|
+
export interface ConversionsOptions {
|
|
52
|
+
destinations: readonly ConversionDestination[];
|
|
53
|
+
/**
|
|
54
|
+
* How `track` completes and stores a server-only event. Without it `setupConversions` returns
|
|
55
|
+
* only `forward`, for a host that only forwards client events.
|
|
56
|
+
*/
|
|
57
|
+
track?: {
|
|
58
|
+
/**
|
|
59
|
+
* The user behind the event, read from the host's records (see `ConversionUser`);
|
|
60
|
+
* `undefined` for one the host has no session of, whose event is then dropped.
|
|
61
|
+
*/
|
|
62
|
+
resolveUser: (userId: string) => Promise<ConversionUser | undefined>;
|
|
63
|
+
/**
|
|
64
|
+
* Stores the event, e.g. in the events table, returning whether it was new: `track` sends
|
|
65
|
+
* only an event it stored, so a webhook delivered again (its handler failed on something
|
|
66
|
+
* else) is not reported twice — not every platform deduplicates a server event sent twice.
|
|
67
|
+
*/
|
|
68
|
+
store: (event: AnyTrackEvent) => Promise<boolean>;
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* The server's counterpart of `setupAnalytics({ integrations })`: every destination configured
|
|
73
|
+
* once, with the same per-platform options as the browser integrations, so the conversions API
|
|
74
|
+
* and the tag report the same events.
|
|
75
|
+
*
|
|
76
|
+
* - `forward(events, data)`: the events a client sent, as stored, on to every destination.
|
|
77
|
+
* - `track(event)`: an event the server reports on its own (a webhook's), completed by
|
|
78
|
+
* `track.resolveUser`, stored with `track.store`, then forwarded.
|
|
79
|
+
*
|
|
80
|
+
* Neither throws, so reporting a conversion can never fail the request it runs in — a payment
|
|
81
|
+
* webhook least of all, which its provider would then deliver again and again. A failure is
|
|
82
|
+
* logged with the ids of the events it lost, for them to be found and sent later: a missed
|
|
83
|
+
* conversion costs less than a duplicate one, or a webhook that keeps failing.
|
|
84
|
+
*/
|
|
85
|
+
export declare function setupConversions(options: ConversionsOptions & {
|
|
86
|
+
track: NonNullable<ConversionsOptions['track']>;
|
|
87
|
+
}): {
|
|
88
|
+
forward: Forward;
|
|
89
|
+
track: Track;
|
|
90
|
+
};
|
|
91
|
+
export declare function setupConversions(options: ConversionsOptions): {
|
|
92
|
+
forward: Forward;
|
|
93
|
+
};
|
|
94
|
+
type Forward = (events: AnyTrackEvent[], data?: UserProvidedData) => Promise<void>;
|
|
95
|
+
type Track = <T extends EventName>(input: ServerEvent<T>) => Promise<TrackEvent<T> | undefined>;
|
|
96
|
+
export {};
|
|
97
|
+
//# sourceMappingURL=setup-conversions.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"setup-conversions.d.mts","names":[],"sources":["../../src/server/setup-conversions.ts"],"mappings":";KAYK,gBAAgB;;;;;;;;;iBAUJ;WACN,OAAO,QAAQ,iBAAiB,MAAM,qBAAqB;;;;;;wBAOtD,sBACd,UAAU,0BACV,OAAO,QAAQ,iBAAiB,MAAM,qBAAqB,mBAC1D;;;;;;;;;;;iBAmBc;EACf,MAAM;EACN;EACA;EACA,UAAU;EACV,aAAa;EACb,MAAM;;;;;;;iBAQS,YAAY,UAAU,YAAY;EACjD;EACA,MAAM,UAAU;;EAEhB;EACA,aAAa,gBAAgB;;EAE7B;;iBAGe;EACf,uBAAuB;;;;;EAKvB;;;;;IAKE,cAAc,mBAAmB,QAAQ;;;;;;IAMzC,QAAQ,OAAO,kBAAkB;;;;;;;;;;;;;;;;;wBAkBrB,iBACd,SAAS;EAAuB,OAAO,YAAY;;EAEnD,SAAS;EACT,OAAO;;wBAEO,iBAAiB,SAAS;EAAuB,SAAS;;KAyDrE,WAAW,QAAQ,iBAAiB,OAAO,qBAAqB;KAChE,SAAS,UAAU,WAAW,OAAO,YAAY,OAAO,QAAQ,WAAW"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// src/server/setup-conversions.ts
|
|
2
|
+
/**
|
|
3
|
+
* A destination that sends only the events `accepts` lets through, and makes no request when none
|
|
4
|
+
* is left: what each conversions API's factory (`metaConversions`, …) is built on.
|
|
5
|
+
*/
|
|
6
|
+
function conversionDestination(accepts, send) {
|
|
7
|
+
return { send: async (events, data) => {
|
|
8
|
+
const accepted = events.filter((event) => accepts(event.name));
|
|
9
|
+
if (accepted.length > 0) await send(accepted, data);
|
|
10
|
+
} };
|
|
11
|
+
}
|
|
12
|
+
function setupConversions(options) {
|
|
13
|
+
const { destinations, track: trackOptions } = options;
|
|
14
|
+
async function forward(events, data = {}) {
|
|
15
|
+
if (events.length === 0) return;
|
|
16
|
+
const settled = await Promise.allSettled(destinations.map((d) => Promise.resolve().then(() => d.send(events, data))));
|
|
17
|
+
const ids = events.map((event) => event.id).join(", ");
|
|
18
|
+
settled.forEach((outcome) => {
|
|
19
|
+
if (outcome.status === "rejected") console.error(`Conversions not sent, events: ${ids}`, outcome.reason);
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Reports a server-only event; resolves with it as sent, or `undefined` when it was not: a
|
|
24
|
+
* user with no session, an event `store` had already stored, or a failure, which is logged.
|
|
25
|
+
*/
|
|
26
|
+
async function track({ resolveUser, store }, input) {
|
|
27
|
+
try {
|
|
28
|
+
const user = await resolveUser(input.userId);
|
|
29
|
+
if (!user) {
|
|
30
|
+
console.warn(`Conversion not reported, event: ${input.id}: no session for ${input.userId}`);
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
const event = {
|
|
34
|
+
id: input.id,
|
|
35
|
+
name: input.name,
|
|
36
|
+
properties: input.properties,
|
|
37
|
+
tags: user.tags,
|
|
38
|
+
visitor_id: user.visitor_id,
|
|
39
|
+
session_id: user.session_id,
|
|
40
|
+
platform: user.platform,
|
|
41
|
+
environment: user.environment,
|
|
42
|
+
created_at: input.createdAt
|
|
43
|
+
};
|
|
44
|
+
if (!await store(event)) return void 0;
|
|
45
|
+
await forward([event], user.data);
|
|
46
|
+
return event;
|
|
47
|
+
} catch (e) {
|
|
48
|
+
console.error(`Conversion not reported, event: ${input.id}`, e);
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
if (!trackOptions) return { forward };
|
|
53
|
+
return {
|
|
54
|
+
forward,
|
|
55
|
+
track: (input) => track(trackOptions, input)
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
export { conversionDestination, setupConversions };
|
|
59
|
+
|
|
60
|
+
//# sourceMappingURL=setup-conversions.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"setup-conversions.mjs","names":[],"sources":["../../src/server/setup-conversions.ts"],"sourcesContent":["import type {\n Environment,\n EventName,\n Platform,\n TrackEvent,\n TrackName,\n TrackProperties,\n TrackTags,\n UserProvidedData,\n} from '../track/types';\n\n// oxlint-disable-next-line @typescript-eslint/no-explicit-any\ntype AnyTrackEvent = TrackEvent<any>;\n\n/**\n * Where `setupConversions` sends events: an ad platform's conversions API, from its factory\n * (`metaConversions`, … next to each sender), or anything else the host forwards events to (a\n * CRM, its own workflows).\n *\n * `send` rejects when it fails, as the SDK's senders do: `setupConversions` logs the failure,\n * with the events' ids, and still sends to the others.\n */\nexport interface ConversionDestination {\n readonly send: (events: AnyTrackEvent[], data: UserProvidedData) => Promise<unknown>;\n}\n\n/**\n * A destination that sends only the events `accepts` lets through, and makes no request when none\n * is left: what each conversions API's factory (`metaConversions`, …) is built on.\n */\nexport function conversionDestination(\n accepts: (name: string) => boolean,\n send: (events: AnyTrackEvent[], data: UserProvidedData) => Promise<unknown>\n): ConversionDestination {\n return {\n send: async (events, data) => {\n const accepted = events.filter((event) => accepts(event.name));\n if (accepted.length > 0) await send(accepted, data);\n },\n };\n}\n\n/**\n * Who a server-only event is from, as the host knows them: what the ad platforms match a client\n * event on, read from the host's own records.\n *\n * - `tags`: the user's latest session's tags (their cookies — `_fbp`, `_rdt_uuid`, … — page\n * and user agent), with the click ids of the touch the conversion is attributed to over them.\n * - `visitor_id`, `session_id`, `platform`, `environment`: from that session. `platform` decides\n * each platform's action source (website or app), as for a client event.\n * - `data`: the user's identity (`email`, `user_id`, …), hashed by each sender.\n */\nexport interface ConversionUser {\n tags: TrackTags;\n visitor_id: string;\n session_id: string;\n platform: Platform;\n environment: Environment;\n data: UserProvidedData;\n}\n\n/**\n * An event the server reports on its own, with no client behind it: a webhook's purchase,\n * renewal or refund. `id` is the transaction id: `track.store` keeps a redelivered webhook from\n * being reported twice, and the platforms pair it with a browser event under the same id.\n */\nexport interface ServerEvent<T extends EventName = EventName> {\n id: string;\n name: TrackName<T>;\n /** The user it belongs to, handed to `track.resolveUser`. */\n userId: string;\n properties?: TrackProperties<T>;\n /** When it happened, as an ISO 8601 string. */\n createdAt: string;\n}\n\nexport interface ConversionsOptions {\n destinations: readonly ConversionDestination[];\n /**\n * How `track` completes and stores a server-only event. Without it `setupConversions` returns\n * only `forward`, for a host that only forwards client events.\n */\n track?: {\n /**\n * The user behind the event, read from the host's records (see `ConversionUser`);\n * `undefined` for one the host has no session of, whose event is then dropped.\n */\n resolveUser: (userId: string) => Promise<ConversionUser | undefined>;\n /**\n * Stores the event, e.g. in the events table, returning whether it was new: `track` sends\n * only an event it stored, so a webhook delivered again (its handler failed on something\n * else) is not reported twice — not every platform deduplicates a server event sent twice.\n */\n store: (event: AnyTrackEvent) => Promise<boolean>;\n };\n}\n\n/**\n * The server's counterpart of `setupAnalytics({ integrations })`: every destination configured\n * once, with the same per-platform options as the browser integrations, so the conversions API\n * and the tag report the same events.\n *\n * - `forward(events, data)`: the events a client sent, as stored, on to every destination.\n * - `track(event)`: an event the server reports on its own (a webhook's), completed by\n * `track.resolveUser`, stored with `track.store`, then forwarded.\n *\n * Neither throws, so reporting a conversion can never fail the request it runs in — a payment\n * webhook least of all, which its provider would then deliver again and again. A failure is\n * logged with the ids of the events it lost, for them to be found and sent later: a missed\n * conversion costs less than a duplicate one, or a webhook that keeps failing.\n */\nexport function setupConversions(\n options: ConversionsOptions & { track: NonNullable<ConversionsOptions['track']> }\n): {\n forward: Forward;\n track: Track;\n};\nexport function setupConversions(options: ConversionsOptions): { forward: Forward };\nexport function setupConversions(options: ConversionsOptions) {\n const { destinations, track: trackOptions } = options;\n\n async function forward(events: AnyTrackEvent[], data: UserProvidedData = {}) {\n if (events.length === 0) return;\n // Called inside a promise: a `send` that throws synchronously rejects there instead of\n // stopping the destinations after it.\n const settled = await Promise.allSettled(\n destinations.map((d) => Promise.resolve().then(() => d.send(events, data)))\n );\n const ids = events.map((event) => event.id).join(', ');\n settled.forEach((outcome) => {\n if (outcome.status === 'rejected') {\n console.error(`Conversions not sent, events: ${ids}`, outcome.reason);\n }\n });\n }\n\n /**\n * Reports a server-only event; resolves with it as sent, or `undefined` when it was not: a\n * user with no session, an event `store` had already stored, or a failure, which is logged.\n */\n async function track<T extends EventName>(\n { resolveUser, store }: NonNullable<ConversionsOptions['track']>,\n input: ServerEvent<T>\n ): Promise<TrackEvent<T> | undefined> {\n try {\n const user = await resolveUser(input.userId);\n if (!user) {\n console.warn(`Conversion not reported, event: ${input.id}: no session for ${input.userId}`);\n return undefined;\n }\n const event: TrackEvent<T> = {\n id: input.id,\n name: input.name,\n properties: input.properties,\n tags: user.tags,\n visitor_id: user.visitor_id,\n session_id: user.session_id,\n platform: user.platform,\n environment: user.environment,\n created_at: input.createdAt,\n };\n if (!(await store(event))) return undefined;\n await forward([event], user.data);\n return event;\n } catch (e: unknown) {\n console.error(`Conversion not reported, event: ${input.id}`, e);\n return undefined;\n }\n }\n\n if (!trackOptions) return { forward };\n return { forward, track: (input: ServerEvent) => track(trackOptions, input) };\n}\n\ntype Forward = (events: AnyTrackEvent[], data?: UserProvidedData) => Promise<void>;\ntype Track = <T extends EventName>(input: ServerEvent<T>) => Promise<TrackEvent<T> | undefined>;\n"],"mappings":";;;;;AA8BA,SAAgB,sBACd,SACA,MACuB;CACvB,OAAO,EACL,MAAM,OAAO,QAAQ,SAAS;EAC5B,MAAM,WAAW,OAAO,QAAQ,UAAU,QAAQ,MAAM,IAAI,CAAC;EAC7D,IAAI,SAAS,SAAS,GAAG,MAAM,KAAK,UAAU,IAAI;CACpD,EACF;AACF;AA8EA,SAAgB,iBAAiB,SAA6B;CAC5D,MAAM,EAAE,cAAc,OAAO,iBAAiB;CAE9C,eAAe,QAAQ,QAAyB,OAAyB,CAAC,GAAG;EAC3E,IAAI,OAAO,WAAW,GAAG;EAGzB,MAAM,UAAU,MAAM,QAAQ,WAC5B,aAAa,KAAK,MAAM,QAAQ,QAAQ,CAAC,CAAC,WAAW,EAAE,KAAK,QAAQ,IAAI,CAAC,CAAC,CAC5E;EACA,MAAM,MAAM,OAAO,KAAK,UAAU,MAAM,EAAE,CAAC,CAAC,KAAK,IAAI;EACrD,QAAQ,SAAS,YAAY;GAC3B,IAAI,QAAQ,WAAW,YACrB,QAAQ,MAAM,iCAAiC,OAAO,QAAQ,MAAM;EAExE,CAAC;CACH;;;;;CAMA,eAAe,MACb,EAAE,aAAa,SACf,OACoC;EACpC,IAAI;GACF,MAAM,OAAO,MAAM,YAAY,MAAM,MAAM;GAC3C,IAAI,CAAC,MAAM;IACT,QAAQ,KAAK,mCAAmC,MAAM,GAAG,mBAAmB,MAAM,QAAQ;IAC1F;GACF;GACA,MAAM,QAAuB;IAC3B,IAAI,MAAM;IACV,MAAM,MAAM;IACZ,YAAY,MAAM;IAClB,MAAM,KAAK;IACX,YAAY,KAAK;IACjB,YAAY,KAAK;IACjB,UAAU,KAAK;IACf,aAAa,KAAK;IAClB,YAAY,MAAM;GACpB;GACA,IAAI,CAAE,MAAM,MAAM,KAAK,GAAI,OAAO,KAAA;GAClC,MAAM,QAAQ,CAAC,KAAK,GAAG,KAAK,IAAI;GAChC,OAAO;EACT,SAAS,GAAY;GACnB,QAAQ,MAAM,mCAAmC,MAAM,MAAM,CAAC;GAC9D;EACF;CACF;CAEA,IAAI,CAAC,cAAc,OAAO,EAAE,QAAQ;CACpC,OAAO;EAAE;EAAS,QAAQ,UAAuB,MAAM,cAAc,KAAK;CAAE;AAC9E"}
|
package/dist/setup/index.cjs
CHANGED
|
@@ -10,8 +10,8 @@ const config = {
|
|
|
10
10
|
getTags: null,
|
|
11
11
|
getDeviceId: null,
|
|
12
12
|
getHeaders: null,
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
integrations: [],
|
|
14
|
+
excludeFromIntegrations: [],
|
|
15
15
|
deepLink: void 0
|
|
16
16
|
};
|
|
17
17
|
function setupAnalytics(init) {
|
|
@@ -26,8 +26,8 @@ function setupAnalytics(init) {
|
|
|
26
26
|
"Content-Type": "application/json",
|
|
27
27
|
...await init.getHeaders?.()
|
|
28
28
|
});
|
|
29
|
-
config.
|
|
30
|
-
config.
|
|
29
|
+
config.integrations = init.integrations ?? [];
|
|
30
|
+
config.excludeFromIntegrations = init.excludeFromIntegrations ?? [];
|
|
31
31
|
config.deepLink = init.deepLink;
|
|
32
32
|
try {
|
|
33
33
|
init.deepLink?.listen();
|
package/dist/setup/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":[],"sources":["../../src/setup/index.ts"],"sourcesContent":["import type {
|
|
1
|
+
{"version":3,"file":"index.cjs","names":[],"sources":["../../src/setup/index.ts"],"sourcesContent":["import type { Integration } from '../third-parties/integration';\nimport type { Environment, EventName, Platform, TrackTags } from '../track/types';\n\nexport interface Storage {\n getItem: (key: string) => string | null;\n setItem: (key: string, value: string) => void;\n}\n\n/**\n * The link that opened the current visit of an app — a universal link, an app link, a custom\n * scheme — whose URL, utm and click ids land the visit as a web page's URL does.\n * `@shware/analytics/native` provides one (`deepLink`).\n */\nexport interface DeepLink {\n /** Starts following the links that open the app; `setupAnalytics` calls it once. */\n listen: () => void;\n /** The opening link's tags (`page_location`, utm, click ids); empty without one. */\n getTags: () => TrackTags | Promise<TrackTags>;\n}\n\nexport interface Options {\n release: string;\n storage: Storage;\n endpoint: string;\n platform: Platform;\n environment: Environment;\n getTags: () => TrackTags | Promise<TrackTags>;\n getDeviceId: () => string | Promise<string>;\n getHeaders?: () => Record<string, string> | Promise<Record<string, string>>;\n /**\n * The third parties events and the signed-in user go to, each configured in one place\n * (`googleAnalytics`, `metaAds`, … from `@shware/analytics/third-parties`). Pass the same array\n * to `<Analytics integrations>` for their scripts.\n */\n integrations?: readonly Integration[];\n /**\n * Events reported server-side only (the Conversions APIs, Google's Data Manager API): stored\n * like any event, but handed to no browser tracker, analytics included — a browser copy under\n * another event id would be counted twice by the platforms. `purchase` and `refund`, typically.\n */\n excludeFromIntegrations?: readonly EventName[];\n /** An app's opening link, which native's `getTags` merges into every event's tags. */\n deepLink?: DeepLink;\n}\n\ninterface Config {\n release: string;\n endpoint: string;\n storage: Storage;\n platform: Platform;\n environment: Environment;\n getTags: () => TrackTags | Promise<TrackTags>;\n getDeviceId: () => string | Promise<string>;\n getHeaders: () => Record<string, string> | Promise<Record<string, string>>;\n integrations: readonly Integration[];\n excludeFromIntegrations: readonly string[];\n deepLink: DeepLink | undefined;\n}\n\ninterface Cache {\n tags: TrackTags | null;\n}\n\nexport const cache: Cache = {\n tags: null,\n};\n\n// oxlint-disable typescript/no-non-null-assertion\nexport const config: Config = {\n endpoint: '',\n release: '0.0.0',\n storage: null!,\n platform: null!,\n environment: null!,\n getTags: null!,\n getDeviceId: null!,\n getHeaders: null!,\n integrations: [],\n excludeFromIntegrations: [],\n deepLink: undefined,\n};\n// oxlint-enable typescript/no-non-null-assertion\n\nexport function setupAnalytics(init: Options) {\n config.release = init.release;\n config.storage = init.storage;\n config.platform = init.platform;\n config.environment = init.environment;\n config.endpoint = init.endpoint.endsWith('/') ? init.endpoint.slice(0, -1) : init.endpoint;\n config.getTags = init.getTags;\n config.getDeviceId = init.getDeviceId;\n config.getHeaders = async () => ({\n 'Content-Type': 'application/json',\n ...(await init.getHeaders?.()),\n });\n config.integrations = init.integrations ?? [];\n config.excludeFromIntegrations = init.excludeFromIntegrations ?? [];\n config.deepLink = init.deepLink;\n // Logged, not thrown: the links are worth less than the setup they would break.\n try {\n init.deepLink?.listen();\n } catch (error) {\n console.error('analytics deepLink.listen failed', error);\n }\n}\n"],"mappings":";;AA+DA,MAAa,QAAe,EAC1B,MAAM,KACR;AAGA,MAAa,SAAiB;CAC5B,UAAU;CACV,SAAS;CACT,SAAS;CACT,UAAU;CACV,aAAa;CACb,SAAS;CACT,aAAa;CACb,YAAY;CACZ,cAAc,CAAC;CACf,yBAAyB,CAAC;CAC1B,UAAU,KAAA;AACZ;AAGA,SAAgB,eAAe,MAAe;CAC5C,OAAO,UAAU,KAAK;CACtB,OAAO,UAAU,KAAK;CACtB,OAAO,WAAW,KAAK;CACvB,OAAO,cAAc,KAAK;CAC1B,OAAO,WAAW,KAAK,SAAS,SAAS,GAAG,IAAI,KAAK,SAAS,MAAM,GAAG,EAAE,IAAI,KAAK;CAClF,OAAO,UAAU,KAAK;CACtB,OAAO,cAAc,KAAK;CAC1B,OAAO,aAAa,aAAa;EAC/B,gBAAgB;EAChB,GAAI,MAAM,KAAK,aAAa;CAC9B;CACA,OAAO,eAAe,KAAK,gBAAgB,CAAC;CAC5C,OAAO,0BAA0B,KAAK,2BAA2B,CAAC;CAClE,OAAO,WAAW,KAAK;CAEvB,IAAI;EACF,KAAK,UAAU,OAAO;CACxB,SAAS,OAAO;EACd,QAAQ,MAAM,oCAAoC,KAAK;CACzD;AACF"}
|
package/dist/setup/index.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Environment,
|
|
2
|
-
import {
|
|
1
|
+
import { Environment, EventName, Platform, TrackTags } from "../track/types.cjs";
|
|
2
|
+
import { Integration } from "../third-parties/integration.cjs";
|
|
3
3
|
export interface Storage {
|
|
4
4
|
getItem: (key: string) => string | null;
|
|
5
5
|
setItem: (key: string, value: string) => void;
|
|
@@ -24,8 +24,18 @@ export interface Options {
|
|
|
24
24
|
getTags: () => TrackTags | Promise<TrackTags>;
|
|
25
25
|
getDeviceId: () => string | Promise<string>;
|
|
26
26
|
getHeaders?: () => Record<string, string> | Promise<Record<string, string>>;
|
|
27
|
-
|
|
28
|
-
|
|
27
|
+
/**
|
|
28
|
+
* The third parties events and the signed-in user go to, each configured in one place
|
|
29
|
+
* (`googleAnalytics`, `metaAds`, … from `@shware/analytics/third-parties`). Pass the same array
|
|
30
|
+
* to `<Analytics integrations>` for their scripts.
|
|
31
|
+
*/
|
|
32
|
+
integrations?: readonly Integration[];
|
|
33
|
+
/**
|
|
34
|
+
* Events reported server-side only (the Conversions APIs, Google's Data Manager API): stored
|
|
35
|
+
* like any event, but handed to no browser tracker, analytics included — a browser copy under
|
|
36
|
+
* another event id would be counted twice by the platforms. `purchase` and `refund`, typically.
|
|
37
|
+
*/
|
|
38
|
+
excludeFromIntegrations?: readonly EventName[];
|
|
29
39
|
/** An app's opening link, which native's `getTags` merges into every event's tags. */
|
|
30
40
|
deepLink?: DeepLink;
|
|
31
41
|
}
|
|
@@ -38,8 +48,8 @@ interface Config {
|
|
|
38
48
|
getTags: () => TrackTags | Promise<TrackTags>;
|
|
39
49
|
getDeviceId: () => string | Promise<string>;
|
|
40
50
|
getHeaders: () => Record<string, string> | Promise<Record<string, string>>;
|
|
41
|
-
|
|
42
|
-
|
|
51
|
+
integrations: readonly Integration[];
|
|
52
|
+
excludeFromIntegrations: readonly string[];
|
|
43
53
|
deepLink: DeepLink | undefined;
|
|
44
54
|
}
|
|
45
55
|
interface Cache {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.cts","names":[],"sources":["../../src/setup/index.ts"],"mappings":";;iBAGiB;EACf,UAAU;EACV,UAAU,aAAa;;;;;;;iBAQR;;EAEf;;EAEA,eAAe,YAAY,QAAQ;;iBAGpB;EACf;EACA,SAAS;EACT;EACA,UAAU;EACV,aAAa;EACb,eAAe,YAAY,QAAQ;EACnC,4BAA4B;EAC5B,mBAAmB,yBAAyB,QAAQ
|
|
1
|
+
{"version":3,"file":"index.d.cts","names":[],"sources":["../../src/setup/index.ts"],"mappings":";;iBAGiB;EACf,UAAU;EACV,UAAU,aAAa;;;;;;;iBAQR;;EAEf;;EAEA,eAAe,YAAY,QAAQ;;iBAGpB;EACf;EACA,SAAS;EACT;EACA,UAAU;EACV,aAAa;EACb,eAAe,YAAY,QAAQ;EACnC,4BAA4B;EAC5B,mBAAmB,yBAAyB,QAAQ;;;;;;EAMpD,wBAAwB;;;;;;EAMxB,mCAAmC;;EAEnC,WAAW;;UAGH;EACR;EACA;EACA,SAAS;EACT,UAAU;EACV,aAAa;EACb,eAAe,YAAY,QAAQ;EACnC,4BAA4B;EAC5B,kBAAkB,yBAAyB,QAAQ;EACnD,uBAAuB;EACvB;EACA,UAAU;;UAGF;EACR,MAAM;;qBAGK,OAAO;qBAKP,QAAQ;wBAeL,eAAe,MAAM"}
|
package/dist/setup/index.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Environment,
|
|
2
|
-
import {
|
|
1
|
+
import { Environment, EventName, Platform, TrackTags } from "../track/types.mjs";
|
|
2
|
+
import { Integration } from "../third-parties/integration.mjs";
|
|
3
3
|
export interface Storage {
|
|
4
4
|
getItem: (key: string) => string | null;
|
|
5
5
|
setItem: (key: string, value: string) => void;
|
|
@@ -24,8 +24,18 @@ export interface Options {
|
|
|
24
24
|
getTags: () => TrackTags | Promise<TrackTags>;
|
|
25
25
|
getDeviceId: () => string | Promise<string>;
|
|
26
26
|
getHeaders?: () => Record<string, string> | Promise<Record<string, string>>;
|
|
27
|
-
|
|
28
|
-
|
|
27
|
+
/**
|
|
28
|
+
* The third parties events and the signed-in user go to, each configured in one place
|
|
29
|
+
* (`googleAnalytics`, `metaAds`, … from `@shware/analytics/third-parties`). Pass the same array
|
|
30
|
+
* to `<Analytics integrations>` for their scripts.
|
|
31
|
+
*/
|
|
32
|
+
integrations?: readonly Integration[];
|
|
33
|
+
/**
|
|
34
|
+
* Events reported server-side only (the Conversions APIs, Google's Data Manager API): stored
|
|
35
|
+
* like any event, but handed to no browser tracker, analytics included — a browser copy under
|
|
36
|
+
* another event id would be counted twice by the platforms. `purchase` and `refund`, typically.
|
|
37
|
+
*/
|
|
38
|
+
excludeFromIntegrations?: readonly EventName[];
|
|
29
39
|
/** An app's opening link, which native's `getTags` merges into every event's tags. */
|
|
30
40
|
deepLink?: DeepLink;
|
|
31
41
|
}
|
|
@@ -38,8 +48,8 @@ interface Config {
|
|
|
38
48
|
getTags: () => TrackTags | Promise<TrackTags>;
|
|
39
49
|
getDeviceId: () => string | Promise<string>;
|
|
40
50
|
getHeaders: () => Record<string, string> | Promise<Record<string, string>>;
|
|
41
|
-
|
|
42
|
-
|
|
51
|
+
integrations: readonly Integration[];
|
|
52
|
+
excludeFromIntegrations: readonly string[];
|
|
43
53
|
deepLink: DeepLink | undefined;
|
|
44
54
|
}
|
|
45
55
|
interface Cache {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","names":[],"sources":["../../src/setup/index.ts"],"mappings":";;iBAGiB;EACf,UAAU;EACV,UAAU,aAAa;;;;;;;iBAQR;;EAEf;;EAEA,eAAe,YAAY,QAAQ;;iBAGpB;EACf;EACA,SAAS;EACT;EACA,UAAU;EACV,aAAa;EACb,eAAe,YAAY,QAAQ;EACnC,4BAA4B;EAC5B,mBAAmB,yBAAyB,QAAQ
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../../src/setup/index.ts"],"mappings":";;iBAGiB;EACf,UAAU;EACV,UAAU,aAAa;;;;;;;iBAQR;;EAEf;;EAEA,eAAe,YAAY,QAAQ;;iBAGpB;EACf;EACA,SAAS;EACT;EACA,UAAU;EACV,aAAa;EACb,eAAe,YAAY,QAAQ;EACnC,4BAA4B;EAC5B,mBAAmB,yBAAyB,QAAQ;;;;;;EAMpD,wBAAwB;;;;;;EAMxB,mCAAmC;;EAEnC,WAAW;;UAGH;EACR;EACA;EACA,SAAS;EACT,UAAU;EACV,aAAa;EACb,eAAe,YAAY,QAAQ;EACnC,4BAA4B;EAC5B,kBAAkB,yBAAyB,QAAQ;EACnD,uBAAuB;EACvB;EACA,UAAU;;UAGF;EACR,MAAM;;qBAGK,OAAO;qBAKP,QAAQ;wBAeL,eAAe,MAAM"}
|
package/dist/setup/index.mjs
CHANGED
|
@@ -9,8 +9,8 @@ const config = {
|
|
|
9
9
|
getTags: null,
|
|
10
10
|
getDeviceId: null,
|
|
11
11
|
getHeaders: null,
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
integrations: [],
|
|
13
|
+
excludeFromIntegrations: [],
|
|
14
14
|
deepLink: void 0
|
|
15
15
|
};
|
|
16
16
|
function setupAnalytics(init) {
|
|
@@ -25,8 +25,8 @@ function setupAnalytics(init) {
|
|
|
25
25
|
"Content-Type": "application/json",
|
|
26
26
|
...await init.getHeaders?.()
|
|
27
27
|
});
|
|
28
|
-
config.
|
|
29
|
-
config.
|
|
28
|
+
config.integrations = init.integrations ?? [];
|
|
29
|
+
config.excludeFromIntegrations = init.excludeFromIntegrations ?? [];
|
|
30
30
|
config.deepLink = init.deepLink;
|
|
31
31
|
try {
|
|
32
32
|
init.deepLink?.listen();
|
package/dist/setup/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":[],"sources":["../../src/setup/index.ts"],"sourcesContent":["import type {
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../../src/setup/index.ts"],"sourcesContent":["import type { Integration } from '../third-parties/integration';\nimport type { Environment, EventName, Platform, TrackTags } from '../track/types';\n\nexport interface Storage {\n getItem: (key: string) => string | null;\n setItem: (key: string, value: string) => void;\n}\n\n/**\n * The link that opened the current visit of an app — a universal link, an app link, a custom\n * scheme — whose URL, utm and click ids land the visit as a web page's URL does.\n * `@shware/analytics/native` provides one (`deepLink`).\n */\nexport interface DeepLink {\n /** Starts following the links that open the app; `setupAnalytics` calls it once. */\n listen: () => void;\n /** The opening link's tags (`page_location`, utm, click ids); empty without one. */\n getTags: () => TrackTags | Promise<TrackTags>;\n}\n\nexport interface Options {\n release: string;\n storage: Storage;\n endpoint: string;\n platform: Platform;\n environment: Environment;\n getTags: () => TrackTags | Promise<TrackTags>;\n getDeviceId: () => string | Promise<string>;\n getHeaders?: () => Record<string, string> | Promise<Record<string, string>>;\n /**\n * The third parties events and the signed-in user go to, each configured in one place\n * (`googleAnalytics`, `metaAds`, … from `@shware/analytics/third-parties`). Pass the same array\n * to `<Analytics integrations>` for their scripts.\n */\n integrations?: readonly Integration[];\n /**\n * Events reported server-side only (the Conversions APIs, Google's Data Manager API): stored\n * like any event, but handed to no browser tracker, analytics included — a browser copy under\n * another event id would be counted twice by the platforms. `purchase` and `refund`, typically.\n */\n excludeFromIntegrations?: readonly EventName[];\n /** An app's opening link, which native's `getTags` merges into every event's tags. */\n deepLink?: DeepLink;\n}\n\ninterface Config {\n release: string;\n endpoint: string;\n storage: Storage;\n platform: Platform;\n environment: Environment;\n getTags: () => TrackTags | Promise<TrackTags>;\n getDeviceId: () => string | Promise<string>;\n getHeaders: () => Record<string, string> | Promise<Record<string, string>>;\n integrations: readonly Integration[];\n excludeFromIntegrations: readonly string[];\n deepLink: DeepLink | undefined;\n}\n\ninterface Cache {\n tags: TrackTags | null;\n}\n\nexport const cache: Cache = {\n tags: null,\n};\n\n// oxlint-disable typescript/no-non-null-assertion\nexport const config: Config = {\n endpoint: '',\n release: '0.0.0',\n storage: null!,\n platform: null!,\n environment: null!,\n getTags: null!,\n getDeviceId: null!,\n getHeaders: null!,\n integrations: [],\n excludeFromIntegrations: [],\n deepLink: undefined,\n};\n// oxlint-enable typescript/no-non-null-assertion\n\nexport function setupAnalytics(init: Options) {\n config.release = init.release;\n config.storage = init.storage;\n config.platform = init.platform;\n config.environment = init.environment;\n config.endpoint = init.endpoint.endsWith('/') ? init.endpoint.slice(0, -1) : init.endpoint;\n config.getTags = init.getTags;\n config.getDeviceId = init.getDeviceId;\n config.getHeaders = async () => ({\n 'Content-Type': 'application/json',\n ...(await init.getHeaders?.()),\n });\n config.integrations = init.integrations ?? [];\n config.excludeFromIntegrations = init.excludeFromIntegrations ?? [];\n config.deepLink = init.deepLink;\n // Logged, not thrown: the links are worth less than the setup they would break.\n try {\n init.deepLink?.listen();\n } catch (error) {\n console.error('analytics deepLink.listen failed', error);\n }\n}\n"],"mappings":";AA+DA,MAAa,QAAe,EAC1B,MAAM,KACR;AAGA,MAAa,SAAiB;CAC5B,UAAU;CACV,SAAS;CACT,SAAS;CACT,UAAU;CACV,aAAa;CACb,SAAS;CACT,aAAa;CACb,YAAY;CACZ,cAAc,CAAC;CACf,yBAAyB,CAAC;CAC1B,UAAU,KAAA;AACZ;AAGA,SAAgB,eAAe,MAAe;CAC5C,OAAO,UAAU,KAAK;CACtB,OAAO,UAAU,KAAK;CACtB,OAAO,WAAW,KAAK;CACvB,OAAO,cAAc,KAAK;CAC1B,OAAO,WAAW,KAAK,SAAS,SAAS,GAAG,IAAI,KAAK,SAAS,MAAM,GAAG,EAAE,IAAI,KAAK;CAClF,OAAO,UAAU,KAAK;CACtB,OAAO,cAAc,KAAK;CAC1B,OAAO,aAAa,aAAa;EAC/B,gBAAgB;EAChB,GAAI,MAAM,KAAK,aAAa;CAC9B;CACA,OAAO,eAAe,KAAK,gBAAgB,CAAC;CAC5C,OAAO,0BAA0B,KAAK,2BAA2B,CAAC;CAClE,OAAO,WAAW,KAAK;CAEvB,IAAI;EACF,KAAK,UAAU,OAAO;CACxB,SAAS,OAAO;EACd,QAAQ,MAAM,oCAAoC,KAAK;CACzD;AACF"}
|
package/dist/tanstack/index.cjs
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const require_tanstack_analytics = require("./analytics.cjs");
|
|
3
2
|
const require_tanstack_middleware = require("./middleware.cjs");
|
|
4
|
-
exports.Analytics = require_tanstack_analytics.Analytics;
|
|
5
3
|
exports.createClickIdMiddleware = require_tanstack_middleware.createClickIdMiddleware;
|
|
@@ -1,3 +1,2 @@
|
|
|
1
|
-
import { Analytics } from "./analytics.cjs";
|
|
2
1
|
import { ClickIdMiddlewareOptions, createClickIdMiddleware } from "./middleware.cjs";
|
|
3
|
-
export {
|
|
2
|
+
export { type ClickIdMiddlewareOptions, createClickIdMiddleware };
|
|
@@ -1,3 +1,2 @@
|
|
|
1
|
-
import { Analytics } from "./analytics.mjs";
|
|
2
1
|
import { ClickIdMiddlewareOptions, createClickIdMiddleware } from "./middleware.mjs";
|
|
3
|
-
export {
|
|
2
|
+
export { type ClickIdMiddlewareOptions, createClickIdMiddleware };
|
package/dist/tanstack/index.mjs
CHANGED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
let react_dom = require("react-dom");
|
|
3
|
+
let react_jsx_runtime = require("react/jsx-runtime");
|
|
4
|
+
// src/third-parties/analytics-props.tsx
|
|
5
|
+
/**
|
|
6
|
+
* Preconnects to every integration's `preconnect` origins. Called while rendering: React dedupes
|
|
7
|
+
* the hints and, on the server, puts them at the top of `<head>`.
|
|
8
|
+
*/
|
|
9
|
+
function preconnectIntegrations(integrations = []) {
|
|
10
|
+
integrations.forEach((integration) => {
|
|
11
|
+
integration.preconnect?.forEach((origin) => (0, react_dom.preconnect)(origin, { crossOrigin: "anonymous" }));
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* The integrations' scripts as plain `<script>` elements, for the frameworks that render them as
|
|
16
|
+
* they are (react).
|
|
17
|
+
*/
|
|
18
|
+
function IntegrationScripts({ integrations = [], nonce }) {
|
|
19
|
+
return integrations.flatMap(({ script }) => script ? [script] : []).map((script) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)("script", {
|
|
20
|
+
id: script.id,
|
|
21
|
+
nonce,
|
|
22
|
+
dangerouslySetInnerHTML: { __html: script.inline }
|
|
23
|
+
}, script.id));
|
|
24
|
+
}
|
|
25
|
+
exports.IntegrationScripts = IntegrationScripts;
|
|
26
|
+
exports.preconnectIntegrations = preconnectIntegrations;
|
|
27
|
+
|
|
28
|
+
//# sourceMappingURL=analytics-props.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"analytics-props.cjs","names":["preconnect"],"sources":["../../src/third-parties/analytics-props.tsx"],"sourcesContent":["import { preconnect } from 'react-dom';\nimport type { Integration } from './integration';\n\n/** The props every framework's `<Analytics>` takes (react, next). */\nexport interface AnalyticsProps {\n /** The integrations passed to `setupAnalytics`, whose scripts this renders. */\n integrations?: readonly Integration[];\n /** Renders `<meta property=\"fb:app_id\">`, for Facebook's sharing insights. */\n facebookAppId?: string;\n /** The request's CSP nonce, set on every script rendered. */\n nonce?: string;\n /** Reports Core Web Vitals as events (on by default). */\n reportWebVitals?: boolean;\n}\n\n/**\n * Preconnects to every integration's `preconnect` origins. Called while rendering: React dedupes\n * the hints and, on the server, puts them at the top of `<head>`.\n */\nexport function preconnectIntegrations(integrations: readonly Integration[] = []) {\n integrations.forEach((integration) => {\n integration.preconnect?.forEach((origin) => preconnect(origin, { crossOrigin: 'anonymous' }));\n });\n}\n\n/**\n * The integrations' scripts as plain `<script>` elements, for the frameworks that render them as\n * they are (react).\n */\nexport function IntegrationScripts({\n integrations = [],\n nonce,\n}: Pick<AnalyticsProps, 'integrations' | 'nonce'>) {\n return integrations\n .flatMap(({ script }) => (script ? [script] : []))\n .map((script) => (\n <script\n key={script.id}\n id={script.id}\n nonce={nonce}\n dangerouslySetInnerHTML={{ __html: script.inline }}\n />\n ));\n}\n"],"mappings":";;;;;;;;AAmBA,SAAgB,uBAAuB,eAAuC,CAAC,GAAG;CAChF,aAAa,SAAS,gBAAgB;EACpC,YAAY,YAAY,SAAS,YAAA,GAAWA,UAAAA,WAAAA,CAAW,QAAQ,EAAE,aAAa,YAAY,CAAC,CAAC;CAC9F,CAAC;AACH;;;;;AAMA,SAAgB,mBAAmB,EACjC,eAAe,CAAC,GAChB,SACiD;CACjD,OAAO,aACJ,SAAS,EAAE,aAAc,SAAS,CAAC,MAAM,IAAI,CAAC,CAAE,CAAC,CACjD,KAAK,WACJ,iBAAA,GAAA,kBAAA,IAAA,CAAC,UAAD;EAEE,IAAI,OAAO;EACJ;EACP,yBAAyB,EAAE,QAAQ,OAAO,OAAO;CAClD,GAJM,OAAO,EAIb,CACF;AACL"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Integration } from "./integration.cjs";
|
|
2
|
+
/** The props every framework's `<Analytics>` takes (react, next). */
|
|
3
|
+
export interface AnalyticsProps {
|
|
4
|
+
/** The integrations passed to `setupAnalytics`, whose scripts this renders. */
|
|
5
|
+
integrations?: readonly Integration[];
|
|
6
|
+
/** Renders `<meta property="fb:app_id">`, for Facebook's sharing insights. */
|
|
7
|
+
facebookAppId?: string;
|
|
8
|
+
/** The request's CSP nonce, set on every script rendered. */
|
|
9
|
+
nonce?: string;
|
|
10
|
+
/** Reports Core Web Vitals as events (on by default). */
|
|
11
|
+
reportWebVitals?: boolean;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Preconnects to every integration's `preconnect` origins. Called while rendering: React dedupes
|
|
15
|
+
* the hints and, on the server, puts them at the top of `<head>`.
|
|
16
|
+
*/
|
|
17
|
+
export declare function preconnectIntegrations(integrations?: readonly Integration[]): void;
|
|
18
|
+
/**
|
|
19
|
+
* The integrations' scripts as plain `<script>` elements, for the frameworks that render them as
|
|
20
|
+
* they are (react).
|
|
21
|
+
*/
|
|
22
|
+
export declare function IntegrationScripts({ integrations, nonce }: Pick<AnalyticsProps, 'integrations' | 'nonce'>): import("react").JSX.Element[];
|
|
23
|
+
//# sourceMappingURL=analytics-props.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"analytics-props.d.cts","names":[],"sources":["../../src/third-parties/analytics-props.tsx"],"mappings":";;iBAIiB;;EAEf,wBAAwB;;EAExB;;EAEA;;EAEA;;;;;;wBAOc,uBAAuB,wBAAuB;;;;;wBAU9C,qBACd,cACA,SACC,KAAK,4DAAyC,IAAA"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Integration } from "./integration.mjs";
|
|
2
|
+
/** The props every framework's `<Analytics>` takes (react, next). */
|
|
3
|
+
export interface AnalyticsProps {
|
|
4
|
+
/** The integrations passed to `setupAnalytics`, whose scripts this renders. */
|
|
5
|
+
integrations?: readonly Integration[];
|
|
6
|
+
/** Renders `<meta property="fb:app_id">`, for Facebook's sharing insights. */
|
|
7
|
+
facebookAppId?: string;
|
|
8
|
+
/** The request's CSP nonce, set on every script rendered. */
|
|
9
|
+
nonce?: string;
|
|
10
|
+
/** Reports Core Web Vitals as events (on by default). */
|
|
11
|
+
reportWebVitals?: boolean;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Preconnects to every integration's `preconnect` origins. Called while rendering: React dedupes
|
|
15
|
+
* the hints and, on the server, puts them at the top of `<head>`.
|
|
16
|
+
*/
|
|
17
|
+
export declare function preconnectIntegrations(integrations?: readonly Integration[]): void;
|
|
18
|
+
/**
|
|
19
|
+
* The integrations' scripts as plain `<script>` elements, for the frameworks that render them as
|
|
20
|
+
* they are (react).
|
|
21
|
+
*/
|
|
22
|
+
export declare function IntegrationScripts({ integrations, nonce }: Pick<AnalyticsProps, 'integrations' | 'nonce'>): import("react").JSX.Element[];
|
|
23
|
+
//# sourceMappingURL=analytics-props.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"analytics-props.d.mts","names":[],"sources":["../../src/third-parties/analytics-props.tsx"],"mappings":";;iBAIiB;;EAEf,wBAAwB;;EAExB;;EAEA;;EAEA;;;;;;wBAOc,uBAAuB,wBAAuB;;;;;wBAU9C,qBACd,cACA,SACC,KAAK,4DAAyC,IAAA"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { preconnect } from "react-dom";
|
|
2
|
+
import { jsx } from "react/jsx-runtime";
|
|
3
|
+
// src/third-parties/analytics-props.tsx
|
|
4
|
+
/**
|
|
5
|
+
* Preconnects to every integration's `preconnect` origins. Called while rendering: React dedupes
|
|
6
|
+
* the hints and, on the server, puts them at the top of `<head>`.
|
|
7
|
+
*/
|
|
8
|
+
function preconnectIntegrations(integrations = []) {
|
|
9
|
+
integrations.forEach((integration) => {
|
|
10
|
+
integration.preconnect?.forEach((origin) => preconnect(origin, { crossOrigin: "anonymous" }));
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* The integrations' scripts as plain `<script>` elements, for the frameworks that render them as
|
|
15
|
+
* they are (react).
|
|
16
|
+
*/
|
|
17
|
+
function IntegrationScripts({ integrations = [], nonce }) {
|
|
18
|
+
return integrations.flatMap(({ script }) => script ? [script] : []).map((script) => /* @__PURE__ */ jsx("script", {
|
|
19
|
+
id: script.id,
|
|
20
|
+
nonce,
|
|
21
|
+
dangerouslySetInnerHTML: { __html: script.inline }
|
|
22
|
+
}, script.id));
|
|
23
|
+
}
|
|
24
|
+
export { IntegrationScripts, preconnectIntegrations };
|
|
25
|
+
|
|
26
|
+
//# sourceMappingURL=analytics-props.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"analytics-props.mjs","names":[],"sources":["../../src/third-parties/analytics-props.tsx"],"sourcesContent":["import { preconnect } from 'react-dom';\nimport type { Integration } from './integration';\n\n/** The props every framework's `<Analytics>` takes (react, next). */\nexport interface AnalyticsProps {\n /** The integrations passed to `setupAnalytics`, whose scripts this renders. */\n integrations?: readonly Integration[];\n /** Renders `<meta property=\"fb:app_id\">`, for Facebook's sharing insights. */\n facebookAppId?: string;\n /** The request's CSP nonce, set on every script rendered. */\n nonce?: string;\n /** Reports Core Web Vitals as events (on by default). */\n reportWebVitals?: boolean;\n}\n\n/**\n * Preconnects to every integration's `preconnect` origins. Called while rendering: React dedupes\n * the hints and, on the server, puts them at the top of `<head>`.\n */\nexport function preconnectIntegrations(integrations: readonly Integration[] = []) {\n integrations.forEach((integration) => {\n integration.preconnect?.forEach((origin) => preconnect(origin, { crossOrigin: 'anonymous' }));\n });\n}\n\n/**\n * The integrations' scripts as plain `<script>` elements, for the frameworks that render them as\n * they are (react).\n */\nexport function IntegrationScripts({\n integrations = [],\n nonce,\n}: Pick<AnalyticsProps, 'integrations' | 'nonce'>) {\n return integrations\n .flatMap(({ script }) => (script ? [script] : []))\n .map((script) => (\n <script\n key={script.id}\n id={script.id}\n nonce={nonce}\n dangerouslySetInnerHTML={{ __html: script.inline }}\n />\n ));\n}\n"],"mappings":";;;;;;;AAmBA,SAAgB,uBAAuB,eAAuC,CAAC,GAAG;CAChF,aAAa,SAAS,gBAAgB;EACpC,YAAY,YAAY,SAAS,WAAW,WAAW,QAAQ,EAAE,aAAa,YAAY,CAAC,CAAC;CAC9F,CAAC;AACH;;;;;AAMA,SAAgB,mBAAmB,EACjC,eAAe,CAAC,GAChB,SACiD;CACjD,OAAO,aACJ,SAAS,EAAE,aAAc,SAAS,CAAC,MAAM,IAAI,CAAC,CAAE,CAAC,CACjD,KAAK,WACJ,oBAAC,UAAD;EAEE,IAAI,OAAO;EACJ;EACP,yBAAyB,EAAE,QAAQ,OAAO,OAAO;CAClD,GAJM,OAAO,EAIb,CACF;AACL"}
|
|
@@ -15,6 +15,38 @@ function setGAUser({ user_id, user_data }) {
|
|
|
15
15
|
window.gtag("set", "user_id", user_id || null);
|
|
16
16
|
if (user_data) window.gtag("set", "user_data", user_data);
|
|
17
17
|
}
|
|
18
|
+
/**
|
|
19
|
+
* Google Analytics 4 (gtag.js): every event, for behavior analysis. `src` serves gtag.js from
|
|
20
|
+
* another path (a first-party proxy); `debug` turns on GA4's DebugView for this page.
|
|
21
|
+
*/
|
|
22
|
+
function googleAnalytics(options) {
|
|
23
|
+
const src = options.src ?? `https://www.googletagmanager.com/gtag/js?id=${options.id}`;
|
|
24
|
+
return {
|
|
25
|
+
type: "analytics",
|
|
26
|
+
track: sendGAEvent,
|
|
27
|
+
setUser: setGAUser,
|
|
28
|
+
script: {
|
|
29
|
+
id: "gtag",
|
|
30
|
+
inline: `
|
|
31
|
+
window.dataLayer = window.dataLayer || [];
|
|
32
|
+
function gtag(){dataLayer.push(arguments);}
|
|
33
|
+
gtag('js', new Date());
|
|
34
|
+
gtag('config', ${JSON.stringify(options.id)}${options.debug ? ", { debug_mode: true }" : ""});
|
|
35
|
+
(function (d, s, u) {
|
|
36
|
+
var j = d.createElement(s);
|
|
37
|
+
j.async = true;
|
|
38
|
+
// A script added here does not inherit the nonce: hand it this one's, or a CSP that
|
|
39
|
+
// allows scripts by nonce (without 'strict-dynamic') blocks gtag.js.
|
|
40
|
+
j.nonce = d.currentScript && d.currentScript.nonce;
|
|
41
|
+
j.src = u;
|
|
42
|
+
var f = d.getElementsByTagName(s)[0];
|
|
43
|
+
f.parentNode.insertBefore(j, f);
|
|
44
|
+
})(document, 'script', ${JSON.stringify(src)});
|
|
45
|
+
`
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
exports.googleAnalytics = googleAnalytics;
|
|
18
50
|
exports.sendGAEvent = sendGAEvent;
|
|
19
51
|
exports.setGAUser = setGAUser;
|
|
20
52
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"google-analytics.cjs","names":[],"sources":["../../src/third-parties/google-analytics.ts"],"sourcesContent":["import type { UpdateVisitorDTO } from '../schema/index';\nimport type { Gtag } from '../track/gtag';\nimport type { EventName, TrackName, TrackProperties } from '../track/types';\n\ndeclare global {\n interface Window {\n /** Undefined until the Google tag (gtag.js) script has loaded. */\n gtag?: Gtag['gtag'];\n }\n}\n\nexport function sendGAEvent<T extends EventName>(\n name: TrackName<T>,\n properties?: TrackProperties<T>\n) {\n if (typeof window === 'undefined' || !window.gtag) {\n console.warn('GA has not been initialized');\n return false;\n }\n window.gtag('event', name, properties);\n}\n\nexport function setGAUser({ user_id, user_data }: UpdateVisitorDTO) {\n if (typeof window === 'undefined' || !window.gtag) {\n console.warn('GA has not been initialized');\n return;\n }\n // No user is sign-out (`reset()`): GA keeps a set user_id for every later event on the page, so\n // it is cleared explicitly.\n window.gtag('set', 'user_id', user_id || null);\n if (user_data) window.gtag('set', 'user_data', user_data);\n}\n"],"mappings":";;
|
|
1
|
+
{"version":3,"file":"google-analytics.cjs","names":[],"sources":["../../src/third-parties/google-analytics.ts"],"sourcesContent":["import type { UpdateVisitorDTO } from '../schema/index';\nimport type { GaId, Gtag } from '../track/gtag';\nimport type { EventName, TrackName, TrackProperties } from '../track/types';\nimport type { Integration } from './integration';\n\ndeclare global {\n interface Window {\n /** Undefined until the Google tag (gtag.js) script has loaded. */\n gtag?: Gtag['gtag'];\n }\n}\n\nexport function sendGAEvent<T extends EventName>(\n name: TrackName<T>,\n properties?: TrackProperties<T>\n) {\n if (typeof window === 'undefined' || !window.gtag) {\n console.warn('GA has not been initialized');\n return false;\n }\n window.gtag('event', name, properties);\n}\n\nexport function setGAUser({ user_id, user_data }: UpdateVisitorDTO) {\n if (typeof window === 'undefined' || !window.gtag) {\n console.warn('GA has not been initialized');\n return;\n }\n // No user is sign-out (`reset()`): GA keeps a set user_id for every later event on the page, so\n // it is cleared explicitly.\n window.gtag('set', 'user_id', user_id || null);\n if (user_data) window.gtag('set', 'user_data', user_data);\n}\n\n/**\n * Google Analytics 4 (gtag.js): every event, for behavior analysis. `src` serves gtag.js from\n * another path (a first-party proxy); `debug` turns on GA4's DebugView for this page.\n */\nexport function googleAnalytics(options: { id: GaId; src?: string; debug?: boolean }): Integration {\n const src = options.src ?? `https://www.googletagmanager.com/gtag/js?id=${options.id}`;\n return {\n type: 'analytics',\n track: sendGAEvent,\n setUser: setGAUser,\n script: {\n id: 'gtag',\n inline: `\n window.dataLayer = window.dataLayer || [];\n function gtag(){dataLayer.push(arguments);}\n gtag('js', new Date());\n gtag('config', ${JSON.stringify(options.id)}${options.debug ? ', { debug_mode: true }' : ''});\n (function (d, s, u) {\n var j = d.createElement(s);\n j.async = true;\n // A script added here does not inherit the nonce: hand it this one's, or a CSP that\n // allows scripts by nonce (without 'strict-dynamic') blocks gtag.js.\n j.nonce = d.currentScript && d.currentScript.nonce;\n j.src = u;\n var f = d.getElementsByTagName(s)[0];\n f.parentNode.insertBefore(j, f);\n })(document, 'script', ${JSON.stringify(src)});\n `,\n },\n };\n}\n"],"mappings":";;AAYA,SAAgB,YACd,MACA,YACA;CACA,IAAI,OAAO,WAAW,eAAe,CAAC,OAAO,MAAM;EACjD,QAAQ,KAAK,6BAA6B;EAC1C,OAAO;CACT;CACA,OAAO,KAAK,SAAS,MAAM,UAAU;AACvC;AAEA,SAAgB,UAAU,EAAE,SAAS,aAA+B;CAClE,IAAI,OAAO,WAAW,eAAe,CAAC,OAAO,MAAM;EACjD,QAAQ,KAAK,6BAA6B;EAC1C;CACF;CAGA,OAAO,KAAK,OAAO,WAAW,WAAW,IAAI;CAC7C,IAAI,WAAW,OAAO,KAAK,OAAO,aAAa,SAAS;AAC1D;;;;;AAMA,SAAgB,gBAAgB,SAAmE;CACjG,MAAM,MAAM,QAAQ,OAAO,+CAA+C,QAAQ;CAClF,OAAO;EACL,MAAM;EACN,OAAO;EACP,SAAS;EACT,QAAQ;GACN,IAAI;GACJ,QAAQ;;;;yBAIW,KAAK,UAAU,QAAQ,EAAE,IAAI,QAAQ,QAAQ,2BAA2B,GAAG;;;;;;;;;;iCAUnE,KAAK,UAAU,GAAG,EAAE;;EAEjD;CACF;AACF"}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { Gtag } from "../track/gtag.cjs";
|
|
1
|
+
import { GaId, Gtag } from "../track/gtag.cjs";
|
|
2
2
|
import { EventName, TrackName, TrackProperties } from "../track/types.cjs";
|
|
3
3
|
import { UpdateVisitorDTO } from "../schema/index.cjs";
|
|
4
|
+
import { Integration } from "./integration.cjs";
|
|
4
5
|
declare global {
|
|
5
6
|
interface Window {
|
|
6
7
|
/** Undefined until the Google tag (gtag.js) script has loaded. */
|
|
@@ -9,4 +10,13 @@ declare global {
|
|
|
9
10
|
}
|
|
10
11
|
export declare function sendGAEvent<T extends EventName>(name: TrackName<T>, properties?: TrackProperties<T>): false | undefined;
|
|
11
12
|
export declare function setGAUser({ user_id, user_data }: UpdateVisitorDTO): void;
|
|
13
|
+
/**
|
|
14
|
+
* Google Analytics 4 (gtag.js): every event, for behavior analysis. `src` serves gtag.js from
|
|
15
|
+
* another path (a first-party proxy); `debug` turns on GA4's DebugView for this page.
|
|
16
|
+
*/
|
|
17
|
+
export declare function googleAnalytics(options: {
|
|
18
|
+
id: GaId;
|
|
19
|
+
src?: string;
|
|
20
|
+
debug?: boolean;
|
|
21
|
+
}): Integration;
|
|
12
22
|
//# sourceMappingURL=google-analytics.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"google-analytics.d.cts","names":[],"sources":["../../src/third-parties/google-analytics.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"google-analytics.d.cts","names":[],"sources":["../../src/third-parties/google-analytics.ts"],"mappings":";;;;QAKQ;YACI;;IAER,OAAO;;;wBAIK,YAAY,UAAU,WACpC,MAAM,UAAU,IAChB,aAAa,gBAAgB;wBASf,YAAY,SAAS,aAAa;;;;;wBAelC,gBAAgB;EAAW,IAAI;EAAM;EAAc;IAAoB"}
|