Skip to content

Commit 0de309b

Browse files
committed
fix(pillarbox-monitoring): sendEvent payload sent has a JSON string
Fixes #302 by improving compatibility and reliability of the event transmission. - replace the JSON string payload with a Blob object to ensure proper handling of the data type when sending events to the server using the Beacon API - update `sendEvent` test cases
1 parent bc5d4ce commit 0de309b

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

src/trackers/PillarboxMonitoring.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -625,12 +625,14 @@ class PillarboxMonitoring {
625625
!this.currentSessionId
626626
) return;
627627

628-
const payload = JSON.stringify({
628+
const payload = new Blob([JSON.stringify({
629629
event_name: eventName,
630630
session_id: this.currentSessionId,
631631
timestamp: PillarboxMonitoring.timestamp(),
632632
version: this.schemaVersion,
633633
data
634+
})], {
635+
type: 'application/json'
634636
});
635637

636638
navigator.sendBeacon(

test/trackers/pillarbox-monitoring.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ describe('PillarboxMonitoring', () => {
558558
property: 'value'
559559
});
560560

561-
expect(spyOnSendBeacon).toHaveBeenCalledWith(expect.any(String), expect.any(String));
561+
expect(spyOnSendBeacon).toHaveBeenCalledWith(expect.any(String), expect.any(Blob));
562562
});
563563

564564
it('should only send a STOP event if there was a previous session', () => {
@@ -581,7 +581,7 @@ describe('PillarboxMonitoring', () => {
581581
});
582582

583583
expect(spyOnSendBeacon).toHaveBeenCalledTimes(1);
584-
expect(spyOnSendBeacon).toHaveBeenCalledWith(expect.any(String), expect.any(String));
584+
expect(spyOnSendBeacon).toHaveBeenCalledWith(expect.any(String), expect.any(Blob));
585585

586586
spyOnIsTrackerDisabled.mockRestore();
587587
});

0 commit comments

Comments
 (0)