Android Integration - OXYGEN-MARKET/oxygen-market.github.io GitHub Wiki

This page refers to integration samples for Android Tracker v0.6.0. This assumes you have already successfully gone through the Setup Guide for setting up all the dependencies for the tracker.

The following example classes are using the bare minimum of settings for building a Tracker. It is encouraged to flesh out the options for the Tracker and Emitter builders.

For previous versions:

Contents

1. Init Tracker

You will need to have imported the following library into your project:

dependencies {
    ...
    compile 'com.snowplowanalytics:snowplow-android-tracker:0.6.0@aar'
}

Example class to return an Android Tracker:

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();
    }
}

Back to top

2. Tracking Events

Once you have successfully built your Tracker object you can track events with calls like the following:

Tracker tracker = getTracker(activity, context);
tracker.track(ScreenView.builder().name("screenName").id("screenId").build());

For an outline of all available tracking combinations have a look here.

Back to top

3. Application Focus

The Tracker Session object can be tuned to timeout in foreground and background scenarios, but you are required to tell us when your application is in these states. Unfortunately it is not possible to do so from a library standpoint.

For Android APIs lower than 14 the current implementation we are using is to override the onPause() and onResume() functions of an application activity to notify the session when we change states.

@Override
protected void onPause() {
    super.onPause();
    tracker.getSession().setIsBackground(true);
}

@Override
protected void onResume() {
    super.onResume();
    tracker.getSession().setIsBackground(false);
}

For Android APIs 14+ we utilise a Lifecycle Handler class to manage this for us. Simply setup the handler with your application activity like so:

Tracker.instance().setLifecycleHandler({{ APPLICATION_ACTIVITY }})

Back to top

⚠️ **GitHub.com Fallback** ⚠️