Skip to content

feat: read requireAnalyticsOptIn setting from visual editor config#1059

Merged
jwartofsky-yext merged 2 commits intomainfrom
requireOptInHandling
Feb 24, 2026
Merged

feat: read requireAnalyticsOptIn setting from visual editor config#1059
jwartofsky-yext merged 2 commits intomainfrom
requireOptInHandling

Conversation

@jwartofsky-yext
Copy link
Contributor

No description provided.

@jwartofsky-yext jwartofsky-yext changed the title feat: read requireOptIn setting from visual editor config feat: read requireAnalyticsOptIn setting from visual editor config Feb 24, 2026
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 24, 2026

Walkthrough

This change reads document.__.visualEditorConfig at runtime, parses it for requireAnalyticsOptIn (safe-falls to false on absence or parse errors), and passes that value as the new requireOptIn prop to AnalyticsProvider in three template files: directory.tsx, locator.tsx, and main.tsx. No other control-flow or exported signatures were changed.

Sequence Diagram(s)

sequenceDiagram
  participant Browser
  participant DocumentGlobal as document.__
  participant Template as (directory|locator|main).tsx
  participant AnalyticsProvider

  Browser->>Template: render template
  Template->>DocumentGlobal: read visualEditorConfig (string?)
  alt config present
    Template->>Template: JSON.parse(config) (try/catch)
    alt parse success
      Template->>Template: extract requireAnalyticsOptIn (default false)
    else parse error
      Template->>Browser: console.error(...)
      Template->>Template: set requireAnalyticsOptIn = false
    end
  else config missing
    Template->>Template: set requireAnalyticsOptIn = false
  end
  Template->>AnalyticsProvider: render with requireOptIn=requireAnalyticsOptIn
  AnalyticsProvider->>AnalyticsProvider: use requireOptIn for analytics behavior
Loading

Possibly related PRs

Suggested reviewers

  • mkilpatrick
  • asanehisa
🚥 Pre-merge checks | ✅ 2
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: reading requireAnalyticsOptIn setting from visual editor config across multiple template files.
Description check ✅ Passed No description was provided by the author; however, the PR title and code changes are clear enough to understand the intent without additional description.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch requireOptInHandling

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

♻️ Duplicate comments (2)
packages/visual-editor/src/vite-plugin/templates/locator.tsx (1)

149-153: ⚠️ Potential issue | 🟠 Major

Same unguarded JSON.parse crash risk as in directory.tsx.

Identical issue: JSON.parse(document.__.visualEditorConfig) is called inline without a try/catch, and the ?.requireAnalyticsOptIn chain can yield undefined instead of false. Apply the same fix described for directory.tsx.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/visual-editor/src/vite-plugin/templates/locator.tsx` around lines
149 - 153, The inline JSON.parse call for document.__.visualEditorConfig used to
compute requireOptIn in locator.tsx is unguarded and can throw or return
undefined; wrap the parse in a safe try/catch (or reuse the same safe
parser/helper used in directory.tsx) and explicitly default
requireAnalyticsOptIn to false on any parse error or missing value so
requireOptIn always becomes a boolean; update the expression that sets
requireOptIn (referencing requireOptIn and document.__.visualEditorConfig) to
call the safe parser and return parsed.requireAnalyticsOptIn ?? false.
packages/visual-editor/src/vite-plugin/templates/main.tsx (1)

147-151: ⚠️ Potential issue | 🟠 Major

Same unguarded JSON.parse crash risk as in directory.tsx.

Identical issue: JSON.parse(document.__.visualEditorConfig) is called inline without a try/catch, and the ?.requireAnalyticsOptIn chain can yield undefined instead of false. Apply the same fix described for directory.tsx.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/visual-editor/src/vite-plugin/templates/main.tsx` around lines 147 -
151, The inline JSON.parse call on document.__.visualEditorConfig used to set
the requireOptIn prop can throw and also yields undefined instead of a boolean;
update the code that computes requireOptIn (the expression referencing
document.__.visualEditorConfig and requireOptIn) to parse safely inside a
try/catch (or use a small safeParse helper), default to an empty object on parse
failure, and then read .requireAnalyticsOptIn with a fallback to false so
requireOptIn always becomes a boolean.
🧹 Nitpick comments (1)
packages/visual-editor/src/vite-plugin/templates/directory.tsx (1)

137-141: Consider extracting a shared helper for this duplicated parsing logic.

The identical JSON.parse(visualEditorConfig)?.requireAnalyticsOptIn pattern is copy-pasted across all three template files. Since these are autogenerated, the fix belongs in the generator — a shared utility (e.g., getRequireOptIn(document)) would centralise the logic and error handling in one place.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/visual-editor/src/vite-plugin/templates/directory.tsx` around lines
137 - 141, The duplicated JSON.parse(...) logic used to derive requireOptIn from
document.__.visualEditorConfig should be extracted into a shared helper (e.g.,
getRequireOptIn(document)) in the generator/runtime so templates reuse it;
implement getRequireOptIn to safely access document.__.visualEditorConfig,
try/catch JSON.parse, return a boolean (default false), and replace the inline
expression in templates like the requireOptIn prop in directory.tsx with a call
to getRequireOptIn(document).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@packages/visual-editor/src/vite-plugin/templates/directory.tsx`:
- Around line 137-141: The JSX currently calls JSON.parse on
document.__.visualEditorConfig without error handling and uses optional chaining
that can yield undefined; update the logic that computes the requireOptIn value
(the expression using document.__.visualEditorConfig and requireAnalyticsOptIn)
to first read the raw string, then try/catch JSON.parse to avoid throwing during
render, and coerce the final value with ?? false so it always supplies a boolean
to the requireOptIn prop; modify the code around the requireOptIn expression in
templates/directory.tsx accordingly (use a local const before the return that
falls back to false on parse error or missing key).

---

Duplicate comments:
In `@packages/visual-editor/src/vite-plugin/templates/locator.tsx`:
- Around line 149-153: The inline JSON.parse call for
document.__.visualEditorConfig used to compute requireOptIn in locator.tsx is
unguarded and can throw or return undefined; wrap the parse in a safe try/catch
(or reuse the same safe parser/helper used in directory.tsx) and explicitly
default requireAnalyticsOptIn to false on any parse error or missing value so
requireOptIn always becomes a boolean; update the expression that sets
requireOptIn (referencing requireOptIn and document.__.visualEditorConfig) to
call the safe parser and return parsed.requireAnalyticsOptIn ?? false.

In `@packages/visual-editor/src/vite-plugin/templates/main.tsx`:
- Around line 147-151: The inline JSON.parse call on
document.__.visualEditorConfig used to set the requireOptIn prop can throw and
also yields undefined instead of a boolean; update the code that computes
requireOptIn (the expression referencing document.__.visualEditorConfig and
requireOptIn) to parse safely inside a try/catch (or use a small safeParse
helper), default to an empty object on parse failure, and then read
.requireAnalyticsOptIn with a fallback to false so requireOptIn always becomes a
boolean.

---

Nitpick comments:
In `@packages/visual-editor/src/vite-plugin/templates/directory.tsx`:
- Around line 137-141: The duplicated JSON.parse(...) logic used to derive
requireOptIn from document.__.visualEditorConfig should be extracted into a
shared helper (e.g., getRequireOptIn(document)) in the generator/runtime so
templates reuse it; implement getRequireOptIn to safely access
document.__.visualEditorConfig, try/catch JSON.parse, return a boolean (default
false), and replace the inline expression in templates like the requireOptIn
prop in directory.tsx with a call to getRequireOptIn(document).

ℹ️ Review info

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 441f37a and eb3d4ec.

📒 Files selected for processing (3)
  • packages/visual-editor/src/vite-plugin/templates/directory.tsx
  • packages/visual-editor/src/vite-plugin/templates/locator.tsx
  • packages/visual-editor/src/vite-plugin/templates/main.tsx

benlife5
benlife5 previously approved these changes Feb 24, 2026
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
packages/visual-editor/src/vite-plugin/templates/main.tsx (1)

142-151: Consider explicit boolean coercion and consistent optional chaining.

Two minor points:

  1. Type safetyJSON.parse(...)?. requireAnalyticsOptIn ?? false produces any (because JSON.parse returns any). TypeScript silently allows assigning any to the inferred-boolean variable, so a mis-typed JSON value (e.g. 1, "true") would propagate unchecked to the requireOptIn prop. Coerce explicitly to boolean.

  2. Optional-chaining consistency – Line 143 uses document.__?.visualEditorConfig but line 146 uses document.__.visualEditorConfig. The guard makes the inner access safe, but the inconsistency is unnecessary noise.

♻️ Proposed fix
  let requireAnalyticsOptIn = false;
  if (document.__?.visualEditorConfig) {
    try {
      requireAnalyticsOptIn =
-       JSON.parse(document.__.visualEditorConfig)?.requireAnalyticsOptIn ??
-       false;
+       Boolean(JSON.parse(document.__?.visualEditorConfig)?.requireAnalyticsOptIn);
    } catch (e) {
      console.error("Failed to parse visualEditorConfig JSON:", e);
    }
  }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/visual-editor/src/vite-plugin/templates/main.tsx` around lines 142 -
151, The code that reads document.__?.visualEditorConfig into
requireAnalyticsOptIn should (1) use consistent optional chaining for both
checks (e.g., reference document.__?.visualEditorConfig in both places) and (2)
explicitly coerce the parsed value to a boolean instead of trusting JSON.parse's
any; update the block around requireAnalyticsOptIn to parse the config into a
local variable (using document.__?.visualEditorConfig), then set
requireAnalyticsOptIn = Boolean(parsed?.requireAnalyticsOptIn) or an explicit
comparison (e.g., parsed?.requireAnalyticsOptIn === true) to ensure a strict
boolean is assigned.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@packages/visual-editor/src/vite-plugin/templates/main.tsx`:
- Around line 142-151: The code that reads document.__?.visualEditorConfig into
requireAnalyticsOptIn should (1) use consistent optional chaining for both
checks (e.g., reference document.__?.visualEditorConfig in both places) and (2)
explicitly coerce the parsed value to a boolean instead of trusting JSON.parse's
any; update the block around requireAnalyticsOptIn to parse the config into a
local variable (using document.__?.visualEditorConfig), then set
requireAnalyticsOptIn = Boolean(parsed?.requireAnalyticsOptIn) or an explicit
comparison (e.g., parsed?.requireAnalyticsOptIn === true) to ensure a strict
boolean is assigned.

ℹ️ Review info

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between eb3d4ec and 0caceb3.

📒 Files selected for processing (3)
  • packages/visual-editor/src/vite-plugin/templates/directory.tsx
  • packages/visual-editor/src/vite-plugin/templates/locator.tsx
  • packages/visual-editor/src/vite-plugin/templates/main.tsx
🚧 Files skipped from review as they are similar to previous changes (2)
  • packages/visual-editor/src/vite-plugin/templates/directory.tsx
  • packages/visual-editor/src/vite-plugin/templates/locator.tsx

@jwartofsky-yext jwartofsky-yext added the create-dev-release Triggers dev release workflow label Feb 24, 2026
@pkg-pr-new
Copy link

pkg-pr-new bot commented Feb 24, 2026

commit: 0caceb3

@jwartofsky-yext jwartofsky-yext merged commit 1aafd79 into main Feb 24, 2026
17 of 18 checks passed
@jwartofsky-yext jwartofsky-yext deleted the requireOptInHandling branch February 24, 2026 20:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

create-dev-release Triggers dev release workflow

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants