Skip to content

Commit 2540865

Browse files
committed
add serializble track event object check
1 parent 55c06dc commit 2540865

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

www/UserNamespace.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { noop, removeListener } from './helpers';
1+
import { isObjectSerializable, noop, removeListener } from './helpers';
22
import PushSubscription from './PushSubscriptionNamespace';
33

44
// Represents the current user state
@@ -259,13 +259,11 @@ export default class User {
259259
* @returns void
260260
*/
261261
trackEvent(name: string, properties?: object): void {
262+
if (properties !== undefined && !isObjectSerializable(properties)) {
263+
console.error('Properties must be JSON-serializable');
264+
return;
265+
}
262266
const args = properties ? [name, properties] : [name];
263-
window.cordova.exec(
264-
function () {},
265-
function () {},
266-
'OneSignalPush',
267-
'trackEvent',
268-
args,
269-
);
267+
window.cordova.exec(noop, noop, 'OneSignalPush', 'trackEvent', args);
270268
}
271269
}

www/helpers.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,18 @@ export function removeListener<T>(
1515

1616
/** No-op function for cordova.exec error/success callbacks */
1717
export const noop = () => {};
18+
19+
/**
20+
* Returns true if the value is a JSON-serializable object.
21+
*/
22+
export function isObjectSerializable(value: unknown): boolean {
23+
if (!(typeof value === 'object' && value !== null && !Array.isArray(value))) {
24+
return false;
25+
}
26+
try {
27+
JSON.stringify(value);
28+
return true;
29+
} catch (e) {
30+
return false;
31+
}
32+
}

0 commit comments

Comments
 (0)