Android (0.2.0) / Java (0.6.0)
1. Overview
The Snowplow Java Tracker allows you to track Snowplow events from your Java-based desktop and server apps, servlets and games. It supports JDK6+.
The Snowplow Android Tracker allows you to track Snowplow events from your Android applications and games. It supports applications using the Android SDK 11 and above.
The tracker should be straightforward to use if you are comfortable with Java development; its API is modelled after Snowplow's Python Tracker so any prior experience with that tracker is helpful but not necessary. If you haven't already, have a look at the Java Tracker Setup or Android Tracker Setup guide before continuing.
1.1 Android specific
The Android Tracker is based off of the Java Tracker core library which was also used by the Java Tracker up to version 0.6.0. Hence, they both very similar in features and with a few differences in them. For sections of this documentation which only apply to Android, we will flag with a sub-section, Android Only.
2 Initialization
Assuming you have completed the Android Tracker Setup or Java Tracker Setup for your project, you are now ready to initialize the Android/Java Tracker.
2.1 Importing the module
Import the Java Tracker's classes into your Java code like so:
import com.snowplowanalytics.snowplow.tracker.*;
That's it - you are now ready to initialize a Tracker instance.
2.2 Understanding the module structure for Android
As we've mentioned in the setup guides, the Android Tracker is still based off a Java core library (which was part of the Java Tracker until version 0.6.0 inclusive). This means that for the Android Tracker you'll find a set the core libraries in com.snowplowanalytics.snowplow.tracker.core.*. If you're using the Android Tracker you would use the classes in com.snowplowanalytics.snowplow.tracker.android.*, since it contains class overrides specific to Android. If the class doesn't exist in that module (mostly enums), you can use the ones in the core package instead.
2.3 Creating a Tracker
To instantiate a tracker in your code (can be global or local to the process being tracked) simply instantiate the Tracker interface with one of the following:
Tracker(Emitter emitter, String namespace, String appId)
Tracker(Emitter emitter, Subject subject, String namespace, String appId)
Tracker(Emitter emitter, String namespace, String appId, boolean base64Encoded)
Tracker(Emitter emitter, Subject subject, String namespace, String appId, boolean base64Encoded)
For example:
Tracker t1 = new Tracker(emitter, user1Subject, "AF003", "cf", true);
Tracker t2 = new Tracker(emitter, "AF003", "cf");
| Argument Name | Description | Required? |
|---|---|---|
emitter | The Emitter object you create | Yes |
subject | The subject that defines a user | No |
namespace | The name of the tracker instance | Yes |
appId | The application ID | Yes |
base64Encoded | Whether to enable base 64 encoding | No (Default true) |
2.3.1 emitter
The emitter to which the tracker will send events. See Emitters for more on emitter configuration.
2.3.2 subject
The user which the Tracker will track. This should be an instance of the Subject class. You don't need to set this during Tracker construction; you can use the Tracker.setSubject method afterwards. In fact, you don't need to create a subject at all. If you don't, though, your events won't contain user-specific data such as timezone and language.
2.3.3 namespace
If provided, the namespace argument will be attached to every event fired by the new tracker. This allows you to later identify which tracker fired which event if you have multiple trackers running.
2.3.4 appId
The appId argument lets you set the application ID to any string.
2.3.5 base64Encoded
By default, unstructured events and custom contexts are encoded into Base64 to ensure that no data is lost or corrupted. You can turn encoding on or off using the Boolean base64Encoded argument.
2.3.6 Change the tracker's platform with setPlatform
You can change the platform by calling:
tracker.setPlatform("cnsl");
For a full list of supported platforms, please see the Snowplow Tracker Protocol.
3. Adding extra data: the Subject class
You may have additional information about your application's environment, current user and so on, which you want to send to Snowplow with each event.
The Subject class has a set of set...() methods to attach extra data relating to the user to all tracked events:
Here are some examples:
s1.setUserID("Kevin Gleason");
s1.setLanguage("en-gb");
s1.setScreenResolution(1920, 1080);
After that, you can add your Subject to your Tracker like so:
Tracker(emitter, s1, namespace, appId);
// OR
t1.setSubject(s1);
3.2 Set user ID with setUserId
You can set the user ID to any string:
s1.setUserId( "{{USER ID}}" )
Example:
s1.setUserId("alexd")
3.3 Set screen resolution with setScreenResolution
If your Java code has access to the device's screen resolution, then you can pass this in to Snowplow too:
t1.setScreenResolution( {{WIDTH}}, {{HEIGHT}} )
Both numbers should be positive integers; note the order is width followed by height. Example:
t1.setScreenResolution(1366, 768)
3.4 Set viewport dimensions with setViewport
If your Java code has access to the viewport dimensions, then you can pass this in to Snowplow too:
s.setViewport( {{WIDTH}}, {{HEIGHT}} )
Both numbers should be positive integers; note the order is width followed by height. Example:
s.setViewport(300, 200)
3.5 Set color depth with setColorDepth
If your Java code has access to the bit depth of the device's color palette for displaying images, then you can pass this in to Snowplow too:
s.setColorDepth( {{BITS PER PIXEL}} )
The number should be a positive integer, in bits per pixel. Example:
s.setColorDepth(32)
3.6 Set timezone with setTimezone
This method lets you pass a user's timezone in to Snowplow:
s.setTimezone( {{TIMEZONE}} )
The timezone should be a string:
s.setTimezone("Europe/London")
3.7 Set the language with setLanguage
This method lets you pass a user's language in to Snowplow:
s.setLanguage( {{LANGUAGE}} )
The language should be a string:
s.setLanguage('en')
4. Tracking specific events
Snowplow has been built to enable you to track a wide range of events that occur when users interact with your websites and apps. We are constantly growing the range of functions available in order to capture that data more richly.
Tracking methods supported by the Java Tracker at a glance:
| Function | *Description |
|---|---|
trackScreenView() | Track the user viewing a screen within the application |
trackPageView() | Track and record views of web pages. |
trackEcommerceTransaction() | Track an ecommerce transaction and its items |
trackStructuredEvent() | Track a Snowplow custom structured event |
trackUnstructuredEvent() | Track a Snowplow custom unstructured event |
4.1 Common
All events are tracked with specific methods on the tracker instance, of the form trackXXX(), where XXX