Visitors and offline
How apps count visitors without storing anything, when to pass storage, and how events wait for a connection.
Phones share carrier addresses and move between networks, so an app cannot count visitors the way the web script does. The package sends its own random ID instead, and it holds events while the phone is offline. The samples on this page are for React Native, and Swift apps has the Swift ones.
Visitors
The package makes a random ID when the app starts and keeps it in memory. The server hashes it with a key that changes every day and never stores it. Nothing is written to the phone, so each cold start counts as a new visitor.
To count a person once a day across restarts, pass storage. The package then keeps the day's ID and any events still waiting to be sent, and makes a new ID each day. Storing data on a device can need consent where you ship, so check before you turn it on.
import AsyncStorage from '@react-native-async-storage/async-storage'
<Analytics site="YOUR_SITE_ID" storage={AsyncStorage} />Any object with getItem and setItem works, so MMKV needs a two-line wrapper. A signed-in person counts as one visitor across days and devices either way, through their ID.
Sessions
A session ends after 30 minutes without an event, as on the web. Coming back to the app after that starts a new session, and its first screen counts as the entry. Visit length comes from when the app goes to the background, so it counts the time someone spent in the app.
Sending and offline
Events leave in one request every 5 seconds, at 20 events, or when the app goes to the background. Without a connection the newest 100 wait and go out later, dated at the time they happened. Events older than 24 hours are dropped. A request that fails and is sent again never counts twice.
Turn it off
Pass enabled={false} until a person agrees to analytics, or when they opt out. While it is off the package queues and sends nothing.
<Analytics site="YOUR_SITE_ID" enabled={consent} />