Skip to content

Commit 70d188c

Browse files
committed
add check for json stringifiable properties
1 parent f3840da commit 70d188c

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

examples/RNOneSignalTS/OSButtons.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ class OSButtons extends React.Component<Props> {
435435
loggingFunction('Tracking event: ', 'ReactNative');
436436
const platform = Platform.OS; // This will be 'ios' or 'android'
437437
OneSignal.User.trackEvent(`ReactNative-${platform}`, {
438-
ABC: '123',
438+
DEF: '456',
439439
});
440440
});
441441

src/index.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,10 +609,14 @@ export namespace OneSignal {
609609
/** Track custom events for the current user. */
610610
export function trackEvent(
611611
name: string,
612-
properties?: Record<string, string>,
612+
properties?: Record<string, unknown>,
613613
) {
614614
if (!isNativeModuleLoaded(RNOneSignal)) return;
615615

616+
if (!isObjectSerializable(properties)) {
617+
return console.error('Properties must be JSON-serializable');
618+
}
619+
616620
RNOneSignal.trackEvent(name, properties);
617621
}
618622
}
@@ -1010,6 +1014,21 @@ export namespace OneSignal {
10101014
}
10111015
}
10121016

1017+
/**
1018+
* Returns true if the value is a JSON-serializable object.
1019+
*/
1020+
function isObjectSerializable(value: unknown): boolean {
1021+
if (!(typeof value === 'object' && value !== null && !Array.isArray(value))) {
1022+
return false;
1023+
}
1024+
try {
1025+
JSON.stringify(value);
1026+
return true;
1027+
} catch (e) {
1028+
return false;
1029+
}
1030+
}
1031+
10131032
export {
10141033
InAppMessage,
10151034
InAppMessageClickEvent,

0 commit comments

Comments
 (0)