UserDefaults, queues events while the network is down, and sends them to https://in.crispstats.com/e. It does not read the advertising identifier.
Add the file
Download the file:CrispStats.swift into your Xcode project and tick your app target. The file is part of your app’s own module, so there is nothing to import. It needs iOS 15 or later and works in Swift 5 and Swift 6 language modes.
Count screens
Create one client and keep it for the life of the app. A global constant is fine, and every method can be called from any thread.cs_pk_xxxxxxxx with the site’s public key from Settings, Tracking code, and com.acme.app with the bundle id. appVersion is the marketing version. appBuild is the build number and can be left out. The iOS version is read for you.
Call screen on each screen the visitor sees:
revenue takes minor units: 4900 with USD is $49.00. A negative amount is ignored.
screen sends the time spent on the previous screen before it counts the new one. Time is counted from the moment you call screen, and it stops while the app is in the background. When the app comes back after 30 minutes or more, the current screen is counted again, because a new visit has started. All of this runs from the app’s background and foreground notifications.
A screen name is one to eight segments of letters, numbers, spaces, ., _ and -, separated by /. Each segment starts with a letter or a number. Settings/Account is stored as the page /Settings/Account. A name with .., a question mark or an accented letter is rejected and nothing is sent.
SwiftUI
Callscreen when a screen appears:
Campaigns from a deep link
When the app opens from a link such asacme://promo?utm_source=newsletter&utm_campaign=spring, pass the link’s campaign to the screen it opens. CrispStats.campaign(from:) reads the query for you.
_ up to 32 characters. Values are trimmed and keep up to 200 characters. At most 10 pairs are sent. The referrer can be up to 2,000 characters.
Tablets
An iPad is counted as a tablet and an iPhone as a phone. To set it yourself, passdevice:
Opt out
optOut drops anything still queued and sends nothing until optIn. The choice is kept across launches. If the visitor opts out before the first event, no install id is created until they opt in.
Errors
Delivery
Each event is sent on its own. Up to 100 events wait on the device while the network is down, and the oldest is dropped first. The queue is sent when the app starts, when it returns to the foreground and after each new event. After a failure the SDK waits 1 second, then 2, then 4, up to a minute between attempts. When the app moves to the background it asks iOS for a few seconds to finish sending. If the site key does not start withcs_pk_, the app id is not a valid bundle id, or the version is empty, the client sends nothing and prints the reason once in a debug build. It never crashes your app.
What gets stored
The screen is a pageview. The browser breakdown shows iOS app, the OS shows iOS, and the device shows mobile or tablet. The language is sent as a tag such asen-IN, with the time zone. The app version, build, OS version and SDK name are properties on the event. The install id itself is not stored: the collector hashes it first. See mobile apps for the rest of the mapping, including revenue from the App Store.