viakite
Dashboard

SDKs/Mobile/React Native

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

terminal
1npx expo install @viakite/react-native @react-native-async-storage/async-storage
terminal
1npm install @viakite/react-native @react-native-async-storage/async-storage
terminal
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:

App.tsx
1import { init } from "@viakite/react-native";
2
3init({ 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.

ts
1import { screen } from "@viakite/react-native";
2
3screen("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:

App.tsx
1import { NavigationContainer, useNavigationContainerRef } from "@react-navigation/native";
2import { trackNavigation } from "@viakite/react-native";
3
4export default function App() {
5 const ref = useNavigationContainerRef();
6 return (
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:

app/_layout.tsx
1import { useEffect } from "react";
2import { Stack, usePathname } from "expo-router";
3import { init, screen } from "@viakite/react-native";
4
5init({ project: "pk_live_us_…" });
6
7export default function Layout() {
8 const pathname = usePathname();
9 useEffect(() => screen(pathname), [pathname]);
10 return <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).

ts
1import { identify, reset, track } from "@viakite/react-native";
2
3track("Purchase", { revenue: 499, currency: "INR", plan: "pro" });
4
5identify("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, 429 or a 5xx stays 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

projectstringrequired
The project's public key, pk_live_<region>_… or pk_test_<region>_….
apistring
Send to another address instead of your key's region, such as a proxy, or your local API while you build (http://10.0.2.2:8080/v1/events from the Android emulator).
hoststring
The hostname screen views are recorded under. Default app. If your project allows only some hostnames (Settings), add this one, or the app's events are dropped.
flushAtnumber
Send once this many events wait. Default 20, at most 100.
flushIntervalnumber
Send what waits every this many milliseconds. Default 10000, at least 1000.
maxQueueSizenumber
The most events kept on the device while they can't be sent; the oldest go first. Default 1000.
storageStorage
Where waiting events and the user id are kept: any object with getItem, setItem and removeItem that return promises, such as a small wrapper around MMKV. Default: AsyncStorage.
debugboolean
Log every event and warning in the console, and send nothing.