@agent-native/core 0.77.24 → 0.78.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.
- package/corpus/README.md +2 -2
- package/corpus/core/CHANGELOG.md +20 -0
- package/corpus/core/docs/content/template-analytics.md +5 -0
- package/corpus/core/docs/content/tracking.md +45 -0
- package/corpus/core/package.json +3 -1
- package/corpus/core/src/agent/types.ts +22 -0
- package/corpus/core/src/client/AgentPanel.tsx +34 -18
- package/corpus/core/src/client/AssistantChat.tsx +40 -0
- package/corpus/core/src/client/agent-chat.ts +173 -0
- package/corpus/core/src/client/analytics.ts +141 -5
- package/corpus/core/src/client/composer/TiptapComposer.tsx +316 -60
- package/corpus/core/src/client/composer/extensions/MentionReference.tsx +3 -0
- package/corpus/core/src/client/composer/types.ts +22 -0
- package/corpus/core/src/client/index.ts +14 -0
- package/corpus/core/src/client/session-replay.ts +800 -0
- package/corpus/core/src/db/schema.ts +12 -0
- package/corpus/core/src/deploy/build.ts +75 -0
- package/corpus/core/src/private-blob/index.ts +18 -0
- package/corpus/core/src/private-blob/registry.ts +241 -0
- package/corpus/core/src/private-blob/types.ts +55 -0
- package/corpus/core/src/scripts/utils.ts +34 -6
- package/corpus/core/src/server/agent-chat-plugin.ts +10 -0
- package/corpus/core/src/server/email.ts +5 -0
- package/corpus/core/src/server/index.ts +1 -0
- package/corpus/core/src/styles/agent-native.css +24 -0
- package/corpus/templates/analytics/.agents/skills/dashboard-management/SKILL.md +1 -1
- package/corpus/templates/analytics/.agents/skills/session-replay/SKILL.md +43 -0
- package/corpus/templates/analytics/AGENTS.md +32 -1
- package/corpus/templates/analytics/DEVELOPING.md +9 -1
- package/corpus/templates/analytics/actions/delete-dashboard-report-subscription.ts +24 -0
- package/corpus/templates/analytics/actions/get-session-replay-events.ts +50 -0
- package/corpus/templates/analytics/actions/get-session-replay-summary.ts +27 -0
- package/corpus/templates/analytics/actions/list-dashboard-report-subscriptions.ts +24 -0
- package/corpus/templates/analytics/actions/list-session-recordings.ts +59 -0
- package/corpus/templates/analytics/actions/navigate.ts +15 -5
- package/corpus/templates/analytics/actions/query-agent-native-analytics.ts +2 -2
- package/corpus/templates/analytics/actions/save-dashboard-report-subscription.ts +36 -0
- package/corpus/templates/analytics/actions/send-dashboard-report-now.ts +60 -0
- package/corpus/templates/analytics/actions/view-screen.ts +50 -2
- package/corpus/templates/analytics/app/components/dashboard/SqlChart.tsx +51 -2
- package/corpus/templates/analytics/app/components/layout/Header.tsx +4 -1
- package/corpus/templates/analytics/app/components/layout/Layout.tsx +14 -2
- package/corpus/templates/analytics/app/components/layout/Sidebar.tsx +6 -6
- package/corpus/templates/analytics/app/hooks/use-navigation-state.ts +35 -6
- package/corpus/templates/analytics/app/i18n-data.ts +1119 -0
- package/corpus/templates/analytics/app/pages/adhoc/sql-dashboard/EmailReportDialog.tsx +437 -0
- package/corpus/templates/analytics/app/pages/adhoc/sql-dashboard/SqlChartCard.tsx +11 -12
- package/corpus/templates/analytics/app/pages/adhoc/sql-dashboard/dashboard-layout.ts +43 -0
- package/corpus/templates/analytics/app/pages/adhoc/sql-dashboard/index.tsx +136 -43
- package/corpus/templates/analytics/app/pages/adhoc/sql-dashboard/report-filters.ts +26 -0
- package/corpus/templates/analytics/app/pages/sessions/SessionDetailPage.tsx +450 -0
- package/corpus/templates/analytics/app/pages/sessions/SessionsPage.tsx +419 -0
- package/corpus/templates/analytics/app/routes/overview.tsx +13 -6
- package/corpus/templates/analytics/app/routes/sessions.$recordingId.tsx +10 -0
- package/corpus/templates/analytics/app/routes/sessions._index.tsx +10 -0
- package/corpus/templates/analytics/changelog/2026-06-25-analytics-now-opens-directly-to-ask-instead-of-the-old-overv.md +6 -0
- package/corpus/templates/analytics/changelog/2026-06-25-dashboard-charts-now-show-reliable-blue-placement-lines-whil.md +6 -0
- package/corpus/templates/analytics/changelog/2026-06-26-dashboards-can-now-send-scheduled-daily-email-reports-from-t.md +6 -0
- package/corpus/templates/analytics/changelog/2026-06-26-session-replay-browser-sessions-can-now-be-reviewed-from.md +6 -0
- package/corpus/templates/analytics/package.json +5 -2
- package/corpus/templates/analytics/scripts/emit-netlify-dashboard-report-cron.ts +131 -0
- package/corpus/templates/analytics/scripts/seed-session-replay.ts +198 -0
- package/corpus/templates/analytics/seeds/dashboards/agent-native-templates-first-party.json +140 -27
- package/corpus/templates/analytics/server/db/index.ts +12 -0
- package/corpus/templates/analytics/server/db/schema.ts +131 -0
- package/corpus/templates/analytics/server/handlers/session-replay.ts +281 -0
- package/corpus/templates/analytics/server/handlers/sql-query.ts +8 -348
- package/corpus/templates/analytics/server/jobs/dashboard-report.ts +66 -0
- package/corpus/templates/analytics/server/jobs/session-replay-retention.ts +29 -0
- package/corpus/templates/analytics/server/lib/dashboard-panel-query.ts +365 -0
- package/corpus/templates/analytics/server/lib/dashboard-report-subscriptions.ts +473 -0
- package/corpus/templates/analytics/server/lib/dashboard-report.ts +405 -0
- package/corpus/templates/analytics/server/lib/first-party-analytics.ts +63 -18
- package/corpus/templates/analytics/server/lib/first-party-metric-catalog.ts +98 -26
- package/corpus/templates/analytics/server/lib/session-replay.ts +1645 -0
- package/corpus/templates/analytics/server/plugins/auth.ts +1 -1
- package/corpus/templates/analytics/server/plugins/dashboard-report-jobs.ts +47 -0
- package/corpus/templates/analytics/server/plugins/db.ts +200 -0
- package/corpus/templates/analytics/server/plugins/session-replay-retention-jobs.ts +41 -0
- package/corpus/templates/analytics/server/routes/api/analytics/replay.options.ts +1 -0
- package/corpus/templates/analytics/server/routes/api/analytics/replay.post.ts +1 -0
- package/corpus/templates/analytics/server/routes/api/dashboard-reports/run.post.ts +61 -0
- package/corpus/templates/analytics/server/routes/api/session-replay/recordings/[recordingId]/chunks/[seq].get.ts +1 -0
- package/corpus/templates/analytics/server/routes/api/session-replay/recordings/[recordingId]/events.get.ts +1 -0
- package/corpus/templates/analytics/server/routes/api/session-replay/recordings/[recordingId]/manifest.get.ts +1 -0
- package/corpus/templates/analytics/server/routes/api/session-replay/recordings/[recordingId].get.ts +1 -0
- package/corpus/templates/analytics/server/routes/api/session-replay/recordings.get.ts +1 -0
- package/corpus/templates/assets/.agents/skills/asset-generation/SKILL.md +10 -2
- package/corpus/templates/assets/.agents/skills/image-generation/SKILL.md +14 -7
- package/corpus/templates/assets/.agents/skills/library-management/SKILL.md +12 -0
- package/corpus/templates/assets/AGENTS.md +18 -3
- package/corpus/templates/assets/actions/_image-model-default.ts +24 -0
- package/corpus/templates/assets/actions/dismiss-variant-slots.ts +15 -22
- package/corpus/templates/assets/actions/generate-asset.ts +30 -23
- package/corpus/templates/assets/actions/generate-image-batch.ts +76 -29
- package/corpus/templates/assets/actions/generate-image.ts +46 -30
- package/corpus/templates/assets/actions/generate-video.ts +18 -3
- package/corpus/templates/assets/actions/list-assets.ts +39 -8
- package/corpus/templates/assets/actions/list-libraries.ts +24 -2
- package/corpus/templates/assets/actions/navigate.ts +1 -1
- package/corpus/templates/assets/actions/open-asset-picker.ts +38 -1
- package/corpus/templates/assets/actions/refresh-generation-run.ts +10 -0
- package/corpus/templates/assets/actions/save-generated-image.ts +21 -19
- package/corpus/templates/assets/actions/variant-slots.ts +131 -28
- package/corpus/templates/assets/actions/view-screen.ts +9 -0
- package/corpus/templates/assets/app/components/generation/GenerationResults.tsx +425 -0
- package/corpus/templates/assets/app/components/layout/Header.tsx +3 -1
- package/corpus/templates/assets/app/components/layout/Layout.tsx +5 -0
- package/corpus/templates/assets/app/components/layout/Sidebar.tsx +4 -6
- package/corpus/templates/assets/app/hooks/use-navigation-state.ts +47 -21
- package/corpus/templates/assets/app/i18n-data.ts +1815 -0
- package/corpus/templates/assets/app/lib/libraries.ts +6 -0
- package/corpus/templates/assets/app/lib/picker-chat-handoff.ts +71 -0
- package/corpus/templates/assets/app/routes/_index.tsx +4 -0
- package/corpus/templates/assets/app/routes/asset.$id.tsx +2 -2
- package/corpus/templates/assets/app/routes/audit.tsx +1 -1
- package/corpus/templates/assets/app/routes/brand-kits.$id.tsx +1557 -2164
- package/corpus/templates/assets/app/routes/brand-kits._index.tsx +7 -191
- package/corpus/templates/assets/app/routes/brand-kits.tsx +1 -5
- package/corpus/templates/assets/app/routes/libraries.tsx +4 -4
- package/corpus/templates/assets/app/routes/library.$id.tsx +9 -7
- package/corpus/templates/assets/app/routes/library.tsx +1454 -175
- package/corpus/templates/assets/server/db/index.ts +1 -1
- package/corpus/templates/assets/server/lib/generation.ts +86 -15
- package/corpus/templates/assets/server/plugins/agent-chat.ts +196 -0
- package/corpus/templates/assets/server/plugins/db.ts +5 -0
- package/corpus/templates/assets/shared/api.ts +6 -3
- package/dist/agent/types.d.ts +21 -0
- package/dist/agent/types.d.ts.map +1 -1
- package/dist/agent/types.js.map +1 -1
- package/dist/client/AgentPanel.d.ts +6 -2
- package/dist/client/AgentPanel.d.ts.map +1 -1
- package/dist/client/AgentPanel.js +10 -4
- package/dist/client/AgentPanel.js.map +1 -1
- package/dist/client/AssistantChat.d.ts +6 -0
- package/dist/client/AssistantChat.d.ts.map +1 -1
- package/dist/client/AssistantChat.js +15 -3
- package/dist/client/AssistantChat.js.map +1 -1
- package/dist/client/agent-chat.d.ts +32 -0
- package/dist/client/agent-chat.d.ts.map +1 -1
- package/dist/client/agent-chat.js +113 -0
- package/dist/client/agent-chat.js.map +1 -1
- package/dist/client/analytics.d.ts +22 -3
- package/dist/client/analytics.d.ts.map +1 -1
- package/dist/client/analytics.js +96 -4
- package/dist/client/analytics.js.map +1 -1
- package/dist/client/composer/TiptapComposer.d.ts +2 -1
- package/dist/client/composer/TiptapComposer.d.ts.map +1 -1
- package/dist/client/composer/TiptapComposer.js +228 -56
- package/dist/client/composer/TiptapComposer.js.map +1 -1
- package/dist/client/composer/extensions/MentionReference.d.ts.map +1 -1
- package/dist/client/composer/extensions/MentionReference.js +3 -0
- package/dist/client/composer/extensions/MentionReference.js.map +1 -1
- package/dist/client/composer/types.d.ts +21 -0
- package/dist/client/composer/types.d.ts.map +1 -1
- package/dist/client/composer/types.js.map +1 -1
- package/dist/client/index.d.ts +2 -2
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +2 -2
- package/dist/client/index.js.map +1 -1
- package/dist/client/session-replay.d.ts +41 -0
- package/dist/client/session-replay.d.ts.map +1 -0
- package/dist/client/session-replay.js +595 -0
- package/dist/client/session-replay.js.map +1 -0
- package/dist/collab/awareness.d.ts +2 -2
- package/dist/collab/awareness.d.ts.map +1 -1
- package/dist/collab/routes.d.ts +2 -2
- package/dist/db/schema.d.ts +3 -1
- package/dist/db/schema.d.ts.map +1 -1
- package/dist/db/schema.js +6 -2
- package/dist/db/schema.js.map +1 -1
- package/dist/deploy/build.d.ts +8 -0
- package/dist/deploy/build.d.ts.map +1 -1
- package/dist/deploy/build.js +66 -0
- package/dist/deploy/build.js.map +1 -1
- package/dist/file-upload/actions/upload-image.d.ts +2 -2
- package/dist/notifications/routes.d.ts +3 -3
- package/dist/observability/routes.d.ts +8 -8
- package/dist/private-blob/index.d.ts +3 -0
- package/dist/private-blob/index.d.ts.map +1 -0
- package/dist/private-blob/index.js +2 -0
- package/dist/private-blob/index.js.map +1 -0
- package/dist/private-blob/registry.d.ts +10 -0
- package/dist/private-blob/registry.d.ts.map +1 -0
- package/dist/private-blob/registry.js +152 -0
- package/dist/private-blob/registry.js.map +1 -0
- package/dist/private-blob/types.d.ts +51 -0
- package/dist/private-blob/types.d.ts.map +1 -0
- package/dist/private-blob/types.js +2 -0
- package/dist/private-blob/types.js.map +1 -0
- package/dist/resources/handlers.d.ts +3 -3
- package/dist/scripts/utils.d.ts +4 -4
- package/dist/scripts/utils.d.ts.map +1 -1
- package/dist/scripts/utils.js +30 -6
- package/dist/scripts/utils.js.map +1 -1
- package/dist/server/agent-chat-plugin.d.ts.map +1 -1
- package/dist/server/agent-chat-plugin.js +5 -0
- package/dist/server/agent-chat-plugin.js.map +1 -1
- package/dist/server/agent-engine-api-key-route.d.ts +2 -2
- package/dist/server/email.d.ts +2 -0
- package/dist/server/email.d.ts.map +1 -1
- package/dist/server/email.js +3 -0
- package/dist/server/email.js.map +1 -1
- package/dist/server/index.d.ts +1 -1
- package/dist/server/index.d.ts.map +1 -1
- package/dist/server/index.js.map +1 -1
- package/dist/server/transcribe-voice.d.ts +1 -1
- package/dist/styles/agent-native.css +24 -0
- package/docs/content/template-analytics.md +5 -0
- package/docs/content/tracking.md +45 -0
- package/package.json +3 -1
- package/corpus/templates/analytics/app/pages/overview/OverviewPage.tsx +0 -209
|
@@ -0,0 +1,800 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getAnalyticsSessionId,
|
|
3
|
+
getAnalyticsAnonymousId,
|
|
4
|
+
scrubUrl,
|
|
5
|
+
} from "./analytics.js";
|
|
6
|
+
|
|
7
|
+
type ReplayEvent = Record<string, unknown>;
|
|
8
|
+
type ReplayStopFn = () => void;
|
|
9
|
+
export type SessionReplayUrlMatcher =
|
|
10
|
+
| string
|
|
11
|
+
| RegExp
|
|
12
|
+
| ((url: string) => boolean);
|
|
13
|
+
|
|
14
|
+
interface RrwebRecordOptions {
|
|
15
|
+
emit: (event: ReplayEvent) => void;
|
|
16
|
+
checkoutEveryNth?: number;
|
|
17
|
+
checkoutEveryNms?: number;
|
|
18
|
+
blockClass?: string | RegExp;
|
|
19
|
+
blockSelector?: string;
|
|
20
|
+
ignoreClass?: string | RegExp;
|
|
21
|
+
ignoreSelector?: string;
|
|
22
|
+
maskTextClass?: string | RegExp;
|
|
23
|
+
maskTextSelector?: string;
|
|
24
|
+
maskAllInputs?: boolean;
|
|
25
|
+
maskInputOptions?: Record<string, boolean>;
|
|
26
|
+
recordCanvas?: boolean;
|
|
27
|
+
recordCrossOriginIframes?: boolean;
|
|
28
|
+
collectFonts?: boolean;
|
|
29
|
+
inlineImages?: boolean;
|
|
30
|
+
sampling?: Record<string, unknown>;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
interface RrwebRecordModule {
|
|
34
|
+
record: (options: RrwebRecordOptions) => ReplayStopFn | undefined;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
interface SessionReplayState {
|
|
38
|
+
active: boolean;
|
|
39
|
+
replayId: string | null;
|
|
40
|
+
sequence: number;
|
|
41
|
+
queue: ReplayEvent[];
|
|
42
|
+
queuedBytes: number;
|
|
43
|
+
flushTimer: number | null;
|
|
44
|
+
maxDurationTimer: number | null;
|
|
45
|
+
flushing: boolean;
|
|
46
|
+
stopRecorder: ReplayStopFn | null;
|
|
47
|
+
restoreUrlMonitor: (() => void) | null;
|
|
48
|
+
removeLifecycleListeners: (() => void) | null;
|
|
49
|
+
options: NormalizedSessionReplayOptions | null;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
interface StoredReplaySession {
|
|
53
|
+
sessionId?: string;
|
|
54
|
+
replayId?: string;
|
|
55
|
+
sequence?: number;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export interface SessionReplayOptions {
|
|
59
|
+
enabled?: boolean;
|
|
60
|
+
publicKey?: string;
|
|
61
|
+
endpoint?: string;
|
|
62
|
+
sampleRate?: number;
|
|
63
|
+
samplingSalt?: string;
|
|
64
|
+
allowUrls?: SessionReplayUrlMatcher[];
|
|
65
|
+
blockUrls?: SessionReplayUrlMatcher[];
|
|
66
|
+
flushIntervalMs?: number;
|
|
67
|
+
maxDurationMs?: number;
|
|
68
|
+
maxEventsPerBatch?: number;
|
|
69
|
+
maxBatchBytes?: number;
|
|
70
|
+
checkoutEveryNth?: number;
|
|
71
|
+
checkoutEveryNms?: number;
|
|
72
|
+
blockSelector?: string;
|
|
73
|
+
ignoreSelector?: string;
|
|
74
|
+
maskTextClass?: string | RegExp;
|
|
75
|
+
maskTextSelector?: string;
|
|
76
|
+
maskAllInputs?: boolean;
|
|
77
|
+
recordCanvas?: boolean;
|
|
78
|
+
recordCrossOriginIframes?: boolean;
|
|
79
|
+
collectFonts?: boolean;
|
|
80
|
+
inlineImages?: boolean;
|
|
81
|
+
extraProperties?:
|
|
82
|
+
| Record<string, unknown>
|
|
83
|
+
| (() => Record<string, unknown> | undefined);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export interface SessionReplayStartResult {
|
|
87
|
+
started: boolean;
|
|
88
|
+
reason?:
|
|
89
|
+
| "disabled"
|
|
90
|
+
| "not-browser"
|
|
91
|
+
| "missing-public-key"
|
|
92
|
+
| "missing-session-id"
|
|
93
|
+
| "sampled-out"
|
|
94
|
+
| "url-blocked"
|
|
95
|
+
| "already-active"
|
|
96
|
+
| "import-failed"
|
|
97
|
+
| "record-failed";
|
|
98
|
+
replayId?: string;
|
|
99
|
+
sessionId?: string;
|
|
100
|
+
sampled?: boolean;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
interface NormalizedSessionReplayOptions {
|
|
104
|
+
publicKey: string;
|
|
105
|
+
endpoint: string;
|
|
106
|
+
sampleRate: number;
|
|
107
|
+
samplingSalt: string;
|
|
108
|
+
allowUrls: SessionReplayUrlMatcher[];
|
|
109
|
+
blockUrls: SessionReplayUrlMatcher[];
|
|
110
|
+
flushIntervalMs: number;
|
|
111
|
+
maxDurationMs: number;
|
|
112
|
+
maxEventsPerBatch: number;
|
|
113
|
+
maxBatchBytes: number;
|
|
114
|
+
checkoutEveryNth?: number;
|
|
115
|
+
checkoutEveryNms?: number;
|
|
116
|
+
blockSelector: string;
|
|
117
|
+
ignoreSelector: string;
|
|
118
|
+
maskTextClass: string | RegExp;
|
|
119
|
+
maskTextSelector: string;
|
|
120
|
+
maskAllInputs: boolean;
|
|
121
|
+
recordCanvas: boolean;
|
|
122
|
+
recordCrossOriginIframes: boolean;
|
|
123
|
+
collectFonts: boolean;
|
|
124
|
+
inlineImages: boolean;
|
|
125
|
+
extraProperties?: SessionReplayOptions["extraProperties"];
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const DEFAULT_REPLAY_PATH = "/api/analytics/replay";
|
|
129
|
+
const DEFAULT_SAMPLING_SALT = "agent-native-session-replay";
|
|
130
|
+
const SESSION_REPLAY_STATE_KEY = Symbol.for(
|
|
131
|
+
"agent-native.client.sessionReplay",
|
|
132
|
+
);
|
|
133
|
+
const SESSION_REPLAY_ID_STORAGE_KEY = "agent-native.session_replay_id";
|
|
134
|
+
const DEFAULT_BLOCK_SELECTOR = [
|
|
135
|
+
"[data-sensitive]",
|
|
136
|
+
"[data-an-block]",
|
|
137
|
+
"[data-an-private]",
|
|
138
|
+
"[data-private]",
|
|
139
|
+
".an-block",
|
|
140
|
+
".an-replay-block",
|
|
141
|
+
".an-private",
|
|
142
|
+
".rr-block",
|
|
143
|
+
"[autocomplete='cc-number']",
|
|
144
|
+
"[autocomplete='cc-csc']",
|
|
145
|
+
"[autocomplete='cc-exp']",
|
|
146
|
+
"[name*='password' i]",
|
|
147
|
+
"[name*='credit' i]",
|
|
148
|
+
"[name*='card' i]",
|
|
149
|
+
"[name*='ssn' i]",
|
|
150
|
+
].join(", ");
|
|
151
|
+
const DEFAULT_IGNORE_SELECTOR = ".an-ignore, [data-an-ignore]";
|
|
152
|
+
const DEFAULT_MASK_TEXT_CLASS = "an-mask";
|
|
153
|
+
const DEFAULT_MASK_TEXT_SELECTOR = "[data-an-mask]";
|
|
154
|
+
const DEFAULT_FLUSH_INTERVAL_MS = 5000;
|
|
155
|
+
const DEFAULT_MAX_DURATION_MS = 30 * 60 * 1000;
|
|
156
|
+
const DEFAULT_MAX_EVENTS_PER_BATCH = 50;
|
|
157
|
+
const DEFAULT_MAX_BATCH_BYTES = 256 * 1024;
|
|
158
|
+
const URL_LIKE_KEYS = new Set([
|
|
159
|
+
"url",
|
|
160
|
+
"uri",
|
|
161
|
+
"href",
|
|
162
|
+
"src",
|
|
163
|
+
"currentUrl",
|
|
164
|
+
"referrer",
|
|
165
|
+
"from",
|
|
166
|
+
"to",
|
|
167
|
+
]);
|
|
168
|
+
|
|
169
|
+
function getState(): SessionReplayState {
|
|
170
|
+
const g = globalThis as typeof globalThis & {
|
|
171
|
+
[SESSION_REPLAY_STATE_KEY]?: SessionReplayState;
|
|
172
|
+
};
|
|
173
|
+
if (!g[SESSION_REPLAY_STATE_KEY]) {
|
|
174
|
+
g[SESSION_REPLAY_STATE_KEY] = {
|
|
175
|
+
active: false,
|
|
176
|
+
replayId: null,
|
|
177
|
+
sequence: 0,
|
|
178
|
+
queue: [],
|
|
179
|
+
queuedBytes: 0,
|
|
180
|
+
flushTimer: null,
|
|
181
|
+
maxDurationTimer: null,
|
|
182
|
+
flushing: false,
|
|
183
|
+
stopRecorder: null,
|
|
184
|
+
restoreUrlMonitor: null,
|
|
185
|
+
removeLifecycleListeners: null,
|
|
186
|
+
options: null,
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
return g[SESSION_REPLAY_STATE_KEY]!;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function safeStorageGet(key: string): string | null {
|
|
193
|
+
try {
|
|
194
|
+
return window.localStorage.getItem(key);
|
|
195
|
+
} catch {
|
|
196
|
+
return null;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function safeStorageSet(key: string, value: string): void {
|
|
201
|
+
try {
|
|
202
|
+
window.localStorage.setItem(key, value);
|
|
203
|
+
} catch {
|
|
204
|
+
// private browsing / storage disabled -- replay still works for this page
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function generateReplayId(): string {
|
|
209
|
+
try {
|
|
210
|
+
if (
|
|
211
|
+
typeof crypto !== "undefined" &&
|
|
212
|
+
typeof crypto.randomUUID === "function"
|
|
213
|
+
) {
|
|
214
|
+
return crypto.randomUUID();
|
|
215
|
+
}
|
|
216
|
+
} catch {
|
|
217
|
+
// fall through
|
|
218
|
+
}
|
|
219
|
+
return `${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}`;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function readStoredReplaySession(): StoredReplaySession | null {
|
|
223
|
+
const raw = safeStorageGet(SESSION_REPLAY_ID_STORAGE_KEY);
|
|
224
|
+
if (!raw) return null;
|
|
225
|
+
try {
|
|
226
|
+
const parsed = JSON.parse(raw) as StoredReplaySession;
|
|
227
|
+
if (!parsed || typeof parsed !== "object") return null;
|
|
228
|
+
return parsed;
|
|
229
|
+
} catch {
|
|
230
|
+
return null;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
function writeStoredReplaySession(value: StoredReplaySession): void {
|
|
235
|
+
safeStorageSet(SESSION_REPLAY_ID_STORAGE_KEY, JSON.stringify(value));
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
function getOrCreateReplaySession(sessionId: string): {
|
|
239
|
+
replayId: string;
|
|
240
|
+
sequence: number;
|
|
241
|
+
} {
|
|
242
|
+
const parsed = readStoredReplaySession();
|
|
243
|
+
if (parsed?.sessionId === sessionId && parsed.replayId) {
|
|
244
|
+
const sequence =
|
|
245
|
+
typeof parsed.sequence === "number" &&
|
|
246
|
+
Number.isFinite(parsed.sequence) &&
|
|
247
|
+
parsed.sequence >= 0
|
|
248
|
+
? Math.floor(parsed.sequence)
|
|
249
|
+
: 0;
|
|
250
|
+
return { replayId: parsed.replayId, sequence };
|
|
251
|
+
}
|
|
252
|
+
const replayId = generateReplayId();
|
|
253
|
+
writeStoredReplaySession({ sessionId, replayId, sequence: 0 });
|
|
254
|
+
return { replayId, sequence: 0 };
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
function persistReplaySequence(
|
|
258
|
+
sessionId: string,
|
|
259
|
+
replayId: string,
|
|
260
|
+
sequence: number,
|
|
261
|
+
): void {
|
|
262
|
+
writeStoredReplaySession({ sessionId, replayId, sequence });
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
function clampSamplingRate(value: number | undefined): number {
|
|
266
|
+
if (typeof value !== "number" || Number.isNaN(value)) return 1;
|
|
267
|
+
return Math.min(1, Math.max(0, value));
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
function readEnvString(key: string): string | undefined {
|
|
271
|
+
return (import.meta.env as Record<string, string | undefined>)?.[key]?.trim();
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
function readEnvNumber(key: string): number | undefined {
|
|
275
|
+
const raw = readEnvString(key);
|
|
276
|
+
if (!raw) return undefined;
|
|
277
|
+
const value = Number(raw);
|
|
278
|
+
return Number.isFinite(value) ? value : undefined;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
function readFirstEnvString(keys: string[]): string | undefined {
|
|
282
|
+
for (const key of keys) {
|
|
283
|
+
const value = readEnvString(key);
|
|
284
|
+
if (value) return value;
|
|
285
|
+
}
|
|
286
|
+
return undefined;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
function readFirstEnvNumber(keys: string[]): number | undefined {
|
|
290
|
+
for (const key of keys) {
|
|
291
|
+
const value = readEnvNumber(key);
|
|
292
|
+
if (value !== undefined) return value;
|
|
293
|
+
}
|
|
294
|
+
return undefined;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
function replayEndpointFromAnalyticsEndpoint(value: string): string | null {
|
|
298
|
+
try {
|
|
299
|
+
const url = new URL(value);
|
|
300
|
+
if (url.pathname.endsWith("/api/analytics/track")) {
|
|
301
|
+
url.pathname = url.pathname.replace(
|
|
302
|
+
/\/api\/analytics\/track$/,
|
|
303
|
+
"/api/analytics/replay",
|
|
304
|
+
);
|
|
305
|
+
return url.toString();
|
|
306
|
+
}
|
|
307
|
+
if (url.pathname.endsWith("/track")) {
|
|
308
|
+
url.pathname = url.pathname.replace(/\/track$/, "/api/analytics/replay");
|
|
309
|
+
return url.toString();
|
|
310
|
+
}
|
|
311
|
+
} catch {
|
|
312
|
+
// Fall through to the relative path cases below.
|
|
313
|
+
}
|
|
314
|
+
if (value.endsWith("/api/analytics/track")) {
|
|
315
|
+
return value.replace(/\/api\/analytics\/track$/, "/api/analytics/replay");
|
|
316
|
+
}
|
|
317
|
+
if (value.endsWith("/track")) {
|
|
318
|
+
return value.replace(/\/track$/, "/api/analytics/replay");
|
|
319
|
+
}
|
|
320
|
+
return null;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
function defaultReplayEndpoint(): string {
|
|
324
|
+
const analyticsEndpoint = readEnvString(
|
|
325
|
+
"VITE_AGENT_NATIVE_ANALYTICS_ENDPOINT",
|
|
326
|
+
);
|
|
327
|
+
const derived = analyticsEndpoint
|
|
328
|
+
? replayEndpointFromAnalyticsEndpoint(analyticsEndpoint)
|
|
329
|
+
: null;
|
|
330
|
+
if (derived) return derived;
|
|
331
|
+
return typeof window !== "undefined"
|
|
332
|
+
? `${window.location.origin}${DEFAULT_REPLAY_PATH}`
|
|
333
|
+
: DEFAULT_REPLAY_PATH;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
export function getSessionReplaySamplingScore(
|
|
337
|
+
sessionId: string,
|
|
338
|
+
salt = DEFAULT_SAMPLING_SALT,
|
|
339
|
+
): number {
|
|
340
|
+
let hash = 2166136261;
|
|
341
|
+
const input = `${salt}:${sessionId}`;
|
|
342
|
+
for (let i = 0; i < input.length; i += 1) {
|
|
343
|
+
hash ^= input.charCodeAt(i);
|
|
344
|
+
hash = Math.imul(hash, 16777619);
|
|
345
|
+
}
|
|
346
|
+
return (hash >>> 0) / 4294967296;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
export function shouldSampleSessionReplay(
|
|
350
|
+
sessionId: string,
|
|
351
|
+
sampleRate = 1,
|
|
352
|
+
salt = DEFAULT_SAMPLING_SALT,
|
|
353
|
+
): boolean {
|
|
354
|
+
const rate = clampSamplingRate(sampleRate);
|
|
355
|
+
if (rate <= 0) return false;
|
|
356
|
+
if (rate >= 1) return true;
|
|
357
|
+
return getSessionReplaySamplingScore(sessionId, salt) < rate;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
function matcherAllows(matcher: SessionReplayUrlMatcher, url: string): boolean {
|
|
361
|
+
if (typeof matcher === "string") return url.includes(matcher);
|
|
362
|
+
if (matcher instanceof RegExp) return matcher.test(url);
|
|
363
|
+
return matcher(url);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
function isUrlRecordable(
|
|
367
|
+
url: string,
|
|
368
|
+
options: Pick<NormalizedSessionReplayOptions, "allowUrls" | "blockUrls">,
|
|
369
|
+
): boolean {
|
|
370
|
+
if (options.allowUrls.length > 0) {
|
|
371
|
+
const allowed = options.allowUrls.some((matcher) =>
|
|
372
|
+
matcherAllows(matcher, url),
|
|
373
|
+
);
|
|
374
|
+
if (!allowed) return false;
|
|
375
|
+
}
|
|
376
|
+
return !options.blockUrls.some((matcher) => matcherAllows(matcher, url));
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
function normalizeOptions(
|
|
380
|
+
options: SessionReplayOptions,
|
|
381
|
+
): NormalizedSessionReplayOptions | null {
|
|
382
|
+
const publicKey =
|
|
383
|
+
options.publicKey ||
|
|
384
|
+
readFirstEnvString([
|
|
385
|
+
"VITE_AGENT_NATIVE_SESSION_REPLAY_PUBLIC_KEY",
|
|
386
|
+
"VITE_SESSION_REPLAY_PUBLIC_KEY",
|
|
387
|
+
]) ||
|
|
388
|
+
(import.meta.env as Record<string, string | undefined>)
|
|
389
|
+
?.VITE_AGENT_NATIVE_ANALYTICS_PUBLIC_KEY;
|
|
390
|
+
if (!publicKey) return null;
|
|
391
|
+
const endpoint =
|
|
392
|
+
options.endpoint ||
|
|
393
|
+
readFirstEnvString([
|
|
394
|
+
"VITE_AGENT_NATIVE_ANALYTICS_REPLAY_ENDPOINT",
|
|
395
|
+
"VITE_AGENT_NATIVE_SESSION_REPLAY_ENDPOINT",
|
|
396
|
+
"VITE_SESSION_REPLAY_INGEST_URL",
|
|
397
|
+
]) ||
|
|
398
|
+
defaultReplayEndpoint();
|
|
399
|
+
return {
|
|
400
|
+
publicKey,
|
|
401
|
+
endpoint,
|
|
402
|
+
sampleRate: clampSamplingRate(
|
|
403
|
+
options.sampleRate ??
|
|
404
|
+
readFirstEnvNumber([
|
|
405
|
+
"VITE_AGENT_NATIVE_SESSION_REPLAY_SAMPLE_RATE",
|
|
406
|
+
"VITE_SESSION_REPLAY_SAMPLE_RATE",
|
|
407
|
+
]),
|
|
408
|
+
),
|
|
409
|
+
samplingSalt: options.samplingSalt || DEFAULT_SAMPLING_SALT,
|
|
410
|
+
allowUrls: options.allowUrls ?? [],
|
|
411
|
+
blockUrls: options.blockUrls ?? [],
|
|
412
|
+
flushIntervalMs: Math.max(
|
|
413
|
+
250,
|
|
414
|
+
options.flushIntervalMs ??
|
|
415
|
+
readFirstEnvNumber([
|
|
416
|
+
"VITE_AGENT_NATIVE_SESSION_REPLAY_CHUNK_INTERVAL_MS",
|
|
417
|
+
"VITE_SESSION_REPLAY_CHUNK_INTERVAL_MS",
|
|
418
|
+
]) ??
|
|
419
|
+
DEFAULT_FLUSH_INTERVAL_MS,
|
|
420
|
+
),
|
|
421
|
+
maxDurationMs: Math.max(
|
|
422
|
+
1000,
|
|
423
|
+
options.maxDurationMs ??
|
|
424
|
+
readFirstEnvNumber([
|
|
425
|
+
"VITE_AGENT_NATIVE_SESSION_REPLAY_MAX_DURATION_MS",
|
|
426
|
+
"VITE_SESSION_REPLAY_MAX_DURATION_MS",
|
|
427
|
+
]) ??
|
|
428
|
+
DEFAULT_MAX_DURATION_MS,
|
|
429
|
+
),
|
|
430
|
+
maxEventsPerBatch: Math.max(
|
|
431
|
+
1,
|
|
432
|
+
options.maxEventsPerBatch ?? DEFAULT_MAX_EVENTS_PER_BATCH,
|
|
433
|
+
),
|
|
434
|
+
maxBatchBytes: Math.max(
|
|
435
|
+
1024,
|
|
436
|
+
options.maxBatchBytes ??
|
|
437
|
+
readFirstEnvNumber([
|
|
438
|
+
"VITE_AGENT_NATIVE_SESSION_REPLAY_CHUNK_MAX_BYTES",
|
|
439
|
+
"VITE_SESSION_REPLAY_CHUNK_MAX_BYTES",
|
|
440
|
+
]) ??
|
|
441
|
+
DEFAULT_MAX_BATCH_BYTES,
|
|
442
|
+
),
|
|
443
|
+
checkoutEveryNth: options.checkoutEveryNth,
|
|
444
|
+
checkoutEveryNms: options.checkoutEveryNms,
|
|
445
|
+
blockSelector: options.blockSelector || DEFAULT_BLOCK_SELECTOR,
|
|
446
|
+
ignoreSelector: options.ignoreSelector || DEFAULT_IGNORE_SELECTOR,
|
|
447
|
+
maskTextClass: options.maskTextClass || DEFAULT_MASK_TEXT_CLASS,
|
|
448
|
+
maskTextSelector: options.maskTextSelector || DEFAULT_MASK_TEXT_SELECTOR,
|
|
449
|
+
maskAllInputs: options.maskAllInputs ?? true,
|
|
450
|
+
recordCanvas: options.recordCanvas ?? false,
|
|
451
|
+
recordCrossOriginIframes: options.recordCrossOriginIframes ?? false,
|
|
452
|
+
collectFonts: options.collectFonts ?? false,
|
|
453
|
+
inlineImages: options.inlineImages ?? false,
|
|
454
|
+
extraProperties: options.extraProperties,
|
|
455
|
+
};
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
function scrubStringValue(key: string, value: string): string {
|
|
459
|
+
const lowerKey = key.toLowerCase();
|
|
460
|
+
const isUrlKey = URL_LIKE_KEYS.has(key) || URL_LIKE_KEYS.has(lowerKey);
|
|
461
|
+
if (
|
|
462
|
+
isUrlKey ||
|
|
463
|
+
value.startsWith("http://") ||
|
|
464
|
+
value.startsWith("https://") ||
|
|
465
|
+
value.startsWith("/")
|
|
466
|
+
) {
|
|
467
|
+
return scrubUrl(value) ?? value;
|
|
468
|
+
}
|
|
469
|
+
return value;
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
function scrubReplayValue(
|
|
473
|
+
value: unknown,
|
|
474
|
+
key = "",
|
|
475
|
+
depth = 0,
|
|
476
|
+
seen = new WeakSet<object>(),
|
|
477
|
+
): unknown {
|
|
478
|
+
if (typeof value === "string") return scrubStringValue(key, value);
|
|
479
|
+
if (!value || typeof value !== "object") return value;
|
|
480
|
+
if (depth > 12) return value;
|
|
481
|
+
if (seen.has(value)) return value;
|
|
482
|
+
seen.add(value);
|
|
483
|
+
if (Array.isArray(value)) {
|
|
484
|
+
return value.map((item) => scrubReplayValue(item, key, depth + 1, seen));
|
|
485
|
+
}
|
|
486
|
+
const out: Record<string, unknown> = {};
|
|
487
|
+
for (const [childKey, childValue] of Object.entries(value)) {
|
|
488
|
+
out[childKey] = scrubReplayValue(childValue, childKey, depth + 1, seen);
|
|
489
|
+
}
|
|
490
|
+
return out;
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
function scrubReplayEvent(event: ReplayEvent): ReplayEvent {
|
|
494
|
+
return scrubReplayValue(event) as ReplayEvent;
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
function estimateJsonBytes(value: unknown): number {
|
|
498
|
+
try {
|
|
499
|
+
return JSON.stringify(value).length;
|
|
500
|
+
} catch {
|
|
501
|
+
return 0;
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
function enqueueReplayEvent(
|
|
506
|
+
state: SessionReplayState,
|
|
507
|
+
event: ReplayEvent,
|
|
508
|
+
): void {
|
|
509
|
+
if (!state.options) return;
|
|
510
|
+
const sanitized = scrubReplayEvent(event);
|
|
511
|
+
const estimatedBytes = estimateJsonBytes(sanitized);
|
|
512
|
+
if (
|
|
513
|
+
state.queue.length > 0 &&
|
|
514
|
+
state.queuedBytes + estimatedBytes > state.options.maxBatchBytes
|
|
515
|
+
) {
|
|
516
|
+
void flushSessionReplay("max-bytes");
|
|
517
|
+
}
|
|
518
|
+
state.queue.push(sanitized);
|
|
519
|
+
state.queuedBytes += estimatedBytes;
|
|
520
|
+
if (state.queue.length >= state.options.maxEventsPerBatch) {
|
|
521
|
+
void flushSessionReplay("max-events");
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
function replayExtraProperties(
|
|
526
|
+
options: NormalizedSessionReplayOptions,
|
|
527
|
+
): Record<string, unknown> | undefined {
|
|
528
|
+
const source = options.extraProperties;
|
|
529
|
+
if (!source) return undefined;
|
|
530
|
+
try {
|
|
531
|
+
const props = typeof source === "function" ? source() : source;
|
|
532
|
+
if (!props || typeof props !== "object") return undefined;
|
|
533
|
+
return scrubReplayValue(props) as Record<string, unknown>;
|
|
534
|
+
} catch {
|
|
535
|
+
return undefined;
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
function buildReplayBody(
|
|
540
|
+
state: SessionReplayState,
|
|
541
|
+
reason: string,
|
|
542
|
+
events: ReplayEvent[],
|
|
543
|
+
): string | null {
|
|
544
|
+
const options = state.options;
|
|
545
|
+
if (!options || !state.replayId) return null;
|
|
546
|
+
const sessionId = getAnalyticsSessionId();
|
|
547
|
+
if (!sessionId) return null;
|
|
548
|
+
const body = {
|
|
549
|
+
publicKey: options.publicKey,
|
|
550
|
+
type: "session_replay",
|
|
551
|
+
replayId: state.replayId,
|
|
552
|
+
sessionId,
|
|
553
|
+
anonymousId: getAnalyticsAnonymousId(),
|
|
554
|
+
sequence: state.sequence,
|
|
555
|
+
reason,
|
|
556
|
+
status: isFinalFlushReason(reason) ? "completed" : "active",
|
|
557
|
+
privacyMode: "mask-inputs-and-selected-text",
|
|
558
|
+
url:
|
|
559
|
+
typeof window !== "undefined"
|
|
560
|
+
? scrubUrl(window.location.href)
|
|
561
|
+
: undefined,
|
|
562
|
+
timestamp: new Date().toISOString(),
|
|
563
|
+
properties: replayExtraProperties(options),
|
|
564
|
+
events,
|
|
565
|
+
};
|
|
566
|
+
state.sequence += 1;
|
|
567
|
+
persistReplaySequence(sessionId, state.replayId, state.sequence);
|
|
568
|
+
return JSON.stringify(body);
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
function isFinalFlushReason(reason: string): boolean {
|
|
572
|
+
return [
|
|
573
|
+
"manual",
|
|
574
|
+
"pagehide",
|
|
575
|
+
"beforeunload",
|
|
576
|
+
"url-blocked",
|
|
577
|
+
"max-duration",
|
|
578
|
+
].includes(reason);
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
export async function flushSessionReplay(reason = "manual"): Promise<void> {
|
|
582
|
+
const state = getState();
|
|
583
|
+
if (!state.options || state.queue.length === 0 || state.flushing) return;
|
|
584
|
+
const events = state.queue.splice(0, state.queue.length);
|
|
585
|
+
state.queuedBytes = 0;
|
|
586
|
+
const body = buildReplayBody(state, reason, events);
|
|
587
|
+
if (!body || !state.options) return;
|
|
588
|
+
state.flushing = true;
|
|
589
|
+
try {
|
|
590
|
+
if (navigator.sendBeacon) {
|
|
591
|
+
const sent = navigator.sendBeacon(state.options.endpoint, body);
|
|
592
|
+
if (sent) return;
|
|
593
|
+
}
|
|
594
|
+
await fetch(state.options.endpoint, {
|
|
595
|
+
method: "POST",
|
|
596
|
+
body,
|
|
597
|
+
keepalive: true,
|
|
598
|
+
headers: {
|
|
599
|
+
"Content-Type": "text/plain;charset=UTF-8",
|
|
600
|
+
"X-Agent-Native-Analytics-Key": state.options.publicKey,
|
|
601
|
+
},
|
|
602
|
+
}).catch(() => {});
|
|
603
|
+
} finally {
|
|
604
|
+
state.flushing = false;
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
function installUrlMonitor(state: SessionReplayState): void {
|
|
609
|
+
if (!state.options || state.restoreUrlMonitor) return;
|
|
610
|
+
const options = state.options;
|
|
611
|
+
const check = () => {
|
|
612
|
+
if (!isUrlRecordable(window.location.href, options)) {
|
|
613
|
+
stopSessionReplay("url-blocked");
|
|
614
|
+
}
|
|
615
|
+
};
|
|
616
|
+
const originalPushState = window.history.pushState;
|
|
617
|
+
const originalReplaceState = window.history.replaceState;
|
|
618
|
+
window.history.pushState = function pushState(...args) {
|
|
619
|
+
const result = originalPushState.apply(this, args);
|
|
620
|
+
queueMicrotask(check);
|
|
621
|
+
return result;
|
|
622
|
+
};
|
|
623
|
+
window.history.replaceState = function replaceState(...args) {
|
|
624
|
+
const result = originalReplaceState.apply(this, args);
|
|
625
|
+
queueMicrotask(check);
|
|
626
|
+
return result;
|
|
627
|
+
};
|
|
628
|
+
window.addEventListener("popstate", check);
|
|
629
|
+
state.restoreUrlMonitor = () => {
|
|
630
|
+
window.history.pushState = originalPushState;
|
|
631
|
+
window.history.replaceState = originalReplaceState;
|
|
632
|
+
window.removeEventListener("popstate", check);
|
|
633
|
+
state.restoreUrlMonitor = null;
|
|
634
|
+
};
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
function installLifecycleListeners(state: SessionReplayState): void {
|
|
638
|
+
if (state.removeLifecycleListeners) return;
|
|
639
|
+
const flushOnHidden = () => {
|
|
640
|
+
if (document.visibilityState === "hidden") {
|
|
641
|
+
void flushSessionReplay("visibility-hidden");
|
|
642
|
+
}
|
|
643
|
+
};
|
|
644
|
+
const flushOnUnload = () => {
|
|
645
|
+
void flushSessionReplay("pagehide");
|
|
646
|
+
};
|
|
647
|
+
document.addEventListener("visibilitychange", flushOnHidden);
|
|
648
|
+
window.addEventListener("pagehide", flushOnUnload);
|
|
649
|
+
state.removeLifecycleListeners = () => {
|
|
650
|
+
document.removeEventListener("visibilitychange", flushOnHidden);
|
|
651
|
+
window.removeEventListener("pagehide", flushOnUnload);
|
|
652
|
+
state.removeLifecycleListeners = null;
|
|
653
|
+
};
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
export async function startSessionReplay(
|
|
657
|
+
options: SessionReplayOptions = {},
|
|
658
|
+
): Promise<SessionReplayStartResult> {
|
|
659
|
+
if (options.enabled === false) return { started: false, reason: "disabled" };
|
|
660
|
+
if (typeof window === "undefined" || typeof document === "undefined") {
|
|
661
|
+
return { started: false, reason: "not-browser" };
|
|
662
|
+
}
|
|
663
|
+
const normalized = normalizeOptions(options);
|
|
664
|
+
if (!normalized) return { started: false, reason: "missing-public-key" };
|
|
665
|
+
|
|
666
|
+
const sessionId = getAnalyticsSessionId();
|
|
667
|
+
if (!sessionId) return { started: false, reason: "missing-session-id" };
|
|
668
|
+
const sampled = shouldSampleSessionReplay(
|
|
669
|
+
sessionId,
|
|
670
|
+
normalized.sampleRate,
|
|
671
|
+
normalized.samplingSalt,
|
|
672
|
+
);
|
|
673
|
+
if (!sampled) {
|
|
674
|
+
return { started: false, reason: "sampled-out", sessionId, sampled };
|
|
675
|
+
}
|
|
676
|
+
if (!isUrlRecordable(window.location.href, normalized)) {
|
|
677
|
+
return { started: false, reason: "url-blocked", sessionId, sampled };
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
const state = getState();
|
|
681
|
+
if (state.active && state.replayId) {
|
|
682
|
+
return {
|
|
683
|
+
started: true,
|
|
684
|
+
reason: "already-active",
|
|
685
|
+
replayId: state.replayId,
|
|
686
|
+
sessionId,
|
|
687
|
+
sampled,
|
|
688
|
+
};
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
let rrweb: RrwebRecordModule;
|
|
692
|
+
try {
|
|
693
|
+
rrweb = (await import("@rrweb/record")) as RrwebRecordModule;
|
|
694
|
+
} catch {
|
|
695
|
+
return { started: false, reason: "import-failed", sessionId, sampled };
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
const replaySession = getOrCreateReplaySession(sessionId);
|
|
699
|
+
state.options = normalized;
|
|
700
|
+
state.replayId = replaySession.replayId;
|
|
701
|
+
state.sequence = replaySession.sequence;
|
|
702
|
+
state.queue = [];
|
|
703
|
+
state.queuedBytes = 0;
|
|
704
|
+
state.stopRecorder = null;
|
|
705
|
+
state.active = true;
|
|
706
|
+
|
|
707
|
+
try {
|
|
708
|
+
const stopRecorder = rrweb.record({
|
|
709
|
+
emit: (event) => enqueueReplayEvent(state, event),
|
|
710
|
+
checkoutEveryNth: normalized.checkoutEveryNth,
|
|
711
|
+
checkoutEveryNms: normalized.checkoutEveryNms,
|
|
712
|
+
blockSelector: normalized.blockSelector,
|
|
713
|
+
ignoreSelector: normalized.ignoreSelector,
|
|
714
|
+
maskTextClass: normalized.maskTextClass,
|
|
715
|
+
maskTextSelector: normalized.maskTextSelector,
|
|
716
|
+
maskAllInputs: normalized.maskAllInputs,
|
|
717
|
+
recordCanvas: normalized.recordCanvas,
|
|
718
|
+
recordCrossOriginIframes: normalized.recordCrossOriginIframes,
|
|
719
|
+
collectFonts: normalized.collectFonts,
|
|
720
|
+
inlineImages: normalized.inlineImages,
|
|
721
|
+
maskInputOptions: {
|
|
722
|
+
color: true,
|
|
723
|
+
date: true,
|
|
724
|
+
"datetime-local": true,
|
|
725
|
+
email: true,
|
|
726
|
+
month: true,
|
|
727
|
+
number: true,
|
|
728
|
+
password: true,
|
|
729
|
+
range: true,
|
|
730
|
+
search: true,
|
|
731
|
+
tel: true,
|
|
732
|
+
text: true,
|
|
733
|
+
time: true,
|
|
734
|
+
url: true,
|
|
735
|
+
week: true,
|
|
736
|
+
},
|
|
737
|
+
});
|
|
738
|
+
if (typeof stopRecorder !== "function") {
|
|
739
|
+
state.active = false;
|
|
740
|
+
state.options = null;
|
|
741
|
+
state.replayId = null;
|
|
742
|
+
return { started: false, reason: "record-failed", sessionId, sampled };
|
|
743
|
+
}
|
|
744
|
+
state.stopRecorder = stopRecorder;
|
|
745
|
+
state.flushTimer = window.setInterval(
|
|
746
|
+
() => void flushSessionReplay("interval"),
|
|
747
|
+
normalized.flushIntervalMs,
|
|
748
|
+
);
|
|
749
|
+
state.maxDurationTimer = window.setTimeout(
|
|
750
|
+
() => stopSessionReplay("max-duration"),
|
|
751
|
+
normalized.maxDurationMs,
|
|
752
|
+
);
|
|
753
|
+
installUrlMonitor(state);
|
|
754
|
+
installLifecycleListeners(state);
|
|
755
|
+
return {
|
|
756
|
+
started: true,
|
|
757
|
+
replayId: state.replayId,
|
|
758
|
+
sessionId,
|
|
759
|
+
sampled,
|
|
760
|
+
};
|
|
761
|
+
} catch {
|
|
762
|
+
state.active = false;
|
|
763
|
+
state.options = null;
|
|
764
|
+
state.replayId = null;
|
|
765
|
+
return { started: false, reason: "record-failed", sessionId, sampled };
|
|
766
|
+
}
|
|
767
|
+
}
|
|
768
|
+
|
|
769
|
+
export function stopSessionReplay(reason = "manual"): void {
|
|
770
|
+
const state = getState();
|
|
771
|
+
if (!state.active) return;
|
|
772
|
+
state.active = false;
|
|
773
|
+
try {
|
|
774
|
+
state.stopRecorder?.();
|
|
775
|
+
} catch {
|
|
776
|
+
// best-effort recorder shutdown
|
|
777
|
+
}
|
|
778
|
+
state.stopRecorder = null;
|
|
779
|
+
if (state.flushTimer) {
|
|
780
|
+
window.clearInterval(state.flushTimer);
|
|
781
|
+
state.flushTimer = null;
|
|
782
|
+
}
|
|
783
|
+
if (state.maxDurationTimer) {
|
|
784
|
+
window.clearTimeout(state.maxDurationTimer);
|
|
785
|
+
state.maxDurationTimer = null;
|
|
786
|
+
}
|
|
787
|
+
state.restoreUrlMonitor?.();
|
|
788
|
+
state.removeLifecycleListeners?.();
|
|
789
|
+
void flushSessionReplay(reason);
|
|
790
|
+
}
|
|
791
|
+
|
|
792
|
+
export function maybeStartSessionReplay(
|
|
793
|
+
options: SessionReplayOptions = {},
|
|
794
|
+
): Promise<SessionReplayStartResult> {
|
|
795
|
+
return startSessionReplay(options);
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
export function isSessionReplayActive(): boolean {
|
|
799
|
+
return getState().active;
|
|
800
|
+
}
|