Integration & Setup

  1. Home
  2. Integration & Setup
  3. Tracking Code & SDKs
  4. App Analytics tracking framework
  5. Android implementation

Android implementation

You add the tracking framework to your application as follows:

1. Copy ‚tracking-framework.aar‘ to your project’s libs folder. and add a dependency to your ‚build.gradle‘ file like this:

repositories {
        flatDir {
            dirs 'libs'
        }
    }

    dependencies {
        compile(name:'etracker-app-control-XXXXX.XXXXXX', ext:'aar')
    }

If any problems occur with like com.android.builder.packaging.DuplicateFileException, please add the following lines to your ‚build.gradle‘

packagingOptions {
            exclude "/META-INF/LICENCE"
            exclude "/META-INF/LICENSE"
            exclude "/META-INF/LICENCE.txt"
            exclude "/META-INF/LICENSE.txt"
            exclude "/META-INF/NOTICE"
            exclude "/META-INF/NOTICE.txt"
            exclude "/LICENCE"
            exclude "/LICENCE.txt"
            exclude "/NOTICE"
            exclude "NOTICE.txt"
     }

See also PackagingOptions To keep a reference to the ‚com.etracker.tracking.Tracker‘ instance around, we recommend to create a custom android.app.Application subclass like this:

import android.app.Application;
public class TrackingApplication extends Application {
    private Tracker tracker;
    @Override
    public void onCreate() {
        super.onCreate();
        tracker = Tracker.getInstance(this);
        // Example for accountKey
        String accountKey ="XXXXXX";
        // Example for Api key (API key is optional)
        String apiKey = "d41d8cd98f"; 
        tracker.startTracker(accountKey, apiKey);
    }
    @Override
    public void onTerminate() {
        super.onTerminate();
        tracker.stopTracker();
    }
}

Note: To avoid any issues with Norton Antivirus or similar products that raise any data privacy concerns, we introduced a new way to initialize the startTracker(), that does not use the Android PackageManager to resolve the app name and app version. Thus the app name and app version is recommend to be provided by using one of the following method signatures.

startTracker(String accountKey, String appName, String appVersion)
startTracker(String accountKey, String sharedSecret, String appName, String appVersion)
startTracker(String accountKey, String sharedSecret, int timeInterval, String appName, String appVersion)

2. Within the created subclass provide the accountKey and, if you want to have the requests signed, the optional API key you got from etracker resp. registered with your account. By default, pending events are sent to the server once every minute.

3. Refer to the above subclass in your AndroidManifest.xml like this:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" ...>
    ... 
    <application android:label="@string/app_name" android:icon="@drawable/icon" android:name=".TrackingApplication">
    ... 
    </application>
    ... 
</manifest>

4. As the tracker needs to access the internet, you have to add the INTERNET permission to the AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" ...>
    ...
    <uses-permission android:name="android.permission.INTERNET" />
    ...
</manifest>

5. Access the ‚global com.etracker.tracking.Tracker‘ instance in your classes like this:

public class TestActivity extends Activity {
    private Tracker tracker;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...
    tracker = Tracker.getInstance(this);
    }
}

6. Before using the tracking service, you should get the visitor’s consent of getting tracked. You can do this by integrating a code snippet in your application like the example below. Via this code snippet the visitor is asked for his consent of getting tracked and the answer is stored by the tracking service. Because of the storage the question is asked only once.

Note: Please notice that no events are tracked until the visitor’s consent has been given.

if (tracker.getUserConsent() == UserConsent.Unknown) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Do you allow anonymous tracking?");
    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialogInterface, int i) {
            tracker.setUserConsent(UserConsent.Granted);
        }
    });
    builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialogInterface, int i) {
            tracker.setUserConsent(UserConsent.Denied);
        }
    });
    builder.create().show();
}

Handle events

All events are documented in the JavaDoc that is delivered together with the library. The following events are available:

EventDescription
trackScreenViewScreen call.
track OrderSending an InApp order.
trackUserDefinedTracks a custom event.

trackScreenView and trackOrder are convenience events that simplify the use of custom events. Visitor sessions are determined automatically: Actions in an app within a time interval of 30 minutes are assigned to one visitor session. To track a screen view event, call trackScreenView(„screenName“) like this:

tracker.trackScreenView("myScreenView", "area1/area2/area3");

To track a user defined event, call trackUserDefined() like this:

tracker.trackScreenView("myScreenView", "area1/area2/area3");
tracker.trackUserDefined("categoryName", "actionName", "objectName", "42", "myScreenView","area1/area2/area3");

NEW: Please note, you can also define up to three area levels with both calls and a screenname with the userdefined event.

If you want to make sure that all pending events are sent to the server now, you can call flush():

tracker.flush();

Data to be transmitted to the server are:

  • For every chunk transmitted to the backend: device model, system version, language code, and country code
  • For each tracked event: timestamp and the string you have chosen to describe the event you want to track.

Like all other methods, this one runs asynchronously. So, if you try to send events just before terminating the application, there might not be enough time to send everything. In that case, events will be sent once the application and therefore the tracker is restarted.