Batch type 1 page_load events + gate observer teardown on success#1878
Merged
subodhr258 merged 3 commits intoMay 22, 2026
Merged
Conversation
…cess Address two follow-ups from the pre-merge review of #1872: 1. Batching / debouncing: replace the one-fetch-per-intersection model with a debounced queue. Intersection events enqueue [videoId, jobId] pairs that flush either after 300ms or when the queue hits 10 entries, preserving the legacy videoIds: [[id, job], ...] schema. Reduces singleton inserts on the ingestion side and protects against WAF rate-limit thresholds for surfaces that genuinely render N inline players (Reels feed, etc.). 2. Defensive observer gating: only call observer.unobserve when trackPageLoadForVideo reports it is done with the element. A "retry" return (currently only when window.analytics is unset) now leaves the IO entry alive so a subsequent intersection tick can retry instead of silently dropping the event. Also flush pending batches through a synchronous keepalive fetch from the existing visibilitychange/pagehide handlers so in-flight events survive page teardown without waiting on the DavidWells async queue. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
🔍 WordPress Plugin Check Report
📊 Report
❌ Errors (1)📁 readme.txt (1 error)
|
| 📍 Line | 🔖 Check | 💬 Message |
|---|---|---|
0 |
mismatched_plugin_name | Plugin name "GoDAM - Organize WordPress Media Library & File Manager with Unlimited Folders for Images, Videos & more" is different from the name declared in plugin header "GoDAM". |
0 |
trademarked_term | The plugin name includes a restricted term. Your chosen plugin name - "GoDAM - Organize WordPress Media Library & File Manager with Unlimited Folders for Images, Videos & more" - contains the restricted term "wordpress" which cannot be used at all in your plugin name. |
📁 composer.json (1 warning)
| 📍 Line | 🔖 Check | 💬 Message |
|---|---|---|
0 |
missing_composer_json_file | The "/vendor" directory using composer exists, but "composer.json" file is missing. |
📁 assets/build/css/main.css (1 warning)
| 📍 Line | 🔖 Check | 💬 Message |
|---|---|---|
0 |
EnqueuedStylesScope | This style is being loaded in all contexts. |
📁 assets/src/libs/analytics.min.js (5 warnings)
| 📍 Line | 🔖 Check | 💬 Message |
|---|---|---|
0 |
EnqueuedScriptsScope | This script is being loaded in all frontend contexts. |
0 |
NonBlockingScripts.NoStrategy | This script on http://localhost:8880 (with handle analytics-library) is loaded in the footer. Consider a defer or async script loading strategy instead. |
0 |
NonBlockingScripts.NoStrategy | This script on http://localhost:8880/2026/05/21/demo-post-post/ (with handle analytics-library) is loaded in the footer. Consider a defer or async script loading strategy instead. |
0 |
NonBlockingScripts.NoStrategy | This script on http://localhost:8880/demo-page-post/ (with handle analytics-library) is loaded in the footer. Consider a defer or async script loading strategy instead. |
0 |
NonBlockingScripts.NoStrategy | This script on http://localhost:8880/demo-attachment-post/ (with handle analytics-library) is loaded in the footer. Consider a defer or async script loading strategy instead. |
📁 assets/build/js/main.min.js (5 warnings)
| 📍 Line | 🔖 Check | 💬 Message |
|---|---|---|
0 |
EnqueuedScriptsScope | This script is being loaded in all frontend contexts. |
0 |
NonBlockingScripts.NoStrategy | This script on http://localhost:8880 (with handle rtgodam-script) is loaded in the footer. Consider a defer or async script loading strategy instead. |
0 |
NonBlockingScripts.NoStrategy | This script on http://localhost:8880/2026/05/21/demo-post-post/ (with handle rtgodam-script) is loaded in the footer. Consider a defer or async script loading strategy instead. |
0 |
NonBlockingScripts.NoStrategy | This script on http://localhost:8880/demo-page-post/ (with handle rtgodam-script) is loaded in the footer. Consider a defer or async script loading strategy instead. |
0 |
NonBlockingScripts.NoStrategy | This script on http://localhost:8880/demo-attachment-post/ (with handle rtgodam-script) is loaded in the footer. Consider a defer or async script loading strategy instead. |
🤖 Generated by WordPress Plugin Check Action • Learn more about Plugin Check
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refines GoDAM’s frontend player analytics (page_load type 1) by batching per-video viewport-triggered events into fewer requests, improving ingestion efficiency on pages with many inline players. It also makes the IntersectionObserver teardown conditional on successful enqueueing, and forces a keepalive flush during page teardown to reduce event loss.
Changes:
- Batch
page_load(type 1) events into a debounced queue with size/time-based flushing. - Only unobserve a video after
trackPageLoadForVideo()indicates it is safe to stop tracking (retry when analytics isn’t ready). - Flush pending type 1 events via synchronous keepalive fetch on
visibilitychange(hidden) and unload events.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Both bail paths in flushPageLoadQueue cleared the debounce timer before deciding whether to drain, so when a bail returned without sending, the queue could sit until the next intersection event triggered another enqueue. - sync + shouldSkipAnalytics: clear the queue. shouldSkipAnalytics() is constant for the page session (isAdminPage / preview query param), so these events would never be sendable through the async path either. Dropping them avoids unbounded queue growth across bfcache restores. - async + !window.analytics: reschedule the flush. Bail before the splice so we don't need the unshift dance, then schedule a new timer so a missing analytics library is retried rather than stranding the batch. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
elifvish
approved these changes
May 21, 2026
ba7aa65
into
feat/type-1-tracking-enhancement
2 of 3 checks passed
subodhr258
added a commit
that referenced
this pull request
May 22, 2026
) * feat: implement type 1 video page load tracking with viewport observation * Batch type 1 page_load events + gate observer teardown on success (#1878) * feat: batch type 1 page_load events and gate observer teardown on success Address two follow-ups from the pre-merge review of #1872: 1. Batching / debouncing: replace the one-fetch-per-intersection model with a debounced queue. Intersection events enqueue [videoId, jobId] pairs that flush either after 300ms or when the queue hits 10 entries, preserving the legacy videoIds: [[id, job], ...] schema. Reduces singleton inserts on the ingestion side and protects against WAF rate-limit thresholds for surfaces that genuinely render N inline players (Reels feed, etc.). 2. Defensive observer gating: only call observer.unobserve when trackPageLoadForVideo reports it is done with the element. A "retry" return (currently only when window.analytics is unset) now leaves the IO entry alive so a subsequent intersection tick can retry instead of silently dropping the event. Also flush pending batches through a synchronous keepalive fetch from the existing visibilitychange/pagehide handlers so in-flight events survive page teardown without waiting on the DavidWells async queue. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * fix: Increase batch flush delay * fix: prevent stranded batches when flushPageLoadQueue bails Both bail paths in flushPageLoadQueue cleared the debounce timer before deciding whether to drain, so when a bail returned without sending, the queue could sit until the next intersection event triggered another enqueue. - sync + shouldSkipAnalytics: clear the queue. shouldSkipAnalytics() is constant for the page session (isAdminPage / preview query param), so these events would never be sendable through the async path either. Dropping them avoids unbounded queue growth across bfcache restores. - async + !window.analytics: reschedule the flush. Bail before the splice so we don't need the unshift dance, then schedule a new timer so a missing analytics library is retried rather than stranding the batch. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Subodh Rajpopat <subodh.rajpopat@rtcamp.com> Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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.
Follow-up to #1872 addressing P0#1 and P0#2 from the pre-merge review in rtCamp/godam-core#1083.
What changed
Scope is limited to
assets/src/js/godam-player/analytics.js.1. Batched dispatch for
page_loadeventsThe per-instance model in #1872 sends one fetch per intersecting
<video>. For surfaces that genuinely render N inline players (Reels feed, etc.) this multiplies request count by N. Replaced the directwindow.analytics.track()call insidetrackPageLoadForVideowith a debounced queue:[ videoId, jobId ]pairs.PAGE_LOAD_BATCH_SIZE(10) entries.setTimeoutfiresPAGE_LOAD_FLUSH_DELAY_MS(1000ms) after the first pending entry.window.analytics.track( 'page_load', { type: 1, videoIds: [[...], ...] } )— preserving the legacy bulk schema, so the analytics endpoint receives the same payload shape it has always handled.This is mostly a server-side win: 30× fewer singleton inserts on the ingestion path, less exposure to WAF / rate-limit thresholds. User-facing perf is unchanged on HTTP/2 (both old and new paths multiplex over one connection).
2. Defensive observer gating
Previously the IntersectionObserver callback called
trackPageLoadForVideothenobserver.unobserve()unconditionally:If
trackPageLoadForVideoreturnedfalse(e.g.window.analyticsnot yet ready), the event was dropped and the observer torn down with no retry. With the bundle structure on this branch the race is unlikely (window.analyticsis set synchronously at module-eval in the same file), but the gating is cheap defense-in-depth.trackPageLoadForVideonow returns:truewhen the caller should stop observing — event enqueued, duplicate, or malformed metadatafalseonly when the caller should keep observing for a later retry tick — analytics library not yet readyThe IO callback only tears down observation when the return is
true.3. Keepalive flush on page teardown
The new queue is flushed synchronously from the existing
visibilitychange === 'hidden'andpagehide/beforeunloadhandlers via a direct keepalive fetch (mirroringsendPlayerHeatmap). This bypasses the DavidWells async pipeline, so pending batches survive tab close / background even when the library's async queue has not yet drained.Test plan
Before
Screen.Recording.2026-05-20.at.6.11.58.PM.mov
After (batches of max 10 videos, 1 second delay)
Batch.fix.for.type.1.mov
Test on iOS
Screen.Recording.2026-05-20.at.7.02.54.PM.mov
🤖 Generated with Claude Code