Don't disable analytics in the setup step

This commit is contained in:
Konstantin Wohlwend 2026-06-15 18:16:47 -07:00
parent ef27c98492
commit 2eabf33612
10 changed files with 34 additions and 30 deletions

View File

@ -106,7 +106,7 @@ export const hexclaveClientApp = new HexclaveClientApp({
### Disabling Analytics Capture in the SDK
SDK-managed analytics capture is enabled by default. If you don't want the SDK to collect any analytics, pass `analytics: { enabled: false }` when creating your client app:
SDK-managed analytics capture is enabled by default. You can disable it by disabling the Analytics app in the config or dashboard. If you don't want the SDK to collect any analytics data at all but would like to keep the Analytics app enabled, you can also pass `analytics: { enabled: false }` when creating your client app:
```ts
import { HexclaveClientApp } from "@hexclave/next";
@ -119,7 +119,7 @@ export const hexclaveClientApp = new HexclaveClientApp({
});
```
This stops the SDK from sending `$page-view` and `$click` events. It also resolves the `ANALYTICS_NOT_ENABLED` warning the SDK logs to the browser console when it tries to send events to a project that hasn't enabled the Analytics app — with capture disabled, the SDK never makes those requests. If you'd rather keep analytics, enable the Analytics app in your dashboard (**Apps -> Analytics**) instead.
This stops the SDK from sending `$page-view` and `$click` events. If you'd rather keep analytics, enable the Analytics app in your dashboard (**Apps -> Analytics**) instead.
## Best Practices

File diff suppressed because one or more lines are too long

View File

@ -184,10 +184,6 @@ The frameworks and languages with explicit SDK support are:
});
```
<Note>
The SDK auto-captures page-view and click analytics. To turn this off (and silence the `ANALYTICS_NOT_ENABLED` console warning that appears until you enable the Analytics app in your dashboard), pass `analytics: { enabled: false }`.
</Note>
In a backend where you can keep a secret key safe, you can use the `HexclaveServerApp`, which provides access to more sensitive APIs compared to `HexclaveClientApp`:
```ts src/hexclave/server.ts

View File

@ -87,7 +87,7 @@ If you're building a client-only app and don't have a `HEXCLAVE_SECRET_SERVER_KE
</ParamField>
<ParamField body="analytics" type="object">
Analytics capture configuration. SDK-managed capture is enabled by default; pass `{ enabled: false }` to disable it entirely (which also avoids the `ANALYTICS_NOT_ENABLED` console warning on projects that haven't enabled the Analytics app), or `{ replays: { enabled: true } }` to record session replays.
Analytics capture configuration. SDK-managed capture is enabled by default; pass `{ enabled: false }` to disable it entirely, or `{ replays: { enabled: true } }` to record session replays.
</ParamField>
<ParamField body="noAutomaticPrefetch" type="boolean">

File diff suppressed because one or more lines are too long

View File

@ -663,10 +663,6 @@ export function getSdkSetupPrompt(mainType: "ai-prompt" | "nextjs" | "react" | "
},
});
\`\`\`
<Note>
The SDK auto-captures page-view and click analytics. To turn this off (and silence the \`ANALYTICS_NOT_ENABLED\` console warning that appears until you enable the Analytics app in your dashboard), pass \`analytics: { enabled: false }\`.
</Note>
` : ""}
${isMaybeBackend ? deindent`

View File

@ -65,7 +65,7 @@ import { EventTracker } from "./event-tracker";
import type { CrossDomainHandoffParams } from "./redirect-page-urls";
import { crossDomainAuthQueryParams, getCrossDomainHandoffParamsFromCurrentUrl, planRedirectToHandler } from "./redirect-page-urls";
import { subscribeSessionRefresh } from "./session-refresh-subscription";
import { AnalyticsOptions, SessionRecorder, analyticsOptionsFromJson, analyticsOptionsToJson } from "./session-replay";
import { AnalyticsOptions, SessionRecorder, analyticsOptionsFromJson, analyticsOptionsToJson, getSessionReplayOptions } from "./session-replay";
// IF_PLATFORM react-like
import { useAsyncCache } from "./common";
@ -686,13 +686,14 @@ export class _HexclaveClientAppImplIncomplete<HasTokenStore extends boolean, Pro
const analyticsEnabled = this._analyticsOptions?.enabled !== false;
if (analyticsEnabled && isBrowserLike() && this._hasPersistentTokenStore() && this._analyticsOptions?.replays?.enabled === true) {
const sessionReplayOptions = getSessionReplayOptions(this._analyticsOptions);
if (analyticsEnabled && isBrowserLike() && this._hasPersistentTokenStore() && sessionReplayOptions.enabled) {
this._sessionRecorder = new SessionRecorder({
projectId: this.projectId,
sendBatch: async (body, opts) => {
return await this._interface.sendSessionReplayBatch(body, await getAnalyticsSession(), opts);
},
}, this._analyticsOptions.replays);
}, sessionReplayOptions);
this._sessionRecorder.start();
}

View File

@ -1,5 +1,17 @@
import { describe, expect, it } from "vitest";
import { analyticsOptionsFromJson, analyticsOptionsToJson } from "./session-replay";
import { analyticsOptionsFromJson, analyticsOptionsToJson, getSessionReplayOptions } from "./session-replay";
describe("session replay options", () => {
it("enables replays by default", () => {
expect(getSessionReplayOptions(undefined).enabled).toBe(true);
expect(getSessionReplayOptions({}).enabled).toBe(true);
expect(getSessionReplayOptions({ replays: {} }).enabled).toBe(true);
});
it("preserves explicit replay opt-out", () => {
expect(getSessionReplayOptions({ replays: { enabled: false } }).enabled).toBe(false);
});
});
describe("analytics option JSON conversion", () => {
it("preserves top-level analytics options when serializing replay block classes", () => {

View File

@ -7,7 +7,7 @@ export type AnalyticsReplayOptions = {
/**
* Whether session replays are enabled.
*
* @default false
* @default true
*/
enabled?: boolean,
/**
@ -40,12 +40,19 @@ export type AnalyticsOptions = {
*/
enabled?: boolean,
/**
* Options for session replay recording. Replays are disabled by default;
* set `enabled: true` to opt in.
* Options for session replay recording. Replays are enabled by default;
* set `enabled: false` to opt out.
*/
replays?: AnalyticsReplayOptions,
};
export function getSessionReplayOptions(analyticsOptions: AnalyticsOptions | undefined): AnalyticsReplayOptions {
return {
...analyticsOptions?.replays,
enabled: analyticsOptions?.replays?.enabled ?? true,
};
}
/**
* Converts AnalyticsOptions to a JSON-safe representation.
* RegExp blockClass values are serialized as `{ __regexp, __flags }` objects.

View File

@ -36,8 +36,8 @@ export type StackClientAppConstructorOptions<HasTokenStore extends boolean, Proj
noAutomaticPrefetch?: boolean,
/**
* Options for analytics and session recording. Replays are disabled by default;
* set `{ replays: { enabled: true } }` to opt in.
* Options for analytics and session recording. Replays are enabled by default;
* set `{ replays: { enabled: false } }` to opt out.
*/
analytics?: AnalyticsOptions,
} & (