Android (1.0.0)
Installation
In order to add the tracker to your project, you must add it as a dependency.
Gradle
If you use Gradle, here's how to add the tracker to your project.
Add into your build.gradle file:
dependencies {
...
// Snowplow Android Tracker
compile 'com.snowplowanalytics:snowplow-android-tracker:1.0.0@aar'
}
This will install version 1.0.0 of the Android tracker. If you would like to ensure that all bug fixes and patches for version 1.0.0 are installed, simply change 1.0.0 into 1.0.+.
dependencies {
...
// Snowplow Android Tracker
compile 'com.snowplowanalytics:snowplow-android-tracker:1.0.+@aar'
}
Note: no breaking changes will occur in the '1.0.x' space.
A complete Gradle file may look like this:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
implementation 'android.arch.lifecycle:extensions:$project.archLifecycleVersion'
implementation 'com.android.support:support-v4:28.0.0'
implementation 'com.android.support:appcompat-v7:28.0.0'
// Optional Google Analytics Library
// - Required to get the IDFA Code
implementation 'com.google.android.gms:play-services-analytics:16.0.1'
// Required Dependency for the Tracker
implementation 'com.squareup.okhttp3:okhttp:3.4.1'
// Tracker Import
implementation 'com.snowplowanalytics:snowplow-android-tracker:1.0.0@aar'
}
Demo apps
With the tracker we've included a demo app that shows an example of how to integrate the tracker.
For general testing, Snowplow Mini can be used as an easily deployable collector with a live web interface for viewing received events.
The app can be run in the Android Studio emulator or on an actual device.
Simply enter the endpoint of the collector in the app's interface once it's launched and press "send events"!
Quick Start
Add the following snippet to a file (e.g. SnowplowTracker.java):
import com.snowplowanalytics.snowplow.tracker.*;
import android.content.Context;
public class SnowplowTrackerBuilder {
public static Tracker getTracker(Activity activity, Context context) {
Emitter emitter = getEmitter(context);
Subject subject = getSubject(context); // Optional
return Tracker.init(new Tracker.TrackerBuilder(emitter, "your-namespace", "your-appid", context)
.subject(subject) // Optional
.build()
).setLifecycleHandler(activity);
}
private static Emitter getEmitter(Context context) {
return new Emitter.EmitterBuilder("notarealuri.fake.io", context)
.build();
}
private static Subject getSubject(Context context) {
return new Subject.SubjectBuilder()
.context(context)
.build();
}
}
Permissions
To send the events, you need to update your AndroidManifest.xml with the following permission:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
If you want to send location information with each event you will need to add the following permissions to your AndroidManifest.xml:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Send events
Now you can try to track events:
Tracker tracker = SnowplowTrackerBuilder.getTracker(activity, context);
tracker.track(ScreenView.builder().name("screenName").id("screenId").build());
Tracker structure
Here we'll explain the purpose of the classes provided in the tracker.
Emitter
Responsible for all the storage, networking and scheduling required to ensure events are sent to a collector.
Details like the collector endpoint and sending timeout lengths are set here.
Tracker
Tracker is the class where you can find the methods available for tracking events. This is also where all parts of the tracker are brought together, i.e. within Tracker you must set the associated emitter, subject, etc.
Payload
Payload is simply a key-value store used for constructing events.
SelfDescribingJson
SelfDescribingJson is the class used for making self-describing JSONs (SDJs).
An SDJ has a schema field that holds a URI (string) that identifies the structure of the data nested in the data field.
All events sent to the collector are self-describing JSONs.
When sending your own custom events, you will want to create a SelfDescribingJson object given two arguments: the schema, and a Map<String, String> or Map<String, Object> that holds the data you'd like to track.
Subject
A "subject" represents an individual user that is being tracked. It is used to track data that persists with a user like timezone, user ID, platform, etc.
Event
This is where all events are found, the available classes are:
- PageView
- Structured
- Unstructured
- ScreenView
- ConsentWithdrawn
- ConsentGranted
- ConsentDocument
- Timing
- EcommTransaction
- EcommTransactionItem
- NotificationContent
- PushNotification
Events are sent by providing them as arguments to the tracking methods found in Tracker.
RequestCallback
This is a class that defines callbacks that are called when an emitter either fails or succeeds to send requests.
Tracking basic methods
Creating an emitter
Every tracker must have an emitter in order to send events.
Note: the URL endpoint is the only required parameter.
Here's an example that creates an emitter:
Emitter e2 = new Emitter
.EmitterBuilder("com.collector.acme", Context context) // Required
.method(HttpMethod.GET) // Optional - Defines how we send the request
.option(BufferOption.Single) // Optional - Defines how many events we bundle in a POST
.security(RequestSecurity.HTTPS) // Optional - Defines what protocol used to send events
.tls(TLSVersion.TLSv1_2) // Optional - Defines what TLS versions are used for the request
.callback(new EmitterCallback() {...})
.build();