Collecting data with Trackers and Webhooks

  1. Home
  2. Docs
  3. Collecting data with Trackers and Webhooks
  4. Trackers – collecting data from your own applications
  5. Java tracker
  6. Initialization

Initialization

Assuming you have completed the Java Tracker Setup for your project, you are now ready to initialize the Java Tracker.

Importing the module

Import the Java Tracker’s classes into your Java code like so:

import com.snowplowanalytics.snowplow.tracker.*; import com.snowplowanalytics.snowplow.tracker.emitter.*; import com.snowplowanalytics.snowplow.tracker.http.*;

That’s it – you are now ready to initialize a Tracker instance.

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 the following builder patterm:

Tracker.TrackerBuilder(Emitter : emitter, String : namespace, String : appId) .subject(Subject) .base64(boolean) .platform(enum DevicePlatforms) .build();

For example:

Tracker tracker = new Tracker.TrackerBuilder(emitter, "AF003", "cf") .subject(user1Subject) .base64(true) .platform(DevicePlatform.Desktop) .build();
Argument NameDescriptionRequired?Default
emitterThe Emitter object you createYesNull
namespaceThe name of the tracker instanceYesNull
appIdThe application IDYesNull
subjectThe subject that defines a userNoNull
base64Whether to enable base 64 encodingNoTrue
platformThe device the Tracker is running onNoServerSideApp

emitter

The emitter to which the tracker will send events. See Emitters for more on emitter configuration.

To attach a new Emitter to the Tracker:

Emitter newEmitter = ...; tracker.setEmitter(newEmitter);

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. Alternatively, you can also include a Subject object with the actual event; this will override the Tracker attached subject.

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.

To attach a new Subject to the Tracker:

Subject newSubject = ...; tracker.setSubject(newSubject);

namespace

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.

To set a new Namespace in the Tracker:

tracker.setNamespace("New-Namespace");

appId

The appId argument lets you set the application ID to any string.

To set a new AppId in the Tracker:

tracker.setAppId("New-App-Id");

base64

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 base64 argument.

To change whether to base64 encode:

tracker.setBase64Encoded(true || false);

Change the tracker’s platform with setPlatform

By default this value is set to DevicePlatform.ServerSideApp.

You can change the platform by calling:

tracker.setPlatform(DevicePlatform.{{ Enum value }});

For a full list of supported platforms, please see the Snowplow Tracker Protocol.