Android tracker
The Android Tracker SDK supports Android 5 (API level 21+).
Installing
The Android Tracker SDK can be installed using Gradle.
Gradle
Add into your build.gradle
file:
dependencies {
...
// Snowplow Android Tracker
implementation 'com.snowplowanalytics:snowplow-android-tracker:5.+'
// In case 'lifecycleAutotracking' is enabled
implementation 'androidx.lifecycle-extensions:2.2.+'
...
}
Setting up
Once the tracker SDK is correctly set as a dependency in your app project you have to instrument the tracker:
In your
Application
subclass, set up the SDK as follows:- Android (Kotlin)
- Android (Java)
val tracker = Snowplow.createTracker(
applicationContext, // Android context (LocalContext.current in Compose apps)
"appTracker", // namespace
"https://snowplow-collector-url.com" // Event collector URL
)The URL path for your collector endpoint should include the protocol, "http" or "https". If not included in the URL, "https" connection will be used by default.
TrackerController tracker = Snowplow.createTracker(
getApplicationContext(), // Android context
"appTracker", // namespace
"https://snowplow-collector-url.com" // Event collector URL
);The URL path for your collector endpoint should include the protocol, "http" or "https". If not included in the URL, "https" connection will be used by default.
It creates a tracker instance which can be used to track events like this:
- Android (Kotlin)
- Android (Java)
val event = Structured("Category_example", "Action_example")
tracker.track(event)Event event = new Structured("Category_example", "Action_example");
tracker.track(event);
If you prefer to access the tracker when the reference is not directly accessible, you can use the defaultTracker
:
- Android (Kotlin)
- Android (Java)
Snowplow.defaultTracker?.track(event)
Snowplow.getDefaultTracker().track(event);
You can override the default configuration with a fine grained configuration when you create the tracker. See the Android API docs and iOS API docs for the Configuration
classes to see all the options and defaults.
- Android (Kotlin)
- Android (Java)
val networkConfig = NetworkConfiguration(
"https://snowplow-collector-url.com",
HttpMethod.POST
)
val trackerConfig = TrackerConfiguration("appId")
.base64encoding(false)
.sessionContext(true)
.platformContext(true)
.lifecycleAutotracking(true)
.screenViewAutotracking(true)
.screenContext(true)
.applicationContext(true)
.exceptionAutotracking(true)
.installAutotracking(true)
.userAnonymisation(false)
val sessionConfig = SessionConfiguration(
TimeMeasure(30, TimeUnit.SECONDS),
TimeMeasure(30, TimeUnit.SECONDS)
)
createTracker(
applicationContext,
"appTracker",
networkConfig,
trackerConfig,
sessionConfig
)
NetworkConfiguration networkConfig = new NetworkConfiguration(
"https://snowplow-collector-url.com",
HttpMethod.POST
);
TrackerConfiguration trackerConfig = new TrackerConfiguration("appId")
.base64encoding(false)
.sessionContext(true)
.platformContext(true)
.lifecycleAutotracking(true)
.screenViewAutotracking(true)
.screenContext(true)
.applicationContext(true)
.exceptionAutotracking(true)
.installAutotracking(true)
.userAnonymisation(false);
SessionConfiguration sessionConfig = new SessionConfiguration(
new TimeMeasure(30, TimeUnit.SECONDS),
new TimeMeasure(30, TimeUnit.SECONDS)
);
Snowplow.createTracker(getApplicationContext(),
"appTracker",
networkConfig,
trackerConfig,
sessionConfig
);
The trackers created with the above method are configured "locally" only. To create a tracker where the configuration can be updated through downloaded files, read this page about remote configuration.
The Android tracker Github repository includes demo apps in Java, Kotlin, and Kotlin with Jetpack Compose. They are provided as simple reference apps to help you set up the tracker.