Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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
Expand All @@ -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

Expand Down
7 changes: 6 additions & 1 deletion examples/HelloWorldExpoRouter/app/(tabs)/index.tsx
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -26,6 +30,7 @@ export default function TabOneScreen() {
<Text style={styles.title}>Tab One</Text>
<View style={styles.separator} lightColor="#eee" darkColor="rgba(255,255,255,0.1)" />
<EditScreenInfo path="app/(tabs)/index.tsx" />
<Button onPress={sendTestEvent} title="Send test event" />
<Button onPress={trackHandledError} title="Track handled error" />
<Button onPress={throwUncaughtError} title="Throw uncaught error" />
</View>
Expand Down
10 changes: 4 additions & 6 deletions examples/HelloWorldExpoRouter/app/_layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -23,19 +23,17 @@ 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,
});

// 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(() => {
Expand Down
12 changes: 9 additions & 3 deletions llms.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 <Button title="Save" onPress={handlePress} />;
}
```

Expand Down
Loading