Skip to content

Commit

Permalink
chore: Add dispose function to SDK for stopping event tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert27 committed Jul 28, 2024
1 parent a156327 commit 2952e30
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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 };
15 changes: 13 additions & 2 deletions src/track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down

0 comments on commit 2952e30

Please sign in to comment.