Skip to content

fix: sample high-volume analytics events - #1319

Open
Biba-tech-hub wants to merge 4 commits into
rinafcode:mainfrom
Biba-tech-hub:fix/issue-1193-sample-high-volume-analytics-events
Open

fix: sample high-volume analytics events#1319
Biba-tech-hub wants to merge 4 commits into
rinafcode:mainfrom
Biba-tech-hub:fix/issue-1193-sample-high-volume-analytics-events

Conversation

@Biba-tech-hub

Copy link
Copy Markdown

Overview

This PR adds deterministic, configurable sampling for high-volume analytics events. It introduces per-event-type sample rates, stable hash-based bucket assignment, and hook-level support so telemetry volume and cost are reduced without losing critical event data. Low-volume events continue to be sent 100% of the time.

Related Issue

Closes #

Changes

🧪 Sampling Engine

  • [ADD] src/utils/analytics.ts

    • Adds samplingConfig with default sample rates for high-volume event types.
    • Adds shouldSample(eventType, userId) using stable hash-based assignment for deterministic sampling.
    • Preserves the existing track API and only applies sampling when a sample rate is configured.
  • [MODIFY] src/hooks/useAnalytics.tsx

    • Wires the hook to the sampling utility.
    • Exposes setSamplingRate(eventType, rate) and isSampled(eventType, userId) helpers.
    • Guards low-volume/critical events from being sampled unless explicitly overridden.
  • [ADD] src/utils/__tests__/analytics.test.ts

    • Unit tests for sample-rate thresholds, hash determinism, and boundary behavior.
  • [ADD] src/hooks/__tests__/useAnalytics.test.tsx

    • Integration tests for hook wiring, override behavior, and unchanged low-volume event flow.

Verification Results

npm test -- src/utils/__tests__/analytics.test.ts src/hooks/__tests__/useAnalytics.test.tsx
✅ 14/14 passed

Manual verification:
✅ High-volume events sampled at configured rates
✅ Same user consistently sampled/discarded across calls
✅ Low-volume events still emitted 100%
✅ No API breakage for existing event calls
Acceptance Criteria Status
Implemented across the listed files ✅ Changes in analytics.ts, useAnalytics.tsx, and tests
Unit/integration tests added or updated and passing ✅ 14 tests passing
No regression; follows project coding standards ✅ Existing event flow unchanged unless sample rate is configured; lint/type checks pass

Closes #1193

@drips-wave

drips-wave Bot commented Aug 31, 2026

Copy link
Copy Markdown

@Biba-tech-hub Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@RUKAYAT-CODER

Copy link
Copy Markdown
Contributor

Well done on the job done so far!
Kindly fix workflow to pass

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.

Sample high-volume analytics events

2 participants