Install
Mobile apps
Screens, events and revenue from iOS, Android, React Native, Expo, Flutter, Capacitor and .NET apps.
An app is added like a website, with its app id in place of a domain: the iOS bundle id or the Android package name, for example com.example.app. Choose Mobile app when you add it. Each screen the app shows is counted like a page, and goals, users and revenue work the same way as on a website.
Add it to your app
Each SDK is one file with no other packages to install, except where noted. Download it, add it to your project and start it when the app launches.
Add EarnedStats.swift to your app target (it works on iPhone, iPad, Mac, Apple TV, Apple Watch and Vision Pro). Start it when the app launches, then send screens from your views.
@main
struct MyApp: App {
init() {
EarnedStats.configure(siteId: "YOUR_SITE_ID")
}
var body: some Scene {
WindowGroup {
ContentView()
.trackScreen("Home")
.onOpenURL { EarnedStats.open(url: $0) }
}
}
}
// Anywhere: a goal
EarnedStats.track("signup", ["plan": "pro"])Add EarnedStats.kt to your app module (package com.earnedstats) and start it in your Application class. It works from Kotlin and Java, with Views or Jetpack Compose.
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
EarnedStats.configure(this, "YOUR_SITE_ID")
}
}
// Compose: one screen view per destination
LaunchedEffect(route) { EarnedStats.screen(route) }
// Anywhere: a goal
EarnedStats.track("signup", mapOf("plan" to "pro"))Put both files in a folder such as src/earnedstats. Install @react-native-async-storage/async-storage so one install stays one visitor, then start it at the top of App.tsx.
import AsyncStorage from '@react-native-async-storage/async-storage';
import EarnedStats, { trackNavigation } from './src/earnedstats';
EarnedStats.configure({
siteId: 'YOUR_SITE_ID',
appId: 'com.example.app',
appVersion: '1.0.0',
storage: AsyncStorage,
});
// React Navigation: a screen view for every route
<NavigationContainer ref={ref} onReady={() => trackNavigation(ref)}>
// Anywhere: a goal
EarnedStats.track('signup', { plan: 'pro' });Put both files in a folder such as lib/earnedstats and run npx expo install @react-native-async-storage/async-storage. Works in Expo Go and in development builds.
import AsyncStorage from '@react-native-async-storage/async-storage';
import Constants from 'expo-constants';
import { usePathname } from 'expo-router';
import EarnedStats, { useTrackPathname } from '../lib/earnedstats';
EarnedStats.configure({
siteId: 'YOUR_SITE_ID',
appId: 'com.example.app',
appVersion: Constants.expoConfig?.version,
storage: AsyncStorage,
});
export default function RootLayout() {
useTrackPathname(usePathname());
return <Stack />;
}Add earnedstats.dart to lib/ and run flutter pub add shared_preferences. Start it in main() and add the observer to your app for automatic screen views.
import 'earnedstats.dart';
Future<void> main() async {
await EarnedStats.configure(
siteId: 'YOUR_SITE_ID',
appId: 'com.example.app',
appVersion: '1.0.0',
);
runApp(MaterialApp(
navigatorObservers: [EarnedStatsObserver()],
home: const HomePage(),
));
}
// Anywhere: a goal
EarnedStats.track('signup', {'plan': 'pro'});Apps that show web pages inside the app use the website script with data-app set to the app id. Put it in index.html, inside <head>. Route changes are counted by themselves.
<script defer data-website-id="YOUR_SITE_ID"
data-app="com.example.app" data-app-version="1.0.0"
src="https://earnedstats.com/js/script.js"></script>Add EarnedStats.cs to the project and start it when the app launches. Call Background() and Foreground() from the app's lifecycle events. Unity needs System.Text.Json in the project.
EarnedStats.Configure("YOUR_SITE_ID", "com.example.app",
appVersion: AppInfo.VersionString,
dataDirectory: FileSystem.AppDataDirectory);
EarnedStats.Screen("Home");
EarnedStats.Track("signup");
// Unity: Configure(..., appVersion: Application.version,
// dataDirectory: Application.persistentDataPath)Anything that can send an HTTP request can be tracked: send one POST per screen view or event. Keep a random install id in the app and send it with every event.
POST https://earnedstats.com/api/event
Content-Type: application/json
{
"w": "YOUR_SITE_ID",
"t": "pageview",
"u": "app://com.example.app/Home",
"m": { "i": "<install id>", "os": "android", "osv": "15",
"dev": "mobile", "av": "1.0.0" }
}
// t: pageview (a screen), goal (with "n": name, "p": properties),
// identify ("p": { "user_id", "email", "name" }), ping ("d": ms in use)
// "ago": ms since it happened, for events sent later (up to 48 hours)Use an AI coding tool? Open the app's Setup page and choose Let my coding AI do it for a prompt with your real site id and app id.
Screens
Call screen("Home") when a screen appears. The name becomes the page path, so Home shows as /Home and settings/billing as /settings/billing. The SwiftUI modifier .trackScreen, the Flutter EarnedStatsObserver and the React Navigation and Expo Router helpers send them for you.
Goals and users
| Call | What it does |
|---|---|
track(name, properties) | A goal, with up to 10 properties. It shows on the Goals page next to your website goals. |
identify(userId, email, name) | Ties this install to your own user, so their visits and payments show on one profile. |
open(url) | Pass the deep link or universal link that opened the app: its utm_ and ref parameters credit the next screen to that campaign. |
visitorId | This install's visitor id, for crediting purchases (below). |
enabled | Turn sending off and on, for an analytics opt out inside the app. |
Campaigns and installs
Put utm_source, utm_medium and utm_campaign on your deep links and pass the link to open(url). On Android, pass the Play Store install referrer to EarnedStats.installReferrer(referrer) once, to credit installs to the campaign that brought them.
Revenue from in-app purchases
Connect RevenueCat under Settings, Revenue, then give RevenueCat the visitor id once it is known:
EarnedStats.onVisitorId { id in
Purchases.shared.attribution.setAttributes(["es_visitor_id": id])
}EarnedStats.onVisitorId { id ->
Purchases.sharedInstance.setAttributes(mapOf("es_visitor_id" to id))
}Each purchase is then credited to the install, its first screen and the campaign that brought it. Payments through Stripe or another web checkout work the same way: send es_visitor_id with the checkout.
One site for the website and the app
To see a website and its app together, add the app id under Settings, General, Additional hostnames of the website's site. When the iOS and Android ids differ, add the second id to the app's site the same way.
How installs are counted
- The SDK makes one random id the first time the app opens and keeps it inside the app. One install is one visitor for as long as the app is installed.
- No advertising id, no device identifier and no tracking permission prompt. Nothing is shared with other apps.
- Visits end after 30 minutes without activity; time in the foreground is counted as visit length.
- Events made while offline are kept, up to 500, and sent when the phone is back online. Events older than 48 hours are dropped.
- App versions show in the Browsers list (
App 2.4.1), the operating system and phone or tablet in Devices, and the country from the network like on a website.
App Store privacy details: declare Product Interaction and, if you call identify, User ID, as analytics data not used for tracking.