Skip to content

fix(consent): follow official vendor snippet order in gateScript (#156) - #174

Merged
jamiedavenport merged 2 commits into
mainfrom
issue-156
Aug 2, 2026
Merged

fix(consent): follow official vendor snippet order in gateScript (#156)#174
jamiedavenport merged 2 commits into
mainfrom
issue-156

Conversation

@jamiedavenport

@jamiedavenport jamiedavenport commented Aug 2, 2026

Copy link
Copy Markdown
Owner

Summary

Fixes #156: gateScript's stub lifecycle dropped calls after load, so the shipped metaPixel() 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, runs init (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:loaded now fires only after the script's real load event, so calls made during download are no longer dropped.

Changes

  • @policystack/core: rework gateScript lifecycle (restore → init → replay → inject), late loaded flag with started re-entry guard, cleanup of stub-created parent objects, and forwarding for captured stub references; codify the new init contract in ScriptDefinition JSDoc
  • @policystack/scripts: Meta Pixel, PostHog, Segment, and Hotjar inits now replicate their official inline snippet bootstraps (GA4/GTM were already snippet-shaped)
  • Tests: fake vendors now decorate-and-drain like the real scripts instead of replacing globals (the gap that let the bug pass), plus coverage for the already-granted path, calls during download, post-load calls, ordering, and dispose races
  • Docs (consent/scripts.md, core.md, react.md) updated to the new contract; minor changeset with a migration note for custom defineScript authors

Summary by CodeRabbit

  • Bug Fixes

    • Improved consent-gated script loading by initializing vendor queues before downloads begin.
    • Preserved and replayed calls made before or during script loading.
    • Ensured load events are emitted only after scripts finish loading.
    • Improved Meta Pixel, PostHog, Segment, and Hotjar initialization.
  • Documentation

    • Clarified vendor bootstrap, queueing, initialization, and custom integration requirements.

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.
@vercel

vercel Bot commented Aug 2, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
op-demo Ready Ready Preview Aug 2, 2026 1:33pm
openpolicy-sh Ready Ready Preview Aug 2, 2026 1:33pm
policystack Ready Ready Preview Aug 2, 2026 1:33pm

Request Review

@coderabbitai

coderabbitai Bot commented Aug 2, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 817be1e8-5291-4022-ac29-e22abfaa22d7

📥 Commits

Reviewing files that changed from the base of the PR and between 2169fd6 and 1441f4b.

📒 Files selected for processing (2)
  • packages/scripts/src/posthog.test.ts
  • packages/scripts/src/posthog.ts
🚧 Files skipped from review as they are similar to previous changes (2)
  • packages/scripts/src/posthog.test.ts
  • packages/scripts/src/posthog.ts

📝 Walkthrough

Walkthrough

gateScript now creates vendor queueing stubs, replays queued calls, and injects scripts in that order. Vendor integrations follow official bootstrap patterns. Tests control load timing and verify lifecycle behavior. Documentation describes the new order.

Changes

Gated script bootstrap

Layer / File(s) Summary
GateScript lifecycle and handoff
packages/core/src/consent/scripts.ts, packages/core/src/consent/types.ts
The loading flow restores globals, runs init, replays queued calls, and injects the script. Generated paths, forwarding, cleanup, and lifecycle state are updated.
Vendor bootstrap integrations
packages/scripts/src/meta-pixel.ts, packages/scripts/src/posthog.ts, packages/scripts/src/segment.ts, packages/scripts/src/hotjar.ts
Integrations create or reuse vendor-compatible stubs and perform bootstrap calls before external script loading.
Lifecycle and integration validation
packages/core/src/consent/scripts.test.ts, packages/scripts/src/*test.ts, packages/scripts/src/test-helpers.ts
Tests control script-load timing and verify initialization order, queue replay, restoration, disposal, and vendor-specific behavior.
Documentation and release notes
apps/web/content/docs/consent/*.md, .changeset/gated-script-snippet-order.md
Documentation describes bootstrap ordering, vendor behavior, and custom defineScript requirements.

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
Loading

Possibly related issues

  • Issue 156 — Covers the gateScript stub lifecycle and initialization-order changes in this pull request.
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 11.11% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: updating gateScript to follow official vendor snippet initialization order.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch issue-156

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

📥 Commits

Reviewing files that changed from the base of the PR and between e7c984f and 2169fd6.

📒 Files selected for processing (18)
  • .changeset/gated-script-snippet-order.md
  • apps/web/content/docs/consent/core.md
  • apps/web/content/docs/consent/react.md
  • apps/web/content/docs/consent/scripts.md
  • packages/core/src/consent/scripts.test.ts
  • packages/core/src/consent/scripts.ts
  • packages/core/src/consent/types.ts
  • packages/scripts/src/ga4.test.ts
  • packages/scripts/src/google-tag-manager.test.ts
  • packages/scripts/src/hotjar.test.ts
  • packages/scripts/src/hotjar.ts
  • packages/scripts/src/meta-pixel.test.ts
  • packages/scripts/src/meta-pixel.ts
  • packages/scripts/src/posthog.test.ts
  • packages/scripts/src/posthog.ts
  • packages/scripts/src/segment.test.ts
  • packages/scripts/src/segment.ts
  • packages/scripts/src/test-helpers.ts

Comment thread packages/scripts/src/posthog.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.
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.

gateScript: stub lifecycle drops calls after load — shipped metaPixel() never fires

1 participant