diff --git a/README.md b/README.md
index dc37e12..57a6ec1 100644
--- a/README.md
+++ b/README.md
@@ -62,13 +62,15 @@ export function Counter() {
const [count, setCount] = useState(0);
const increment = () => {
- setCount(count + 1);
- trackEvent("increment", { count });
+ const next = count + 1;
+ setCount(next);
+ trackEvent("increment", { count: next });
};
const decrement = () => {
- setCount(count - 1);
- trackEvent("decrement", { count });
+ const next = count - 1;
+ setCount(next);
+ trackEvent("decrement", { count: next });
};
return (
@@ -81,6 +83,8 @@ export function Counter() {
}
```
+Call `trackEvent` from event handlers like `onPress`, so each event maps to a user action.
+
To disable tracking events, you can call the `dispose` function. This will stop and deinitialize the SDK.
```js
@@ -95,7 +99,7 @@ A few important notes:
1. The SDK will automatically enhance the event with some useful information, like the OS, the app version, and other things.
2. You're in control of what gets sent to Aptabase. This SDK does not automatically track any events, you need to call `trackEvent` manually.
- - Because of this, it's generally recommended to at least track an event at startup
+ - Because of this, it's generally recommended to at least track an event at startup (e.g. right after `Aptabase.init`, outside of any component)
3. You do not need to await for the `trackEvent` function, it'll run in the background.
4. Only strings and numbers values are allowed on custom properties
diff --git a/examples/HelloWorldExpoRouter/app/(tabs)/index.tsx b/examples/HelloWorldExpoRouter/app/(tabs)/index.tsx
index 8ccbcf3..96a7029 100644
--- a/examples/HelloWorldExpoRouter/app/(tabs)/index.tsx
+++ b/examples/HelloWorldExpoRouter/app/(tabs)/index.tsx
@@ -1,10 +1,14 @@
import { Button, StyleSheet } from 'react-native';
-import { trackError } from '@aptabase/react-native';
+import { trackError, trackEvent } from '@aptabase/react-native';
import EditScreenInfo from '../../components/EditScreenInfo';
import { Text, View } from '../../components/Themed';
export default function TabOneScreen() {
+ const sendTestEvent = () => {
+ trackEvent('test_event', { source: 'HelloWorldExpoRouter' });
+ };
+
const trackHandledError = () => {
try {
throw new Error('handled test error from HelloWorldExpoRouter');
@@ -26,6 +30,7 @@ export default function TabOneScreen() {
Tab One
+
diff --git a/examples/HelloWorldExpoRouter/app/_layout.tsx b/examples/HelloWorldExpoRouter/app/_layout.tsx
index 5693509..65d94cd 100644
--- a/examples/HelloWorldExpoRouter/app/_layout.tsx
+++ b/examples/HelloWorldExpoRouter/app/_layout.tsx
@@ -8,7 +8,7 @@ import { useFonts } from "expo-font";
import { SplashScreen, Stack } from "expo-router";
import { useEffect } from "react";
import { useColorScheme } from "react-native";
-import { init, trackEvent } from "@aptabase/react-native";
+import { init } from "@aptabase/react-native";
export {
// Catch any errors thrown by the Layout component.
@@ -23,8 +23,9 @@ export const unstable_settings = {
// Prevent the splash screen from auto-hiding before asset loading is complete.
SplashScreen.preventAutoHideAsync();
+init("A-US-0928558097", { enableCrashReporting: true });
+
export default function RootLayout() {
- init("A-US-0928558097", { enableCrashReporting: true });
const [loaded, error] = useFonts({
SpaceMono: require("../assets/fonts/SpaceMono-Regular.ttf"),
...FontAwesome.font,
@@ -32,10 +33,7 @@ export default function RootLayout() {
// Expo Router uses Error Boundaries to catch errors in the navigation tree.
useEffect(() => {
- if (error) {
- trackEvent("error", { error: error.message });
- throw error;
- }
+ if (error) throw error;
}, [error]);
useEffect(() => {
diff --git a/llms.txt b/llms.txt
index 9151684..5f886b9 100644
--- a/llms.txt
+++ b/llms.txt
@@ -64,14 +64,20 @@ trackEvent("purchase_completed", {
});
```
-When using the Context provider, use the `useAptabase` hook:
+When using the Context provider, use the `useAptabase` hook and call `trackEvent` from an event handler:
```jsx
import { useAptabase } from "@aptabase/react-native";
+import { Button } from "react-native";
-function MyComponent() {
+function SaveButton() {
const { trackEvent } = useAptabase();
- trackEvent("screen_viewed", { screen: "home" });
+
+ function handlePress() {
+ trackEvent("settings_saved", { theme: "dark" });
+ }
+
+ return ;
}
```