CrispStats.kt. It uses only the Android SDK and the Java standard library, so there is no extra dependency. It works from Android 5.0 (API 21). It keeps a random install id on the device, queues events while the network is down, and sends them to https://in.crispstats.com/e. It does not read the advertising id.
Add the file
From the app module, download the file into the packagecom.crispstats.sdk:
AndroidManifest.xml:
Create the client
Create one client in yourApplication class and keep it for the life of the app.
AndroidManifest.xml with android:name=".AcmeApp" on the <application> element.
Replace cs_pk_xxxxxxxx with the site’s public key from Settings, Tracking code, and com.acme.app with the application id. appVersion is the version name. appBuild is the version code and can be left out. osVersion defaults to Build.VERSION.RELEASE, so you can leave it out too.
If the key does not start with cs_pk_, the application id has characters other than letters, digits, ., _ and -, or the version is empty, the client sends nothing and says why once in Logcat in a debug build. It never throws.
Count screens
Callscreen when the visible destination changes:
/. Each segment starts with a letter or a digit and then uses letters, digits, spaces, ., _ and -, up to 80 characters. The whole name is at most 200 characters. Settings/Account is stored as the page /Settings/Account. A name that breaks these rules sends nothing.
screen sends the time spent on the previous screen before it counts the new one. While at least one activity is started, the current screen’s timer runs. When the last activity stops, the timer pauses and the time so far is sent. When the app comes back within 30 minutes, the same screen keeps adding up. After 30 minutes or more in the background, the screen is counted again, because a new session has started.
Events, users and revenue
revenue takes minor units: 4900 with USD is $49.00. A negative amount is ignored.
Campaigns from a deep link
Pass the campaign tags of the link that opened the app to the firstscreen call. CrispStats.campaignFrom reads them from the link:
acme://pricing?utm_source=newsletter&utm_medium=email sets the channel to email. You can also pass the pairs yourself, and a referrer URL:
_, up to 32 characters. Values are trimmed and kept when they are 1 to 200 characters. At most 10 pairs are sent. The referrer is sent when it is 1 to 2,000 characters. Both apply to that one screen view only.
Tablets
The device is detected from the smallest screen width: 600 dp or more counts as a tablet. To set it yourself, passdevice in onCreate:
Opt out
optOut drops anything still queued and sends nothing until optIn. The choice is kept across launches.
Errors
Offline and retries
Each event is one request. Events wait in a queue of up to 100, kept inSharedPreferences, and the oldest is dropped when it is full. The SDK sends the queue when the client is created, when the app returns to the foreground and after each new event. After a failure it tries again after 1 second, then 2, 4 and so on, up to once a minute. A request that takes longer than 10 seconds counts as a failure. Storage and network run on one background thread, so no call blocks the main thread.