diff --git a/README.md b/README.md index 5874a08..7feaea6 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,12 @@ export function Counter() { ); } ``` +To disable tracking events, you can call the `dispose` function. This will stop and deinitalize the SDK. +```js +import Aptabase from "@aptabase/react-native"; + +Aptabase.dispose(); +``` **Note for Expo apps:** Events sent during development while running on Expo Go will not have the `App Version` property because native modules are not available in Expo Go. However, when you build your app and run it on a real device, the `App Version` property will be available. Alternatively, you can also set the `appVersion` during the `init` call so that it's also available during development. diff --git a/src/index.ts b/src/index.ts index 13cae3f..bc8ac24 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,6 @@ export type { AptabaseOptions } from "./types"; export { AptabaseProvider, useAptabase } from "./context"; -import { init, trackEvent } from "./track"; -export { init, trackEvent }; +import { init, trackEvent, dispose } from "./track"; +export { init, trackEvent, dispose }; -export default { init, trackEvent }; +export default { init, trackEvent, dispose }; diff --git a/src/track.ts b/src/track.ts index 8d61120..49bd301 100644 --- a/src/track.ts +++ b/src/track.ts @@ -16,8 +16,7 @@ export function init(appKey: string, options?: AptabaseOptions) { const [ok, msg] = validate(Platform.OS, appKey, options); if (!ok) { if (_client) { - _client.stopPolling(); - _client = undefined; + dispose(); } console.warn(`Aptabase: ${msg}. Tracking will be disabled.`); return; @@ -42,6 +41,18 @@ export function init(appKey: string, options?: AptabaseOptions) { }); } +/** + * Dispose the SDK and stop tracking events + */ +export function dispose() { + if (_client) { + _client.stopPolling(); + _client = undefined; + } else { + console.warn(`Aptabase: dispose was called but SDK was not initialized.`); + } +} + /** * Track an event using given properties * @param {string} eventName - The name of the event to track