Progress will discontinue Telerik Platform on May 10th, 2018. Learn more

Monitor Integration in Android

Integrating the Android analytics monitor.

This article will describe how to integrate the Analytics Android monitor in an Android application.

Before getting started you must also have created an application so you have a product id. You will need that in the integration. Read more about how to Add Analytics to Your Application.

Adding the Android analytics monitor to your project.

Download the monitor for your project and unzip the downloaded monitor file to your machine.

Android Studio:

  • Create folder called libs in your project that is in the same hierarchical level as the build and src folders if not already created.
  • Put the analyticsmonitor.jar file inside.
  • Right-click the library and select the option "Add as library"

    Adding analitycsmonitor.jar as library into your android project

  • Make sure compile files('libs/analyticsmonitor.jar') is present in your build.gradle file:

    dependencies { 
        compile files('libs/analyticsmonitor.jar')
    }
    

Eclipse:

  • Create folder called libs in your project that is in the same hierarchical level as the build and src folders if not already created.
  • Right-click libs folder and choose Import… Then select General -> File System in the tree. Click Next, browse in the file system to find the library's parent directory (i.e.: where the monitor library resides) and click OK.

    Importing analitycsmonitor.jar from file system in Eclipse

  • Click the directory name in the left pane, then check the analyticsmonitor.jar in the right pane. Click Finish. This puts the library into your project.

    Importing analitycsmonitor.jar from file system in Eclipse - next step

  • Right-click on your project, choose Build Path -> Configure Build Path. Then click the Libraries tab, then Add JARs… button. Navigate to analyticsmonitor.jar in the libs directory and add it. Click the OK buttons in both windows.

    Configuring build path in Eclipse

The analytics monitor uses internet to send information, so you need to add INTERNET permission to your AndroidManifext.xml:

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

Starting and stopping the monitor

The monitor should start when the application begins and stop when the user is done using it. The tricky part is that Android does not provide a native way to catch when the application exits. To work around this it is enough to know when the application is visible and when it goes to the background. Stopping the monitor is best placed right before going to the background, and if the user decides to come back after that (say the application is still alive), then the monitor needs to start again. This is done by defining a custom class that implements the Application.ActivityLifecycleCallbacks. This allows you to get all the calls from all the activities and respond to the correct moments for toggling the monitor’s state:

  • Start the monitor at the beginning of the app.
  • Stop the monitor if the application loses focus (call, home button etc.)
  • Start the monitor if the user comes back.
  • Stop the monitor if the user exits the application (back button all the way).

Here is a sample implementation:

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;

public class TrackedActivityLifeCycleHandler implements Application.ActivityLifecycleCallbacks {

    private int activitiesCount = 0;

    @Override
    public void onActivityCreated(Activity activity, Bundle bundle) {
    }

    @Override
    public void onActivityStarted(Activity activity) {
    }

    @Override
    public void onActivityResumed(Activity activity) {
        if (appIsInBackground()) { // The application is either just starting or it just has been resumed after being in the background.
            Analytics.getInstance(activity.getApplicationContext()).monitor().start();
        }
        activitiesCount++;
    }

    @Override
    public void onActivityPaused(Activity activity) {
    }

    @Override
    public void onActivityStopped(Activity activity) {
        activitiesCount--;
        if (appIsInBackground()) { // The application is going to the background (User received a call or pressed the Home button etc.)
            Analytics.getInstance(activity.getApplicationContext()).monitor().stop();
        }
    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
    }

    @Override
    public void onActivityDestroyed(Activity activity) {
    }

    private boolean appIsInBackground() {
        return activitiesCount == 0;
    }
}

Now call retisterActivityLifecycleCallbacks() in onCreate() method of your application:

import android.app.Application;

public class MonitoredApp extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        registerActivityLifecycleCallbacks(new TrackedActivityLifeCycleHandler());
    }
}

Sharing a single monitor instance among all activities.

Sharing a single instance is done through a singleton:

import android.content.Context;
import eqatec.analytics.monitor.AnalyticsMonitorFactory;
import eqatec.analytics.monitor.IAnalyticsMonitor;
import eqatec.analytics.monitor.Version;

public final class Analytics {
    private final static String ANALYTICS_KEY = "123abc..."; // Use your real key here

    private static Analytics instance;
    private IAnalyticsMonitor monitor;

    private Analytics(Context context) {
        try {
            this.monitor = AnalyticsMonitorFactory.createMonitor(
                    context.getApplicationContext(), ANALYTICS_KEY,
                    new Version(context.getResources().getString(R.string.version_name)
                    ));
        } 
        catch (Throwable e) {
            e.printStackTrace();
        }
    }

    public IAnalyticsMonitor monitor() {
        return this.monitor;
    }

    public static Analytics getInstance(Context context) {
        if (instance == null){
            instance = new Analytics(context);
        }
        return instance;
    }
}

Now you can access the monitor and use its API to start tracking features:

Analytics.getInstance(this).monitor().trackFeature(String.format("%s.%s",
     this.getString(R.string.featureCategory1), 
     this.getString(R.string.feature2)));
Start a free trial Request a demo
Contact us: +1-888-365-2779
sales@telerik.com
Copyright © 2016-2017, Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.