From de9fc0f76a1c453977a1b707db1b03a0a8b5a99d Mon Sep 17 00:00:00 2001 From: Bogdan Ivanov Date: Thu, 5 Nov 2026 08:45:20 +0200 Subject: [PATCH 1/2] docs: track events from handlers, not render logic - llms.txt: replace the useAptabase snippet that called trackEvent in the component body with an onPress handler, and add guidance against render/effect tracking. - README: send the updated count (not the stale one) and note where to call trackEvent. - HelloWorldExpoRouter: call init once at module load instead of on every render, drop trackEvent from useEffect, and add a 'Send test event' button. Co-Authored-By: Claude Opus 5.5 --- README.md | 14 +++++++++----- examples/HelloWorldExpoRouter/app/(tabs)/index.tsx | 8 +++++++- examples/HelloWorldExpoRouter/app/_layout.tsx | 11 +++++------ llms.txt | 14 +++++++++++--- 4 files changed, 32 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index dc37e12..d700d6d 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` (user actions), not directly in a component body or inside `useEffect`. Components re-render for many reasons that have nothing to do with the user, so tracking from render logic would count those re-renders as user activity. + 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..9411b57 100644 --- a/examples/HelloWorldExpoRouter/app/(tabs)/index.tsx +++ b/examples/HelloWorldExpoRouter/app/(tabs)/index.tsx @@ -1,10 +1,15 @@ 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() { + // Track user actions from event handlers, not from the component body or useEffect + const sendTestEvent = () => { + trackEvent('test_event', { source: 'HelloWorldExpoRouter' }); + }; + const trackHandledError = () => { try { throw new Error('handled test error from HelloWorldExpoRouter'); @@ -26,6 +31,7 @@ export default function TabOneScreen() { Tab One +