Adding Mobile Tracking to Android Projects
  • 16 Apr 2020
  • 2 Minutes to read
  • Contributors
  • Dark
    Light
  • PDF

Adding Mobile Tracking to Android Projects

  • Dark
    Light
  • PDF

Article summary

How to add permissions, import the Webtrends.xml file, and initialize tracking:

  1. Add the Webtrends-android-sdk-release.x.x.aar file to your project as a new module dependency.

  2. Add the minimum permissions to your application and ensure that an application is defined.

    Edit AndroidManifest.xml to add the INTERNET and ACCESS_NETWORK_STATE permissions. Here's an example of the XML code with an application defined and these permissions granted:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.webtrends.helloworld" 
android:versionCode="1"         android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".HelloWorld" android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
 
    <uses-permission android:name="android.permission.INTERNET"/>
 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>       
 
 </manifest>
  1. You may need additional permissions to suspend data collection based on battery life, to add location data to your collected statistics, or to perform other tracking tasks.

    To add optional persmissions for BATTERY_STATS:

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

  2. Import the Webtrends.xml file to establish operating settings for the library and provide a unique identifier for the data that you send to Webtrends. The Webtrends.xml file is included in the Library.

    1. Copy the Webtrends.xml file into the res/values/ directory. This file must be edited to include your unique DCSID. The DCSID is a unique identifier assigned to your account by Webtrends for the data that you send to our collection servers. The Webtrends.xml file is included in the Library distribution.
    2. Change the wt_dc_dcsid value to your DCSID, and change the wt_dc_timezone value to match the time zone of the Webtrends data source, expressed as an offset from GMT.
    3. If you want to run your application in debug mode, set wt_dc_debug.
      wt_dc_debug logs messages using the Log class of the android.util package, so you may view them using the LogCat utility.
  3. Initialize application tracking: This enables you to track application opens and exits and uncaught exceptions. Session starts and ends are managed, and the Webtrends queue is initialized to capture both online and offline events. If your application extends the Application base class, you can set it to use the WTApplication class instead. This allows you to streamline application-level measurement by implementing a number of Webtrends data collection methods without additional coding.

    Note

    If you would rather not extend your application for automatic tracking, then you must call the WTDataCollector.setApplication() method to initialize the Library before calling any of the event methods. Inside the main application, as the first line in the onCreate method, add the call to setApplication() as follows:

    public class HelloWorld extends Application {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            // programmatically configure the Library
            WTDataCollector.setApplication(this);                        
            ...
        }
        ...
    }
    
    • Open the .java file containing your Application implementation.

    • Import the Webtrends package by adding this import statement:

      import com.webtrends.mobile.analytics.*;

    • Modify the main class that extends “Application” by replacing “Application” with "WTApplication” as shown in the graphic:

    extendWTApplication

  4. (Optional) Extend the Activity: This enables you to streamline Activity-level measurement by implementing a number of Webtrends data collection methods without additional coding.

    Note

    If you would rather not extend your activities for automatic tracking, and you have not already called setApplication() at the Application level as described in Initialize Application Tracking, add the call to setApplication(). Inside the main application, as the first line in the onCreate method, enter:

    public class HelloWorld extends Activity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            // programmatically configure the Library
            WTDataCollector.setApplication(this);                        
            ... 
        }
        ...
    }
    
    • Open the .java file containing your Activity implementation.

    • Import the Webtrends package by adding this import statement:

      import com.webtrends.mobile.analytics.*;

  5. (Optional) Add Explicit Measurement Methods To further customize application tracking and enrich your data, you can add explicit measurement events anywhere in your application. For information about adding this instrumentation code, see "Instrumenting an Android Application."


Was this article helpful?