---
title: "Adding Mobile Tracking to Android Projects "
slug: "adding-mobile-tracking-to-android-projects"
updated: 2020-04-16T20:10:04Z
published: 2020-04-16T20:10:05Z
canonical: "onpremises.webtrends.help/adding-mobile-tracking-to-android-projects"
---

> ## Documentation Index
> Fetch the complete documentation index at: https://onpremises.webtrends.help/llms.txt
> Use this file to discover all available pages before exploring further.

# Adding Mobile Tracking to Android Projects 

:::(Internal) 
I changed #2 below a bit to incorporate some info on the "Getting Started with..." page in the original. So, the numbering is off by 1. 
Also, on the "Getting Started with.." page it mentioned at a high-level to use a method called `WTDataCollector.getInstance().eventMethod()`and to see (this article below) for details. However, I do not see any details on using that method. And I didn't know how to add it.

-also, as usual, double-check how I labeled the code blocks. 

sw
:::



**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
<?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>
```



3. 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" />`

4. 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.

5. 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.
   
    :::(Info) (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: 

    ```java
    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](https://cdn.document360.io/0afe9842-6601-4812-b9e2-2726979f2233/Images/Documentation/extendWTApplication.png){height="" width=""}
    

6. (Optional) Extend the Activity: This enables you to streamline Activity-level measurement by implementing a number of Webtrends data collection methods without additional coding.
 
     :::(Info) (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: 


    ```java
    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.*;`

7. (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."


:::(Internal) (Related Article, above)
link to the article that's in this subcategory

sw
:::
