Commit b84f02d
WooCommerce Analytics: Use Beacon API for reliable event tracking with fallback (#45891)
* Analytics: Use Beacon API for reliable event tracking with keepalive 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 <[email protected]>
* changelog
* Enhance event tracking reliability by checking for trackEndpoint availability
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
* Update pnpm-lock.yaml
---------
Co-authored-by: Claude <[email protected]>1 parent 616d794 commit b84f02d
File tree
7 files changed
+61
-15
lines changed- projects/packages/woocommerce-analytics
- changelog
- src
- client
- types
7 files changed
+61
-15
lines changedSome generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 4 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
28 | | - | |
29 | 28 | | |
30 | 29 | | |
31 | 30 | | |
| |||
Lines changed: 3 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
86 | 86 | | |
87 | 87 | | |
88 | 88 | | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
89 | 92 | | |
90 | 93 | | |
91 | 94 | | |
| |||
Lines changed: 53 additions & 9 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
5 | 4 | | |
6 | 5 | | |
7 | 6 | | |
8 | 7 | | |
9 | | - | |
| 8 | + | |
10 | 9 | | |
11 | 10 | | |
12 | 11 | | |
| |||
77 | 76 | | |
78 | 77 | | |
79 | 78 | | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
80 | 105 | | |
81 | 106 | | |
82 | 107 | | |
| |||
93 | 118 | | |
94 | 119 | | |
95 | 120 | | |
96 | | - | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
97 | 135 | | |
98 | 136 | | |
99 | 137 | | |
| |||
109 | 147 | | |
110 | 148 | | |
111 | 149 | | |
112 | | - | |
113 | | - | |
| 150 | + | |
114 | 151 | | |
115 | | - | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
116 | 158 | | |
117 | | - | |
118 | 159 | | |
119 | | - | |
120 | | - | |
| 160 | + | |
| 161 | + | |
121 | 162 | | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
122 | 166 | | |
123 | 167 | | |
124 | 168 | | |
| |||
Lines changed: 0 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
7 | | - | |
8 | 6 | | |
9 | 7 | | |
Lines changed: 1 addition & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
| |||
0 commit comments