ENG-9611: defer tracking/analytics scripts so they don't block render
Summary
- External
srctracker scripts (gtag loader, clearbit) getasync_=Trueso they no longer block HTML parsing while downloading. - Inline tracker bootstraps (posthog, koala, common_room, rb2b, unify, default, gtag init) get
type="module", which the browser defers until after HTML parsing. - All eight telemetry script components switch from
rx.scripttorx.el.script. These trackers reach the DOM viaApp(head_components=...), whichreflex/compiler/utils.py:create_document_rootcompiles straight into<Head>β so the Helmet wrapper thatrx.scriptadds is redundant reconciliation cost. gtag_report_conversionis intentionally left non-module: it defines a globalfunction gtag_report_conversion()that pageonclickhandlers call by name; module scope would hide it fromwindow.
Linear: ENG-9611
Cross-repo follow-up
get_common_room_trackers is currently rendered inside rx.theme(rx.cond(...)) in two consumers (cloud, flexgen) rather than head_components. The old rx.script/Helmet wrapper hoisted it to <head> regardless; switching to rx.el.script means it now stays in the body where the theme renders it. The IIFE is idempotent and type="module" defers the same way regardless of DOM position, so it still works β but the head/body location changes for those two consumers.
A follow-up PR to cloud and flexgen should move the get_common_room_trackers(...) call into head_components= and adapt the rx.cond(~AuthState.in_session & is_prod_mode(), ...) gate accordingly.
Test plan
- [x]
uv run ruff check .clean - [x]
uv run ruff format .clean - [x]
uv run pyrightclean on the telemetry package - [ ] After merge/deploy: open DevTools β Network on a site using
get_pixel_website_trackers()and confirm tracker requests are no longer in the render-blocking section of the waterfall
Merging this PR will not alter performance
β
24 untouched benchmarks
Comparing khaleel/eng-9611-defer-tracker-scripts (9e37f01) with main (cf5e4ed)[^unexpected-base]
[^unexpected-base]: No successful run was found on main (8ad0f56) during the generation of this report, so cf5e4ed was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.
Greptile Summary
This PR defers all telemetry/analytics scripts to eliminate render-blocking by applying async_=True to external src scripts (Clearbit, gtag loader) and type="module" to inline bootstrap scripts (PostHog, Koala, Common Room, RB2B, Unify, Default, and the gtag init snippet). All eight tracker components are also migrated from rx.script (Helmet wrapper) to rx.el.script (raw element) since they are placed directly into <head> via head_components.
- The previous review concern about
function gtag()being module-scoped and therefore invisible towindowhas been addressed: the bootstrap now useswindow.gtag = window.gtag || function gtag() {...}to explicitly assign to the global object. - Every other inline IIFE was already module-safe: all globals they expose (
window.posthog,window.reb2b,window.ko,window.signals,window.unify,window.__default__) are assigned via explicitwindow.*properties. gtag_report_conversionintentionally remains a plain (non-module)rx.el.scriptso that thefunction gtag_report_conversion()declaration lands onwindowand is reachable byonclickhandlers, and so its internal baregtag()call can resolvewindow.gtagfrom the global scope.
Confidence Score: 5/5
Changes are safe to merge; all inline scripts correctly expose their globals via explicit window.* assignments, and the intentional non-module exception for gtag_report_conversion is well-reasoned.
The previous concern about window.gtag not being set from within a module script was the key risk here, and it has been correctly resolved by explicitly assigning window.gtag. Every other converted script already used window.* for any globals it exposes, so the type="module" deferral is safe across the board.
No files require special attention. The one file worth a second glance is google.py for the window.gtag fix and the intentional non-module treatment of gtag_report_conversion, both of which are handled correctly.
Important Files Changed
| Filename | Overview |
|---|---|
| packages/reflex-components-internal/src/reflex_components_internal/blocks/telemetry/google.py | GTAG init bootstrap now uses `window.gtag = window.gtag |
| packages/reflex-components-internal/src/reflex_components_internal/blocks/telemetry/posthog.py | Inline PostHog bootstrap converted to rx.el.script with type="module"; IIFE assigns to window.posthog explicitly, and bare posthog.init() call at module top-level correctly resolves from global scope. |
| packages/reflex-components-internal/src/reflex_components_internal/blocks/telemetry/rb2b.py | Both RB2B pixel scripts switched to rx.el.script with type="module"; both IIFEs assign to window.reb2b explicitly, making them module-safe. |
| packages/reflex-components-internal/src/reflex_components_internal/blocks/telemetry/clearbit.py | External Clearbit src script correctly gets async_=True to stop blocking HTML parsing. |
| packages/reflex-components-internal/src/reflex_components_internal/blocks/telemetry/common_room.py | Converted to rx.el.script with type="module"; IIFE uses window.signals assignment, so the global is properly accessible after deferral. |
| packages/reflex-components-internal/src/reflex_components_internal/blocks/telemetry/koala.py | Converted to rx.el.script with type="module"; IIFE sets window.ko explicitly before using the bare ko identifier, which resolves correctly from the global scope in a module context. |
| packages/reflex-components-internal/src/reflex_components_internal/blocks/telemetry/default.py | Converted to type="module"; the minified IIFE uses named-function-expression idiom (not declaration), so window.__default__ assignment via the e parameter is unaffected by module scope. |
| packages/reflex-components-internal/src/reflex_components_internal/blocks/telemetry/unify.py | Converted to rx.el.script with type="module"; IIFE assigns to window.unify and window.unifyBrowser explicitly, compatible with module scope. |
Reviews (2): Last reviewed commit: "fix: assign gtag to window so type=modul..." | Re-trigger Greptile
@greptile