Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/RNOneSignalTS/OSButtons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ class OSButtons extends React.Component<Props> {
loggingFunction('Tracking event: ', 'ReactNative');
const platform = Platform.OS; // This will be 'ios' or 'android'
OneSignal.User.trackEvent(`ReactNative-${platform}`, {
ABC: '123',
DEF: '456',
});
});

Expand Down
21 changes: 20 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@
export namespace User {
export namespace pushSubscription {
/** Add a callback that fires when the OneSignal subscription state changes. */
export function addEventListener(

Check warning on line 290 in src/index.ts

View workflow job for this annotation

GitHub Actions / build

'addEventListener' is already declared in the upper scope on line 412 column 21
event: 'change',
listener: (event: PushSubscriptionChangedState) => void,
) {
Expand All @@ -302,7 +302,7 @@
}

/** Clears current subscription observers. */
export function removeEventListener(

Check warning on line 305 in src/index.ts

View workflow job for this annotation

GitHub Actions / build

'removeEventListener' is already declared in the upper scope on line 427 column 21
event: 'change',
listener: (event: PushSubscriptionChangedState) => void,
) {
Expand Down Expand Up @@ -609,10 +609,14 @@
/** Track custom events for the current user. */
export function trackEvent(
name: string,
properties?: Record<string, string>,
properties?: Record<string, unknown>,
) {
if (!isNativeModuleLoaded(RNOneSignal)) return;

if (!isObjectSerializable(properties)) {
return console.error('Properties must be JSON-serializable');
}

RNOneSignal.trackEvent(name, properties);
}
}
Expand Down Expand Up @@ -1010,6 +1014,21 @@
}
}

/**
* Returns true if the value is a JSON-serializable object.
*/
function isObjectSerializable(value: unknown): boolean {
if (!(typeof value === 'object' && value !== null && !Array.isArray(value))) {
return false;
}
try {
JSON.stringify(value);
return true;
} catch (e) {
return false;
}
}

export {
InAppMessage,
InAppMessageClickEvent,
Expand Down
Loading