Skip to content

Conversation

robertsreberski
Copy link
Contributor

@robertsreberski robertsreberski commented Jul 10, 2025

Completes MYJP-223

Proposed Changes

This PR introduces comprehensive JavaScript error tracking to the My Jetpack interface using a custom error monitoring solution that integrates with Jetpack Analytics.

Features Added

  • Custom JPJSErrorTracker Library (projects/packages/my-jetpack/src/js-static/jp-js-error-tracker/index.js):

    • Captures JavaScript errors, resource failures, network issues, and performance problems
    • Uses cryptographically secure session IDs via crypto.getRandomValues()
    • Implements error throttling (1-second) to prevent spam
    • Provides rich context data including browser info, viewport, route, and timing
  • My Jetpack Integration (projects/packages/my-jetpack/_inc/utils/error-tracker.js):

    • Initializes error tracker with My Jetpack-specific context
    • Sends error data to Jetpack Analytics via jetpack_my_jetpack_js_error events
    • Includes route tracking and user agent information
  • Testing Component (projects/packages/my-jetpack/components/error-tracker-test/index.jsx):

    • Development-only component for testing different error types
    • Provides buttons to trigger JavaScript, network, resource, and promise errors
    • Shows real-time error count and history

Technical Implementation

  • Error tracker script loaded before React app initialization
  • Automatic registration in class-initializer.php:267
  • Error tracking initialized in admin.jsx:115 before React rendering
  • Test component conditionally shown in development builds

Other information:

  • Have you written new tests for your changes, if applicable?
  • Have you checked the E2E test CI results, and verified that your changes do not break them?
  • Have you tested your changes on WordPress.com, if applicable (if so, you'll see a generated comment below with a script to run)?

Jetpack product discussion

Does this pull request change what data or activity we track or use?

Yes,

Error Data Collected

Each error includes comprehensive context:

  • Error details (message, stack trace, filename, line/column)
  • Session information (secure session ID, timestamp, error count)
  • Browser context (user agent, viewport, language, platform)
  • Location data (route, URL, referrer)
  • Performance metrics (time from page load)

Testing instructions:

  1. Check out this branch and build My Jetpack
  2. Visit the My Jetpack interface in development mode
  3. Look for the "🚨 Error Tracker Test Panel" at the top of the interface
  4. Use the colored buttons to trigger different error types:
    • JavaScript Error - Triggers a TypeError
    • Promise Rejection - Creates unhandled promise rejection
    • Network Error - Attempts to fetch non-existent endpoint
    • Resource Error - Loads non-existent image
    • Type Error - Triggers undefined property access
    • Reference Error - Calls non-existent function
  5. Click "Check Error History" to see captured errors in console
  6. Verify errors are sent to Jetpack Analytics (check network tab for jetpack_my_jetpack_js_error events)

Copy link
Contributor

github-actions bot commented Jul 10, 2025

Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.

  • To test on WoA, go to the Plugins menu on a WoA dev site. Click on the "Upload" button and follow the upgrade flow to be able to upload, install, and activate the Jetpack Beta plugin. Once the plugin is active, go to Jetpack > Jetpack Beta, select your plugin (Jetpack), and enable the add/jpjse branch.
  • To test on Simple, run the following command on your sandbox:
bin/jetpack-downloader test jetpack add/jpjse

Interested in more tips and information?

  • In your local development environment, use the jetpack rsync command to sync your changes to a WoA dev blog.
  • Read more about our development workflow here: PCYsg-eg0-p2
  • Figure out when your changes will be shipped to customers here: PCYsg-eg5-p2

@github-actions github-actions bot added [Package] My Jetpack [Plugin] Backup A plugin that allows users to save every change and get back online quickly with one-click restores. [Plugin] Boost A feature to speed up the site and improve performance. [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ [Plugin] Protect A plugin with features to protect a site: brute force protection, security scanning, and a WAF. [Plugin] Search A plugin to add an instant search modal to your site to help visitors find content faster. [Plugin] Social Issues about the Jetpack Social plugin [Plugin] Starter Plugin [Plugin] VideoPress A standalone plugin to add high-quality VideoPress videos to your site. [Status] In Progress labels Jul 10, 2025
Copy link
Contributor

github-actions bot commented Jul 10, 2025

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • ✅ Add a "[Status]" label (In Progress, Needs Review, ...).
  • ✅ Add a "[Type]" label (Bug, Enhancement, Janitorial, Task).
  • ✅ Add testing instructions.
  • ✅ Specify whether this PR includes any changes to data or privacy.
  • ✅ Add changelog entries to affected projects

This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖


Follow this PR Review Process:

  1. Ensure all required checks appearing at the bottom of this PR are passing.
  2. Make sure to test your changes on all platforms that it applies to. You're responsible for the quality of the code you ship.
  3. You can use GitHub's Reviewers functionality to request a review.
  4. When it's reviewed and merged, you will be pinged in Slack to deploy the changes to WordPress.com simple once the build is done.

If you have questions about anything, reach out in #jetpack-developers for guidance!


Jetpack plugin:

The Jetpack plugin has different release cadences depending on the platform:

  • WordPress.com Simple releases happen as soon as you deploy your changes after merging this PR (PCYsg-Jjm-p2).
  • WoA releases happen weekly.
  • Releases to self-hosted sites happen monthly:
    • Scheduled release: September 2, 2025
    • Code freeze: September 1, 2025

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.


Backup plugin:

No scheduled milestone found for this plugin.

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.


Boost plugin:

No scheduled milestone found for this plugin.

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.


Search plugin:

No scheduled milestone found for this plugin.

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.


Social plugin:

No scheduled milestone found for this plugin.

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.


Starter Plugin plugin:

No scheduled milestone found for this plugin.

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.


Protect plugin:

  • Next scheduled release: September 28, 2025

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.


Videopress plugin:

No scheduled milestone found for this plugin.

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.

@github-actions github-actions bot added the [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. label Jul 10, 2025
Copy link

jp-launch-control bot commented Jul 10, 2025

Code Coverage Summary

Coverage changed in 2 files.

File Coverage Δ% Δ Uncovered
projects/packages/my-jetpack/src/class-initializer.php 54/308 (17.53%) -0.59% 10 💔
projects/packages/my-jetpack/_inc/admin.jsx 0/16 (0.00%) 0.00% 1 ❤️‍🩹

2 files are newly checked for coverage.

File Coverage
projects/packages/my-jetpack/_inc/utils/error-tracker.js 0/8 (0.00%) 💔
projects/packages/my-jetpack/src/js-static/jp-js-error-tracker/index.js 0/118 (0.00%) 💔

Full summary · PHP report · JS report

If appropriate, add one of these labels to override the failing coverage check: Covered by non-unit tests Use to ignore the Code coverage requirement check when E2Es or other non-unit tests cover the code Coverage tests to be added later Use to ignore the Code coverage requirement check when tests will be added in a follow-up PR I don't care about code coverage for this PR Use this label to ignore the check for insufficient code coveage.

Moved the initializeErrorTracker function from admin.jsx to a new utils/error-tracker.js file for better modularity and maintainability. Updated imports to use the new location.
…e docs

Renamed jp-js-error-tracker.js to jp-js-error-tracker/index.js for better module organization and updated the script registration path in class-initializer.php accordingly.
Introduces documentation detailing features, usage, error data structure, configuration, and security considerations for the My Jetpack JavaScript error tracking solution.
@github-actions github-actions bot added the Docs label Jul 14, 2025
@manzoorwanijk manzoorwanijk requested a review from a team July 14, 2025 05:42
@manzoorwanijk manzoorwanijk marked this pull request as ready for review July 14, 2025 05:42
@manzoorwanijk manzoorwanijk marked this pull request as draft July 14, 2025 05:43
@manzoorwanijk manzoorwanijk self-assigned this Jul 14, 2025
@manzoorwanijk manzoorwanijk marked this pull request as ready for review July 14, 2025 05:52
@manzoorwanijk manzoorwanijk removed the [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. label Jul 14, 2025
@manzoorwanijk manzoorwanijk added [Type] Enhancement Changes to an existing feature — removing, adding, or changing parts of it [Status] Needs Team Review Obsolete. Use Needs Review instead. and removed [Status] In Progress labels Jul 14, 2025
Copy link
Contributor

@tbradsha tbradsha left a comment

Choose a reason for hiding this comment

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

I did a light review and left some comments inline. Here are a few more comments:

  • Any idea of what kind of volume you might get? It might be good to limit to some specific error types to start.
  • One limitation of not using a custom endpoint is not being able to control things on the receiving side (e.g. filtering the type of things we collect on the fly rather than waiting for each release).
  • Batching errors into a single call would be a nice way to limit requests sent, but that'd probably also best be done via a custom endpoint.
  • It might be worth splitting this into a separate package down the road. The downside would be that people might actually consume it without care. 😄
  • Can we add a few simple unit tests (e.g. run JS with no error and run JS with an error)? I'm not sure how easy that would be to mock.


const clearErrors = useCallback( () => {
if ( window.myJetpackErrorTracker ) {
window.myJetpackErrorTracker.clearErrors();
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this ever defined?

Copy link
Member

Choose a reason for hiding this comment

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

It was defined in its first iteration but remained in this test component which is going to be removed before merging this.

Copy link
Member

Choose a reason for hiding this comment

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

Updated.

Comment on lines +64 to +65
const history = window.myJetpackErrorTracker.getErrorHistory();
const count = window.myJetpackErrorTracker.getErrorCount();
Copy link
Contributor

Choose a reason for hiding this comment

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

Are these ever defined?

Copy link
Member

Choose a reason for hiding this comment

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


- **Modern Browsers**: Full feature support including Performance Observer
- **Legacy Browsers**: Graceful degradation with core error tracking
- **Crypto API**: Uses secure random generation when available, falls back to Math.random()
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't understand why we'd need cryptographically-secure randomness here. We're just generating an arbitrary session ID to connect Tracks events.

Copy link
Member

Choose a reason for hiding this comment

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

It was Claude responding to #44266 (review) but yes I agree that it's not a big deal here.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd either stick with Math.random() (as that should be adequate for our needs). If it's not adequate, we shouldn't be allowing it as a fallback.

Copy link
Member

Choose a reason for hiding this comment

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

Updated to stick to Math.random

this.pageLoadTime = Date.now();
this.errorCount = 0;
this.throttledErrors = new Map();
this.throttleMs = 1000; // Fixed throttle time
Copy link
Contributor

Choose a reason for hiding this comment

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

The docs indicate this is configurable, but it seems to be hard-coded? Also, it might be wise to be a bit more conservative at 5s to start.

Copy link
Member

Choose a reason for hiding this comment

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

Updated the code to reflect the docs

this.onError = onError;
this.sessionId = this.generateSessionId();
this.pageLoadTime = Date.now();
this.errorCount = 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if we should have a maxErrors as well, rolling off the oldest errors to prevent this from getting too large. However, in order to do that properly we'd need an accurate stack of all errors, not just unique ones.

Copy link
Member

Choose a reason for hiding this comment

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

Updated.

const self = this;

// Monitor fetch
if ( window.fetch ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be available in any modern browser.

I'll also note that it'd be good to have a way to prevent one from double-monkeypatching the function. Right now each time you run setupNetworkMonitoring() the call will get more deeply nested; we shouldn't allow it to run more than once.

One solution would be to store the original fetch function as a new property in window, and then check for that property prior to the monkeypatch.

Copy link
Member

Choose a reason for hiding this comment

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

That is a great point. I have updated it to handle that case.

## Browser Support

- **Modern Browsers**: Full feature support including Performance Observer
- **Legacy Browsers**: Graceful degradation with core error tracking
Copy link
Contributor

Choose a reason for hiding this comment

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

In my opinion, it's probably not worth adding any support or fallbacks for legacy browsers. Worst case, the data doesn't send. There's no change to the user experience, and it's not like we're going to be expanding support to older browsers in the future.

Copy link
Member

Choose a reason for hiding this comment

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

Removed

@manzoorwanijk manzoorwanijk requested a review from a team July 21, 2025 10:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Docs [Package] My Jetpack [Plugin] Backup A plugin that allows users to save every change and get back online quickly with one-click restores. [Plugin] Boost A feature to speed up the site and improve performance. [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ [Plugin] Protect A plugin with features to protect a site: brute force protection, security scanning, and a WAF. [Plugin] Search A plugin to add an instant search modal to your site to help visitors find content faster. [Plugin] Social Issues about the Jetpack Social plugin [Plugin] Starter Plugin [Plugin] VideoPress A standalone plugin to add high-quality VideoPress videos to your site. [Status] In Progress [Status] Needs Team Review Obsolete. Use Needs Review instead. [Type] Enhancement Changes to an existing feature — removing, adding, or changing parts of it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants