Skip to content

Commit d764e44

Browse files
committed
fix(rest): return full checksum string for JS tracker compatibility
The REST handler was stripping the checksum and returning only the bare numeric ID. The JS tracker stores the response as params.id and sends it back on subsequent requests (events, consent upgrade), where Utils::getValueWithoutChecksum() requires the full "<id>.<hash>" format. Also applies Yoda conditions per WP coding standards, removes redundant conditional assertion in fallback test, and strengthens REST response test to validate checksum format.
1 parent 051c799 commit d764e44

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

src/Controllers/Rest/TrackingRestController.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,13 +189,15 @@ public function handle_tracking(\WP_REST_Request $request)
189189
$result = Tracker::slimtrack_ajax();
190190

191191
// Success: pure numeric ID (rare — most paths return checksum format)
192-
if (is_numeric($result) && (int) $result > 0) {
192+
if (is_numeric($result) && 0 < (int) $result) {
193193
return rest_ensure_response((string) $result);
194194
}
195195

196196
// Success: checksum-formatted string "<id>.<hash>" from Utils::getValueWithChecksum()
197-
if (is_string($result) && preg_match('/^(\d+)\.[0-9a-fA-F]+$/', $result, $matches) && (int) $matches[1] > 0) {
198-
return rest_ensure_response($matches[1]);
197+
// Return the full checksum string so the JS tracker can send it back for
198+
// subsequent requests (consent upgrade, events) where getValueWithoutChecksum() validates it.
199+
if (is_string($result) && preg_match('/^(\d+)\.[0-9a-fA-F]+$/', $result, $matches) && 0 < (int) $matches[1]) {
200+
return rest_ensure_response($result);
199201
}
200202

201203
// If no valid tracking ID detected, return a non-200 status to trigger fallback tracking methods

tests/e2e/tracking-request-methods.spec.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,17 @@ test.describe('Tracking Request Methods — All Transports', () => {
145145
const stat = await waitForStatRow(marker);
146146
expect(stat).toBeTruthy();
147147

148-
// If we captured the response, verify it contains a numeric ID
149-
// REST response wraps the result in JSON — the body should contain a numeric string
148+
// If we captured the response, verify it contains a valid checksum-formatted tracking ID
150149
if (restResponseBody) {
151-
// Response should be parseable (not a 500 error page)
152150
expect(restResponseBody).not.toContain('Internal Server Error');
151+
// REST wraps the result in JSON — parse and verify checksum format "<id>.<hash>"
152+
try {
153+
const parsed = JSON.parse(restResponseBody);
154+
const body = typeof parsed === 'string' ? parsed : String(parsed);
155+
expect(body).toMatch(/^\d+\.[0-9a-fA-F]+$/);
156+
} catch {
157+
// sendBeacon may produce non-JSON response — skip format check
158+
}
153159
}
154160
});
155161
});
@@ -331,10 +337,7 @@ test.describe('Tracking Request Methods — All Transports', () => {
331337
expect(stat!.resource).toContain(marker);
332338

333339
// When REST is blocked, AJAX should be used as fallback
334-
// (unless server-side tracking captured it first)
335-
if (ajaxFallbackUsed) {
336-
expect(ajaxFallbackUsed).toBe(true);
337-
}
340+
expect(ajaxFallbackUsed).toBe(true);
338341
});
339342
});
340343

0 commit comments

Comments
 (0)