Ritam/unified tracking#1132
Open
ritamzico wants to merge 18 commits into
Open
Conversation
…urrent models and unified model)
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: b066c49899
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
Contributor
Author
|
I will create another PR after getting rid of the ClickEvent model and other such tracking infrastructure. The migration needs to be run on production first before doing that, so that steps needs to be separate. |
PineND
reviewed
Apr 23, 2026
PineND
requested changes
Apr 23, 2026
|
LGTM! |
Contributor
Author
|
@PineND lmk if it looks good now |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Two problems with old tracking:
Fragmented models. Click events, banner views, and
class views each had their own model (
ClickEvent,BannerViewCount,ClassViewCount) with incompatibleschemas. Adding a new event type meant a new model, new
resolver, new migration. No shared query surface across
event types.
No frontend batching. Every tracking call fired its
own network request immediately. High-traffic pages (catalog
search, class view) hammered the backend with individual
mutations.
Solution
Single
TrackingEventmodel with open string enums(
eventType,targetType) and a flexiblemetadata: JSONfield. Any team can add new event types without schema
changes.
Frontend
useTrackinghook queues events client-side andflushes in one
trackEventsmutation — either every 5seconds or when 50 events accumulate, whichever comes first.
Server enriches each event with
sessionId,ipHash,userAgent, andreferrerso clients send only what theyknow.
What changed
Backend
TrackingEventMongoose model with indexes on[eventType, targetType, timestamp],[targetId, timestamp],[sessionId, timestamp], and[userId, timestamp]trackEventsGraphQL mutation — accepts up to 50 eventsper call, buffers to Redis, flushed to MongoDB every 5
minutes
trackingEventsTimeSeriesstaff-only query for analytics(
/banner/click/:id,/go/*path,/message/click/:id) —old counters preserved, new model always written
Frontend
useTrackinghook with client-side batching(
trackClick,trackView,trackDismiss,trackSearch,trackSearchClick)CourseSearch— debounced 1s,captures query + result count + which result was clicked
TrackingEventalongside existing trackingOutreachAnalytics) reads fromTrackingEventviatrackingEventsTimeSeriesMigration
apps/backend/src/scripts/migrate-click-events.tsbackfills all historical
ClickEventrecords intoTrackingEvent. Safe to re-run (ignores duplicate keyerrors)
What's preserved
Old
clickCount/viewCount/dismissCountcounters onbanners, redirects, and targeted messages are untouched. Old
ClickEventwrites still happen whenclickEventLoggingis enabled on a resource. No data is deleted.