Skip to content

custom events#1290

Open
mantrakp04 wants to merge 14 commits intodario-likes-mcpsfrom
custom-events
Open

custom events#1290
mantrakp04 wants to merge 14 commits intodario-likes-mcpsfrom
custom-events

Conversation

@mantrakp04
Copy link
Copy Markdown
Collaborator

  • Enhance documentation tools integration
  • Enhance error handling and API response for documentation tools
  • Refactor askStackAuth key to ask_stack_auth in API documentation
  • Enhance analytics and spans functionality in backend

- Added new SQL migrations for Clickhouse to support spans and events, including new columns and views.
- Implemented validation for analytics events and spans, ensuring proper structure and data integrity.
- Introduced new API endpoints for uploading analytics spans and events, improving data collection capabilities.
- Updated existing analytics event handling to accommodate new fields and ensure compatibility with the updated database schema.
- Added tests for analytics event insertion and validation to ensure reliability and correctness.

These changes improve the analytics framework, enabling better tracking and analysis of user interactions.
Copilot AI review requested due to automatic review settings March 25, 2026 22:44
@vercel
Copy link
Copy Markdown

vercel bot commented Mar 25, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
stack-auth-hosted-components Ready Ready Preview, Comment Mar 27, 2026 0:44am
stack-backend Ready Ready Preview, Comment Mar 27, 2026 0:44am
stack-dashboard Ready Ready Preview, Comment Mar 27, 2026 0:44am
stack-demo Ready Ready Preview, Comment Mar 27, 2026 0:44am
stack-docs Ready Ready Preview, Comment Mar 27, 2026 0:44am

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Mar 25, 2026

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: c682f659-765b-4e01-9f53-4c237d583aa9

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch custom-events

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@mantrakp04 mantrakp04 review requested due to automatic review settings March 25, 2026 22:46
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps bot commented Mar 25, 2026

Greptile Summary

This PR ships the custom events / tracing feature for Stack Auth's analytics SDK. It adds client-side and server-side trackEvent(), captureException(), startSpan(), super-properties, a new /analytics/spans/batch backend route, a new analytics_internal.spans ClickHouse table, and shared validation/sanitization helpers. All critical issues from the previous review round have been addressed.

Issues found:

  • In events.tsx, system events logged through logEvent receive trace_id: generateUuid() — a random UUID — rather than null, creating orphaned traces inconsistent with SDK convention.
  • SPANS_VIEW_SQL is executed twice in runClickhouseMigrations() — first call is redundant.
  • The events batch route contains a duplicate user-auth guard.

Confidence Score: 4/5

Safe to merge; no data-loss or security regressions found. All prior critical concerns are addressed.

The implementation is thorough, well-tested, and resolves all issues flagged in the previous review. The one logic issue (random trace_id on system events) affects analytics data quality but is not a runtime error or security vulnerability. Remaining findings are minor style/redundancy issues.

apps/backend/src/lib/events.tsx (trace_id generation for system events), apps/backend/scripts/clickhouse-migrations.ts (redundant SPANS_VIEW_SQL call)

Important Files Changed

Filename Overview
apps/backend/src/lib/events.tsx Refactored logEvent to insert analytics rows via insertAnalyticsEvents; adds span-based telemetry export, but generates random trace_id for every system event instead of null.
apps/backend/src/app/api/latest/analytics/events/batch/route.tsx Events batch route expanded to support server-sent custom events, session replay ID validation against DB, and proper from_server attribution.
apps/backend/src/app/api/latest/analytics/spans/batch/route.tsx New spans batch endpoint with proper auth checks; client auth guard for user_id/team_id override is correctly implemented.
apps/backend/scripts/clickhouse-migrations.ts Adds spans table, trace_id and from_server columns; SPANS_VIEW_SQL is executed twice (harmless but redundant).
apps/backend/src/lib/analytics-validation.ts New shared validation helpers for event/span types and lone-surrogate sanitization; centralizes logic previously duplicated across routes.
apps/backend/src/lib/spans.tsx New insertSpans helper with lone-surrogate sanitization, analogous to insertAnalyticsEvents.
packages/template/src/lib/stack-app/apps/implementations/client-app-impl.ts Adds trackEvent, captureException, register/unregister super-properties, startSpan, and span batcher; fixes origin check with URL parsing; fetch patch is now applied once globally.
packages/template/src/lib/stack-app/apps/implementations/server-app-impl.ts Adds server-side trackEvent, captureException, startSpan, middleware(); user context resolved once per span rather than at end time, avoiding N concurrent API calls.
packages/template/src/lib/stack-app/apps/implementations/server-event-batcher.ts New server-side batcher that groups items by session key; correctly uses Symbol.for to share state across CJS/ESM dual-package builds.
packages/template/src/lib/stack-app/apps/implementations/tracing.ts New SpanImpl using Map for attribute storage (avoids prototype pollution); correctly implements async context propagation.
packages/template/src/lib/stack-app/apps/implementations/event-tracker.ts Extended event tracker with rage click, scroll depth, clipboard, error, and custom event support; configurable thresholds via env vars.
packages/template/src/lib/stack-app/apps/implementations/analytics-events.ts Shared client-side validation helpers for event names, timestamps, payloads, and replay link options.
packages/stack-shared/src/interface/crud/analytics.ts Adds AnalyticsBatchEvent, AnalyticsBatchSpan types and AUTO_CAPTURED_ANALYTICS_EVENT_TYPES; well-structured with clear client/server permission split.

Sequence Diagram

sequenceDiagram
    participant Browser as Browser SDK
    participant Server as Server SDK
    participant EventsAPI as /analytics/events/batch
    participant SpansAPI as /analytics/spans/batch
    participant CH as ClickHouse

    Note over Browser: trackEvent() / captureException()
    Browser->>Browser: EventTracker._enqueueEvent()
    Browser->>Browser: ClientSpanBatcher.push()
    Browser-->>EventsAPI: POST (client auth, keepalive)
    Browser-->>SpansAPI: POST (client auth, keepalive)

    Note over Server: trackEvent() / startSpan()
    Server->>Server: ServerBatcher.push(event, session)
    Server->>Server: ServerBatcher.push(span, session)
    Server-->>EventsAPI: POST (server auth, grouped by session)
    Server-->>SpansAPI: POST (server auth, grouped by session)

    EventsAPI->>EventsAPI: Validate auth type + event_type
    EventsAPI->>EventsAPI: Resolve session_replay_id from DB
    EventsAPI->>CH: insertAnalyticsEvents()

    SpansAPI->>SpansAPI: Validate auth type + span_type
    SpansAPI->>CH: insertSpans()

    Note over CH: ReplacingMergeTree deduplicates spans by span_id
Loading

Fix All in Claude Code Fix All in Cursor Fix All in Codex

Reviews (5): Last reviewed commit: "Enhance client analytics event validatio..." | Re-trigger Greptile

- Added validation to prevent client analytics spans from overriding user_id or team_id.
- Refactored _superProperties in client and server app implementations to use Map for better performance and memory management.
- Updated methods to handle super properties using Object.fromEntries for compatibility with the new Map structure.

These changes improve data integrity and optimize property management in the analytics framework.
@mantrakp04
Copy link
Copy Markdown
Collaborator Author

@greptile-ai review

- Removed unused browserSessionId from analytics event batch upload and query functions to streamline data handling.
- Updated SQL queries in tests to include new event types and ensure proper validation messages.
- Enhanced test cases to reflect changes in permissions for accessing spans, improving overall test coverage.

These changes optimize the analytics event processing and strengthen the integrity of test validations.
- Removed unused browserSessionId from client-side and server-side event tracking to streamline the analytics process.
- Updated the `trackEvent` method to ensure immediate flushing of events, enhancing reliability in event delivery.
- Improved documentation for `trackEvent` usage, clarifying the behavior of fire-and-forget calls and the importance of flushing analytics.

These changes optimize event handling and enhance the clarity of the analytics framework documentation.
@mantrakp04
Copy link
Copy Markdown
Collaborator Author

@greptile-ai review additionally give extra attention to hard to read slop, non DRY code, n etc

- Added a new utility function `stripLoneSurrogates` to handle lone surrogate characters in strings, ensuring compatibility with ClickHouse's JSON parser.
- Refactored `sanitizeAnalyticsEventData` and `insertSpans` functions to utilize the new `stripLoneSurrogates` function for data sanitization before processing.
- Improved code organization by moving the lone surrogate handling logic from `events.tsx` to a dedicated validation module.

These changes enhance data integrity and prevent potential parsing errors in analytics event handling.
- Updated the type of `_waitUntil` to use `Promise<unknown>` for improved type safety.
- Refactored the `_trackAutoCapturedEvent` method to simplify the handling of request options.
- Enhanced the `_normalizeRequestArg` method to ensure proper type handling for headers and options.
- Improved global state management in `server-event-batcher` by using a more explicit type definition.
- Adjusted header reading functions in `tracing.ts` to enhance type safety and clarity.

These changes improve code maintainability and ensure better type safety across the server app implementation.
- Introduced a `Set` for known event types in the `PageClient` component to enhance performance and readability.
- Updated the filtering logic for custom event types to utilize the new `Set`, ensuring more efficient checks.
- Adjusted type definitions in `server-app-impl` and `server-event-batcher` to use `Promise<unknown>` for better type safety.

These changes streamline event type management and enhance type safety across the application.
@mantrakp04
Copy link
Copy Markdown
Collaborator Author

@greptile-ai review and check for best practices and clean code with a high focus on readability

- Removed browser_session_id from SQL queries and test assertions to streamline analytics data handling.
- Updated related functions to ensure consistency in event type queries and improve clarity in test cases.

These changes enhance the focus on relevant event data and improve the overall structure of analytics queries.
…ences

- Removed all instances of browser_session_id from analytics queries and documentation to streamline event tracking and improve clarity.
- Updated related SQL queries and documentation to reflect the changes in session replay linkage, focusing on sessionReplayId and sessionReplaySegmentId.
- Improved type safety in server app specifications by adjusting the waitUntil promise type.

These changes optimize the analytics framework and enhance the overall clarity of event handling.
- Introduced a new validation function, `isValidClientAnalyticsEventType`, to ensure client analytics events do not use server-only event types.
- Updated the analytics validation logic to differentiate between client and server event types, improving clarity and security in event handling.
- Adjusted documentation to reflect the new validation rules for client-sent analytics events.

These changes strengthen the integrity of analytics event processing and enhance the overall structure of event type management.
@mantrakp04
Copy link
Copy Markdown
Collaborator Author

@greptile-ai review

- Introduced a new endpoint for ingesting OpenTelemetry traces in OTLP/HTTP JSON format, allowing backend services to send trace data as analytics spans.
- Implemented validation and sanitization for incoming trace data, ensuring only valid spans are processed.
- Enhanced error handling for invalid payloads and added support for server or admin authentication.
- Updated analytics validation to accept additional formats for OpenTelemetry IDs, improving flexibility in trace identification.

These changes expand the analytics capabilities and improve the integration with OpenTelemetry for better observability.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant