@formo/analytics 1.33.1 → 1.34.1

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.
Files changed (45) hide show
  1. package/README.md +3 -0
  2. package/dist/cjs/src/FormoAnalytics.d.ts +65 -19
  3. package/dist/cjs/src/FormoAnalytics.js +188 -94
  4. package/dist/cjs/src/event/EventFactory.d.ts +1 -1
  5. package/dist/cjs/src/event/EventFactory.js +32 -16
  6. package/dist/cjs/src/event/sanitize.d.ts +13 -0
  7. package/dist/cjs/src/event/sanitize.js +94 -0
  8. package/dist/cjs/src/privy/index.d.ts +8 -2
  9. package/dist/cjs/src/privy/index.js +8 -2
  10. package/dist/cjs/src/privy/types.d.ts +25 -2
  11. package/dist/cjs/src/privy/utils.d.ts +100 -0
  12. package/dist/cjs/src/privy/utils.js +375 -16
  13. package/dist/cjs/src/session/index.d.ts +73 -6
  14. package/dist/cjs/src/session/index.js +309 -12
  15. package/dist/cjs/src/solana/SolanaManager.d.ts +1 -1
  16. package/dist/cjs/src/solana/SolanaManager.js +1 -1
  17. package/dist/cjs/src/solana/storeTypes.d.ts +1 -1
  18. package/dist/cjs/src/solana/storeTypes.js +1 -1
  19. package/dist/cjs/src/solana/types.d.ts +2 -2
  20. package/dist/cjs/src/types/base.d.ts +17 -1
  21. package/dist/cjs/src/version.d.ts +1 -1
  22. package/dist/cjs/src/version.js +1 -1
  23. package/dist/esm/src/FormoAnalytics.d.ts +65 -19
  24. package/dist/esm/src/FormoAnalytics.js +188 -94
  25. package/dist/esm/src/event/EventFactory.d.ts +1 -1
  26. package/dist/esm/src/event/EventFactory.js +32 -16
  27. package/dist/esm/src/event/sanitize.d.ts +13 -0
  28. package/dist/esm/src/event/sanitize.js +88 -0
  29. package/dist/esm/src/privy/index.d.ts +8 -2
  30. package/dist/esm/src/privy/index.js +8 -2
  31. package/dist/esm/src/privy/types.d.ts +25 -2
  32. package/dist/esm/src/privy/utils.d.ts +100 -0
  33. package/dist/esm/src/privy/utils.js +374 -16
  34. package/dist/esm/src/session/index.d.ts +73 -6
  35. package/dist/esm/src/session/index.js +309 -12
  36. package/dist/esm/src/solana/SolanaManager.d.ts +1 -1
  37. package/dist/esm/src/solana/SolanaManager.js +1 -1
  38. package/dist/esm/src/solana/storeTypes.d.ts +1 -1
  39. package/dist/esm/src/solana/storeTypes.js +1 -1
  40. package/dist/esm/src/solana/types.d.ts +2 -2
  41. package/dist/esm/src/types/base.d.ts +17 -1
  42. package/dist/esm/src/version.d.ts +1 -1
  43. package/dist/esm/src/version.js +1 -1
  44. package/dist/index.umd.min.js +1 -1
  45. package/package.json +7 -7
@@ -0,0 +1,94 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ Object.defineProperty(exports, "__esModule", { value: true });
14
+ exports.sanitizeTrafficSources = exports.sanitizeUtm = exports.sanitizeRef = exports.sanitizeClickId = void 0;
15
+ var constants_1 = require("./constants");
16
+ /**
17
+ * Traffic-source value sanitization.
18
+ *
19
+ * Vulnerability scanners (e.g. Acunetix) crawl customer sites injecting XSS
20
+ * probes such as `javascript:domxssExecutionSink(1,"'\"><xsstag>()locxss")`
21
+ * or `<script>alert(1)</script>` into every query parameter. Without
22
+ * validation those payloads are captured verbatim as utm_* / click-id / ref
23
+ * values, persisted as sticky session traffic sources, and pollute the
24
+ * customer's attribution reporting. Each field class gets the tightest rule
25
+ * its legitimate values allow (verified against production data):
26
+ *
27
+ * - Click IDs are opaque platform-generated tokens (base64url-ish); every
28
+ * legitimate production value matches the strict token pattern.
29
+ * - Referral codes are short tokens; >99.5% of production values match the
30
+ * strict pattern and none of the remainder are legitimate (scanner
31
+ * payloads, mangled encodings, URLs glued to codes).
32
+ * - UTM values are free-form (spaces, unicode, `+` are legitimate), so they
33
+ * only reject markup/quote characters, dangerous URL schemes, control and
34
+ * zero-width characters, and absurd lengths.
35
+ *
36
+ * Invalid values are dropped to "" — the same representation as "parameter
37
+ * absent" — rather than repaired, so a poisoned value can never be persisted
38
+ * or reported.
39
+ */
40
+ var CLICK_ID_PATTERN = /^[A-Za-z0-9._-]{1,255}$/;
41
+ var REF_PATTERN = /^[A-Za-z0-9._-]{1,64}$/;
42
+ var UTM_MAX_LENGTH = 255;
43
+ // Markup/quote/backslash characters plus C0/C1 control characters and
44
+ // zero-width / bidi / BOM / replacement characters (mangled-encoding
45
+ // markers). Explicit ranges instead of \p{C} to avoid the `u`-flag
46
+ // property-escape requirement.
47
+ var UTM_FORBIDDEN_CHARS = /[<>"'`\\\u0000-\u001f\u007f-\u009f\u200b-\u200f\u2028-\u202e\u2060\ufeff\ufffd]/;
48
+ // Values smuggling an executable/URL scheme, e.g. `javascript:alert(1)`.
49
+ var FORBIDDEN_SCHEME_PREFIX = /^\s*(javascript|data|vbscript):/i;
50
+ var sanitizeClickId = function (value) {
51
+ return CLICK_ID_PATTERN.test(value) ? value : "";
52
+ };
53
+ exports.sanitizeClickId = sanitizeClickId;
54
+ var sanitizeRef = function (value) {
55
+ return REF_PATTERN.test(value) ? value : "";
56
+ };
57
+ exports.sanitizeRef = sanitizeRef;
58
+ var sanitizeUtm = function (value) {
59
+ return value.length <= UTM_MAX_LENGTH &&
60
+ !UTM_FORBIDDEN_CHARS.test(value) &&
61
+ !FORBIDDEN_SCHEME_PREFIX.test(value)
62
+ ? value
63
+ : "";
64
+ };
65
+ exports.sanitizeUtm = sanitizeUtm;
66
+ var CLICK_ID_KEYS = new Set(constants_1.CLICK_ID_PARAMS);
67
+ /**
68
+ * Sanitize every traffic-source field of a (possibly sparse) traffic-source
69
+ * object. `referrer` is left untouched: it is a browser-set URL already
70
+ * handled by redactUrl, not an attacker-controlled query parameter. Unknown
71
+ * keys fall through to the UTM rule, the most permissive one.
72
+ */
73
+ var sanitizeTrafficSources = function (trafficSources) {
74
+ var result = __assign({}, trafficSources);
75
+ for (var _i = 0, _a = Object.keys(result); _i < _a.length; _i++) {
76
+ var key = _a[_i];
77
+ var value = result[key];
78
+ if (typeof value !== "string" || value === "" || key === "referrer") {
79
+ continue;
80
+ }
81
+ if (CLICK_ID_KEYS.has(key)) {
82
+ result[key] = sanitizeClickId(value);
83
+ }
84
+ else if (key === "ref") {
85
+ result[key] = sanitizeRef(value);
86
+ }
87
+ else {
88
+ result[key] = sanitizeUtm(value);
89
+ }
90
+ }
91
+ return result;
92
+ };
93
+ exports.sanitizeTrafficSources = sanitizeTrafficSources;
94
+ //# sourceMappingURL=sanitize.js.map
@@ -1,8 +1,14 @@
1
1
  /**
2
2
  * Privy integration module
3
3
  *
4
- * Provides utilities for enriching wallet profiles with Privy user data.
5
- * This module exports the property extraction utility and related types.
4
+ * Exports `parsePrivyProperties`, which parses a Privy user into a flat
5
+ * properties object and the list of linked wallets without emitting anything -
6
+ * useful for inspecting or displaying what an identify would send.
7
+ *
8
+ * The identify itself is `formo.identify(user)`, which recognises a Privy user
9
+ * by shape. The function behind it lives in ./utils and is internal.
10
+ *
11
+ * This module is React-free so it can be used from the `core` entry.
6
12
  */
7
13
  export { parsePrivyProperties } from "./utils";
8
14
  export type { PrivyUser, PrivyLinkedAccount, PrivyAccountType, PrivyProfileProperties, PrivyWalletInfo, } from "./types";
@@ -2,8 +2,14 @@
2
2
  /**
3
3
  * Privy integration module
4
4
  *
5
- * Provides utilities for enriching wallet profiles with Privy user data.
6
- * This module exports the property extraction utility and related types.
5
+ * Exports `parsePrivyProperties`, which parses a Privy user into a flat
6
+ * properties object and the list of linked wallets without emitting anything -
7
+ * useful for inspecting or displaying what an identify would send.
8
+ *
9
+ * The identify itself is `formo.identify(user)`, which recognises a Privy user
10
+ * by shape. The function behind it lives in ./utils and is internal.
11
+ *
12
+ * This module is React-free so it can be used from the `core` entry.
7
13
  */
8
14
  Object.defineProperty(exports, "__esModule", { value: true });
9
15
  exports.parsePrivyProperties = void 0;
@@ -38,6 +38,16 @@ export interface PrivyLinkedAccount {
38
38
  telegramUserId?: string | null;
39
39
  firstName?: string | null;
40
40
  lastName?: string | null;
41
+ credentialId?: string | null;
42
+ customUserId?: string | null;
43
+ embeddedWallets?: Array<{
44
+ address: string;
45
+ chainType?: string | null;
46
+ }> | null;
47
+ smartWallets?: Array<{
48
+ address: string;
49
+ chainType?: string | null;
50
+ }> | null;
41
51
  firstVerifiedAt?: Date | null;
42
52
  latestVerifiedAt?: Date | null;
43
53
  }
@@ -51,8 +61,12 @@ export interface PrivyLinkedAccount {
51
61
  export interface PrivyUser {
52
62
  /** Privy user ID in DID format (e.g., "did:privy:cm3np...") */
53
63
  id: string;
54
- /** Account creation timestamp */
55
- createdAt?: Date;
64
+ /**
65
+ * Account creation timestamp. Privy's React SDK supplies a `Date`, but a user
66
+ * object from the REST API or one that made a JSON round-trip carries an ISO
67
+ * string or epoch number, so all three are accepted and normalized.
68
+ */
69
+ createdAt?: Date | string | number;
56
70
  /** All linked accounts */
57
71
  linkedAccounts?: PrivyLinkedAccount[];
58
72
  /** Optional custom metadata */
@@ -123,6 +137,11 @@ export interface PrivyUser {
123
137
  username: string | null;
124
138
  name: string | null;
125
139
  };
140
+ /** Privy's Twitch account exposes only subject/username - no email. */
141
+ twitch?: {
142
+ subject: string;
143
+ username: string | null;
144
+ };
126
145
  line?: {
127
146
  subject: string;
128
147
  name: string | null;
@@ -150,9 +169,11 @@ export interface PrivyProfileProperties {
150
169
  privyDid: string;
151
170
  privyCreatedAt?: number;
152
171
  email?: string;
172
+ phone?: string;
153
173
  apple?: string;
154
174
  discord?: string;
155
175
  twitter?: string;
176
+ twitch?: string;
156
177
  farcaster?: string;
157
178
  github?: string;
158
179
  google?: string;
@@ -162,6 +183,8 @@ export interface PrivyProfileProperties {
162
183
  telegram?: string;
163
184
  tiktok?: string;
164
185
  instagram?: string;
186
+ /** The `customUserId` of a linked `custom_auth` account, when present. */
187
+ customUserId?: string;
165
188
  [key: string]: unknown;
166
189
  }
167
190
  /**
@@ -2,12 +2,19 @@
2
2
  * Utility functions for extracting profile properties from Privy user objects.
3
3
  */
4
4
  import { PrivyProfileProperties, PrivyUser, PrivyWalletInfo } from "./types";
5
+ import { IFormoAnalytics } from "../types/base";
6
+ import { IFormoEventProperties } from "../types/events";
5
7
  /**
6
8
  * Extract profile properties and wallet addresses from a Privy user object.
7
9
  *
8
10
  * Parses the Privy user's linked accounts into a flat properties object
9
11
  * (email, social accounts, etc.) and extracts all linked wallet addresses.
10
12
  *
13
+ * For most apps prefer the {@link identifyPrivyUser} one-liner, which builds on
14
+ * this function and also forwards per-wallet metadata and handles event
15
+ * attribution. Use `parsePrivyProperties` directly only for advanced/custom
16
+ * flows.
17
+ *
11
18
  * @param user - The Privy user object from `usePrivy()`
12
19
  * @returns An object with `properties` and `wallets`
13
20
  *
@@ -29,4 +36,97 @@ export declare function parsePrivyProperties(user: PrivyUser): {
29
36
  properties: PrivyProfileProperties;
30
37
  wallets: PrivyWalletInfo[];
31
38
  };
39
+ /**
40
+ * Options for {@link identifyPrivyUser}.
41
+ */
42
+ export interface IdentifyPrivyUserOptions {
43
+ /**
44
+ * Optional override for the wallet that should own event attribution - the
45
+ * one promoted to the SDK's current address/user, while every other linked
46
+ * wallet is recorded only for clustering.
47
+ *
48
+ * You usually don't need this. When omitted, the helper first uses the wallet
49
+ * the SDK already treats as active (a prior wagmi/EIP-1193 connect), then
50
+ * Privy's own surfaced wallet (`user.wallet`), then a best-effort guess
51
+ * (embedded wallets deprioritized, so the last external wallet). Pass it only
52
+ * when you want to pin attribution to a specific wallet - e.g. the currently
53
+ * connected wallet from `useWallets()[0]?.address`, which reflects the live
54
+ * active wallet more precisely than `user.wallet`.
55
+ *
56
+ * Matched strictly: if it doesn't correspond to one of the user's linked
57
+ * wallets, no wallet is promoted and the SDK's current wallet is left as-is
58
+ * (so a connected wallet that isn't linked in Privy is preserved).
59
+ */
60
+ activeAddress?: string;
61
+ /**
62
+ * Extra properties merged into every identify call, on top of the profile
63
+ * properties parsed from the Privy user (email, socials, DID, …) and the
64
+ * per-wallet metadata (`wallet_client`, `chain_type`, `is_embedded`).
65
+ *
66
+ * Identify events are deduped per `(wallet, user, properties)` within a
67
+ * session, so changing these properties re-emits with the updated profile
68
+ * rather than being swallowed. The flip side: a value that changes on every
69
+ * call (a timestamp, a random id) makes every identify a new event. Pass
70
+ * stable identity metadata here and put volatile values on `track()` events.
71
+ */
72
+ properties?: IFormoEventProperties;
73
+ }
74
+ /**
75
+ * Identify every wallet linked to a Privy user under that user's Privy DID.
76
+ *
77
+ * This is the one-liner replacement for hand-rolling a loop over
78
+ * {@link parsePrivyProperties}. For each linked wallet it calls
79
+ * `analytics.identify({ address, userId: user.id }, …)` with the shared
80
+ * profile properties plus that wallet's `wallet_client`, `chain_type`, and
81
+ * `is_embedded` metadata. Because every wallet is tagged with the same Privy
82
+ * `userId`, Formo can cluster them server-side into a single user.
83
+ *
84
+ * Attribution: only the active wallet (see
85
+ * {@link IdentifyPrivyUserOptions.activeAddress}) promotes the SDK's current
86
+ * address/user; every other linked wallet is recorded purely for clustering and
87
+ * does not repoint attribution. Because the clustering identifies don't touch
88
+ * active state, a connected wallet that isn't linked in Privy is left untouched
89
+ * rather than overwritten. This does not change the public `identify()` API.
90
+ *
91
+ * Before emitting, it reconciles the SDK's chain id with the active wallet's
92
+ * chain namespace (clearing a stale EVM chain id when a Solana wallet becomes
93
+ * active, and vice versa), so identifies aren't dropped by an `excludeChains`
94
+ * gate and later events aren't paired with the wrong chain. This happens here,
95
+ * so the direct helper and the `formo.identify(user)` form
96
+ * behave identically.
97
+ *
98
+ * Returns the active linked wallet's `{ address, chainType }` (the one now
99
+ * owning attribution), or `undefined` when no linked wallet matched the
100
+ * requested active address (or the user had no wallets).
101
+ *
102
+ * All linked wallet addresses used here come from `user.linkedAccounts`, which
103
+ * is fully available on the frontend from Privy's `usePrivy()` hook.
104
+ *
105
+ * Note: `identify()` is keyed on a wallet address, so a Privy user with no
106
+ * linked wallet is a no-op (nothing is emitted). Attaching a user identity that
107
+ * has no wallet is out of scope for this address-keyed helper.
108
+ *
109
+ * @param analytics - The Formo analytics instance (e.g. from `useFormo()`)
110
+ * @param user - The Privy user object from `usePrivy()`
111
+ * @param options - See {@link IdentifyPrivyUserOptions}
112
+ *
113
+ * @example
114
+ * ```ts
115
+ * import { identifyPrivyUser } from '@formo/analytics';
116
+ *
117
+ * const { user } = usePrivy();
118
+ * const { wallets } = useWallets();
119
+ * if (user) {
120
+ * // activeAddress is optional - omit it if the SDK already tracks the
121
+ * // connected wallet via a wagmi/EIP-1193 connect.
122
+ * await identifyPrivyUser(formo, user, {
123
+ * activeAddress: wallets[0]?.address,
124
+ * });
125
+ * }
126
+ * ```
127
+ */
128
+ export declare function identifyPrivyUser(analytics: IFormoAnalytics, user: PrivyUser, options?: IdentifyPrivyUserOptions): Promise<{
129
+ address: string;
130
+ chainType?: string;
131
+ } | undefined>;
32
132
  //# sourceMappingURL=utils.d.ts.map