fix(consent): follow official vendor snippet order in gateScript (#156) - #174
Conversation
gateScript now restores stubbed globals, runs init, and replays the pre-consent queue before injecting the script; loaded is set only after the script's real load event. Meta Pixel, PostHog, Segment, and Hotjar inits now create their vendor's official snippet stub, and the fake vendors in tests decorate-not-replace like the real scripts do.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
📝 WalkthroughWalkthrough
ChangesGated script bootstrap
Estimated code review effort: 4 (Complex) | ~45 minutes Sequence Diagram(s)sequenceDiagram
participant Consent as Consent state
participant GateScript
participant VendorStub as Vendor stub
participant ExternalScript as External script
Consent->>GateScript: grant consent
GateScript->>GateScript: restore globals
GateScript->>VendorStub: run init and create queue
GateScript->>VendorStub: replay queued calls
GateScript->>ExternalScript: inject script
ExternalScript->>VendorStub: decorate and drain stub
Possibly related issues
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches 💡 1📝 Generate docstrings 💡
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@packages/scripts/src/posthog.ts`:
- Around line 21-44: Update STUB_METHODS and the related pre-init queue behavior
to match the current PostHog snippet: retain get_distinct_id, add set_config,
getFeatureFlagResult, updateEarlyAccessFeatureEnrollment,
getEarlyAccessFeatures, getSurveys, getActiveMatchingSurveys,
register_for_session, and setPersonProperties, and ensure queued calls return
push()’s numeric length. Remove top-level set and set_once unless they are
explicitly translated to the current person-property API.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: 68f09e0a-56be-42bf-a037-5aa4739160c1
📒 Files selected for processing (18)
.changeset/gated-script-snippet-order.mdapps/web/content/docs/consent/core.mdapps/web/content/docs/consent/react.mdapps/web/content/docs/consent/scripts.mdpackages/core/src/consent/scripts.test.tspackages/core/src/consent/scripts.tspackages/core/src/consent/types.tspackages/scripts/src/ga4.test.tspackages/scripts/src/google-tag-manager.test.tspackages/scripts/src/hotjar.test.tspackages/scripts/src/hotjar.tspackages/scripts/src/meta-pixel.test.tspackages/scripts/src/meta-pixel.tspackages/scripts/src/posthog.test.tspackages/scripts/src/posthog.tspackages/scripts/src/segment.test.tspackages/scripts/src/segment.tspackages/scripts/src/test-helpers.ts
Adds the queueable methods the current posthog-js snippet stubs (setPersonProperties, set_config, feature-flag and survey getters, register_for_session, ...) and drops top-level set/set_once, which are not client methods; the gated queue path posthog.set becomes posthog.setPersonProperties accordingly.
Summary
Fixes #156:
gateScript's stub lifecycle dropped calls after load, so the shippedmetaPixel()never fired in either consent path (and PostHog/Segment/Hotjar were similarly broken). The gate now follows the official vendor snippet order — on consent it restores the stubbed globals, runsinit(which now creates the vendor's own snippet stub), replays the pre-consent queue into it, and only then injects the script, so vendors like fbevents.js that decorate the existing global and drain its queue bootstrap correctly.script:loadednow fires only after the script's real load event, so calls made during download are no longer dropped.Changes
@policystack/core: reworkgateScriptlifecycle (restore → init → replay → inject), lateloadedflag withstartedre-entry guard, cleanup of stub-created parent objects, and forwarding for captured stub references; codify the newinitcontract inScriptDefinitionJSDoc@policystack/scripts: Meta Pixel, PostHog, Segment, and Hotjarinits now replicate their official inline snippet bootstraps (GA4/GTM were already snippet-shaped)consent/scripts.md,core.md,react.md) updated to the new contract; minor changeset with a migration note for customdefineScriptauthorsSummary by CodeRabbit
Bug Fixes
Documentation