React Native
@viakite/react-native: iOS and Android, Expo and bare
@viakite/react-native brings Viakite Analytics to iOS and Android apps built with React Native, with Expo or without. It sends screens as page views, custom events and your signed-in users, and keeps events on the device until they are sent, so an app that is offline or closed too soon still sends them later.
Install
1npx expo install @viakite/react-native @react-native-async-storage/async-storage
1npm install @viakite/react-native @react-native-async-storage/async-storage
1yarn add @viakite/react-native @react-native-async-storage/async-storage
It needs React Native 0.70 or later. It has no native code of its own; it keeps waiting events in AsyncStorage, which needs a native rebuild in a bare app (npx pod-install on iOS).
Start it
You need your project's public key, which starts with pk_live_: the Install page in the dashboard (Analytics → Install) shows it. Call init() once, as the app starts, outside any component:
1import { init } from "@viakite/react-native";23init({ project: "pk_live_us_…" });
A missing key, or a key that isn't a public key, turns the SDK off without an error; with debug: true it says why in the console. A second init() does nothing.
Screens
Each screen counts as a page view. screen("Settings") is a view of the page /Settings on the hostname app (set host to use your own), so screens show in Pages next to your website's pages.
1import { screen } from "@viakite/react-native";23screen("Settings");
React Navigation
trackNavigation() sends the first route and every route change after it, named after the route. Call it from the container's onReady:
1import { NavigationContainer, useNavigationContainerRef } from "@react-navigation/native";2import { trackNavigation } from "@viakite/react-native";34export default function App() {5const ref = useNavigationContainerRef();6return (7<NavigationContainer ref={ref} onReady={() => trackNavigation(ref)}>8{/* your navigators */}9</NavigationContainer>10);11}
It returns a function that stops it.
Expo Router
Send the path when it changes, in the root layout. A path that starts with / is kept as it is:
1import { useEffect } from "react";2import { Stack, usePathname } from "expo-router";3import { init, screen } from "@viakite/react-native";45init({ project: "pk_live_us_…" });67export default function Layout() {8const pathname = usePathname();9useEffect(() => screen(pathname), [pathname]);10return <Stack />;11}
Events and users
track(), identify() and reset() work like the web SDK's, with the same limits: an event name of up to 120 characters and up to 20 flat properties, with revenue and currency for a sale (Custom events).
1import { identify, reset, track } from "@viakite/react-native";23track("Purchase", { revenue: 499, currency: "INR", plan: "pro" });45identify("user_8231", { plan: "pro" });6reset(); // on sign-out
An event belongs to the last screen sent. The user id from identify() is kept on the device, so it lasts across app starts until reset(). Never use an e-mail address or phone number as the id; one that looks like either is dropped. More in Identify users.
Calls made before init() are kept and sent once it runs.
Sending, offline and retries
- Events wait on the device and go in batches: when 20 wait, every 10 seconds, when the app moves to the background or comes back, and when you call
flush(). - A batch that fails with a network error,
429or a5xxstays on the device and is tried again after 1, 5 and 15 seconds, then every minute. Every event keeps its id, so a retry is stored once. Other answers drop the batch. - Waiting events survive the app being closed and are sent on the next start. At most 1,000 wait (the oldest go first), and an event older than a day is dropped, because the server places an event at most a day in the past.
await flush() sends what waits at once; it never rejects.
The visitor and the device
Like the web SDK, the app sets no id of its own on the device to count visitors: the server counts a visitor from the network address and the device's description, hashed with a salt that changes every day, then dropped. The SDK describes the device the way ingest reads it: iOS or Android and its version, iPhone or iPad, the Android model, the screen size and the language. In Devices, the Browser card shows the app as ReactNative.
Options
pk_live_<region>_… or pk_test_<region>_….http://10.0.2.2:8080/v1/events from the Android emulator).app. If your project allows only some hostnames (Settings), add this one, or the app's events are dropped.20, at most 100.10000, at least 1000.1000.getItem, setItem and removeItem that return promises, such as a small wrapper around MMKV. Default: AsyncStorage.