-
Notifications
You must be signed in to change notification settings - Fork 841
WooCommerce Analytics: Use Beacon API for reliable event tracking with fallback #45891
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
WooCommerce Analytics: Use Beacon API for reliable event tracking with fallback #45891
Conversation
…fallback Implements navigator.sendBeacon() as the primary method for sending analytics events, with graceful fallback to fetch() with keepalive option. This ensures reliable event delivery, especially during page unload scenarios where traditional async requests may be cancelled by the browser. Changes: - Add sendEventsViaBeacon() method using navigator.sendBeacon API - Replace @wordpress/api-fetch with native fetch() + keepalive option - Pass REST API endpoint URL from server to handle plain permalinks correctly - Add trackEndpoint to wcAnalytics config in PHP (using rest_url()) - Remove @wordpress/api-fetch dependency from package.json - Remove unused API_NAMESPACE and API_ENDPOINT constants Benefits: - Guaranteed event delivery on page unload (browser queues beacon requests) - Better mobile browser support (especially iOS Safari) - Works correctly with plain permalinks - Filters out sophisticated bots that disable beaconing APIs - Simpler codebase with native Web APIs 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
|
Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.
Interested in more tips and information?
|
|
Thank you for your PR! When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:
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:
If you have questions about anything, reach out in #jetpack-developers for guidance! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR improves the reliability of WooCommerce Analytics event tracking by implementing the Beacon API as the primary transport method with a fetch-based fallback, and removes the @wordpress/api-fetch dependency in favor of native browser APIs.
Key Changes:
- Implements
navigator.sendBeacon()for guaranteed event delivery during page unload/navigation - Adds fallback to native
fetch()withkeepalive: truefor browsers without Beacon API support - Moves REST API endpoint URL generation to server-side using
rest_url()for better plain permalink support
Reviewed Changes
Copilot reviewed 5 out of 7 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
src/client/types/global.d.ts |
Adds trackEndpoint property to window.wcAnalytics interface |
src/client/constants.ts |
Removes unused API_NAMESPACE and API_ENDPOINT constants now that endpoint is server-controlled |
src/client/api-client.ts |
Replaces @wordpress/api-fetch with Beacon API and native fetch, adds new sendEventsViaBeacon method |
src/class-universal.php |
Injects REST API tracking endpoint URL via rest_url() into client-side wcAnalytics object |
package.json |
Removes @wordpress/api-fetch dependency |
changelog/wooa7s-661-use-beaconing-apis-for-sending-tracking-events |
Adds changelog entry for the changes |
pnpm-lock.yaml |
Updates lockfile to reflect dependency removal |
Files not reviewed (1)
- pnpm-lock.yaml: Language not supported
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…lability This update adds a check for the existence of the trackEndpoint in the wcAnalytics configuration before attempting to send events. If the trackEndpoint is not available, a debug message is logged, and the event sending process is halted. The fallback mechanism to fetch with keepalive is retained for cases where the Beacon API fails, ensuring robust event delivery. Changes: - Added a conditional check for window.wcAnalytics?.trackEndpoint - Updated debug logging for better clarity on event sending status
Code Coverage SummaryCoverage changed in 1 file.
|
Fixes WOOA7S-661
Proposed changes:
navigator.sendBeacon()as the primary method for sending analytics eventsfetch()withkeepalive: trueoption for browsers without beacon supportrest_url())@wordpress/api-fetchdependency with nativefetch()API for simpler, more portable codeAPI_NAMESPACE,API_ENDPOINT) as endpoint is now fully server-controlledOther information:
Does this pull request change what data or activity we track or use?
No. This PR only changes how events are sent to the server, not what data is tracked. The tracking endpoint, event payloads, and data handling remain unchanged.
Testing instructions:
Prerequisites:
add_filter('woocommerce_analytics_experimental_proxy_tracking_enabled', '__return_true');OR enable WooCommerce Analytics pluginTest 1: Verify Beacon API is used
localStorage.setItem('debug', 'wc-analytics:*')"Sent X events via Beacon API"/wp-json/woocommerce-analytics/v1/trackbut can see requests in All tab.Test 2: Verify fallback for browsers without beacon
Preserve logcheckboxnavigator.sendBeacon = undefined(simulates old browser)"Failed to send events via Beacon API, falling back to fetch with keepalive"/wp-json/woocommerce-analytics/v1/trackwithkeepaliveflagKey Benefits: