Skip to content

fix: Analytics plugin - #2624

Closed
creeperkatze wants to merge 2 commits into
wxt-dev:mainfrom
creeperkatze:fix-analytics-plugin-main-world
Closed

creeperkatze wants to merge 2 commits into
wxt-dev:mainfrom
creeperkatze:fix-analytics-plugin-main-world

Conversation

@creeperkatze

Copy link
Copy Markdown
Contributor

Overview

The analytics plugin ran in every entrypoint, including MAIN-world content scripts without browser.runtime access. It imported #analytics at the top of the file, so createAnalytics() threw during module load and crashed the entrypoint before its own code ran. Now the import is dynamic and guarded by a browser.runtime.id check, so it's skipped where there's no runtime access instead of crashing.

Manual Testing

Added a MAIN-world content script next to the analytics module and confirmed it no longer crashes, while analytics still works in the background, popup, and isolated content scripts.

Related Issue

This PR closes #1847

@netlify

netlify Bot commented Sep 18, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for creative-fairy-df92c4 ready!

Name Link
🔨 Latest commit 4271794
🔍 Latest deploy log https://app.netlify.com/projects/creative-fairy-df92c4/deploys/6abbbcc574d8fa0008164f64
😎 Deploy Preview https://deploy-preview-2624--creative-fairy-df92c4.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@github-actions github-actions Bot added the pkg/analytics Includes changes to the `packages/analytics` directory label Sep 18, 2026
@creeperkatze

Copy link
Copy Markdown
Contributor Author
image

@creeperkatze

Copy link
Copy Markdown
Contributor Author

Once addWxtPlugin supports filtering by entrypoint type, the background-plugin could just be registered as background-only instead of guarding itself at runtime. That would fix it properly. This PR is a fix for the immediate crash.

@coderabbitai

coderabbitai Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Advanced

Run ID: 369def75-2921-41c8-a094-70516e88a055

📥 Commits

Reviewing files that changed from the base of the PR and between bda3a5c and 4271794.

📒 Files selected for processing (1)
  • packages/analytics/modules/analytics/background-plugin.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 7 remain after this review.


📝 Walkthrough

Walkthrough

The analytics background plugin now dynamically imports #analytics only when browser.runtime.id is present. Previously, it imported the module unconditionally.

Changes

Analytics runtime guard

Layer / File(s) Summary
Conditional analytics loading
packages/analytics/modules/analytics/background-plugin.ts
The plugin checks for browser.runtime.id before dynamically importing #analytics.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Merge Risk: 🔵 Low · up to 42717

Background analytics may miss an install or update event if it arrives before the provider is ready. This is a limited reporting gap; the change remains mergeable with awareness of the potential telemetry loss.

Architecture Summary

Architecture risk: 🔵 Low · up to 42717

The change affects 1 system.

Changed systems: packages/analytics

Architecture concerns
No architecture-level concerns identified.

Review details

Systems and components

  • observed — packages/analytics (library) was modified; 1 changed file maps to changed impact.

Before / after behavior

  • observed — Modified behavior in packages/analytics/modules/analytics/background-plugin.ts: The plugin replaces the unconditional #analytics import with a dynamic import guarded by the presence of browser.runtime.id.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description check ✅ Passed The description includes all required sections. It explains the crash, the dynamic import guard, manual testing, and the related issue.
Title check ✅ Passed The title is concise and directly relates to the analytics plugin fix described in the changes.
Linked Issues check ✅ Passed Issue [#1847] requires the analytics integration to avoid failing in contexts without browser.runtime access. At the reviewed head, background-plugin.ts checks browser?.runtime?.id before it dyn…
Out of Scope Changes check ✅ Passed The reviewed change is limited to packages/analytics/modules/analytics/background-plugin.ts. It adds the runtime-access guard and conditional dynamic import that directly address issue [#1847]. The …
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 1…
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR

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

@creeperkatze

Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Sep 30, 2026 •

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@aklinker1 aklinker1 left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This changes the setup from a synchronous import to a dynamic one. I don't want to do that.

The "correct" change is to add the abilitiy to choose which entrypoints a plugin runs in. That's a bigger change, but one I've been meaning to make for a while. If you want to take a crack at it, please do.

@aklinker1

Copy link
Copy Markdown
Member

Looks like someone has already opened a PR to allow filtering which entrypoints a plugin is loaded into.

#2613

@creeperkatze

Copy link
Copy Markdown
Contributor Author

Closing in favor of #2613

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pkg/analytics Includes changes to the `packages/analytics` directory

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cannot use WXT analytics in contexts without access to the browser.runtime APIs

2 participants