Mobile 110: Getting started with Android & Android TV - 5.x

Mobile 110: Getting started with Android & Android TV - 5.x

by Community Manager on ‎09-15-2016 10:51 AM - edited on ‎03-20-2017 09:41 AM by (2,465 Views)

NOTE: See the newer Adding Tealium to Your Android App article. 

NOTE: This article covers Android Mobile and Android TV Integrations

 

To integrate the Tealium Android Library:

  1. Library Setup
  2. Initialize
  3. Track Views
  4. Track Events
  5. Cookie Management
  6. View Samples & Documentation

0. Requirements

- IDE: Android Studio

- OS Minimum: Android API Level 9, NDK 5

 

1. Library Setup:

1a. Option 1: Maven Dependency

In <PROJECT_ROOT>/build.gradle, add maven:

allprojects {
repositories {
jcenter()

maven {
url "http://maven.tealiumiq.com/android/releases/"
}

}
}

In<PROJECT_ROOT>/<MODULE>/build.gradle, add dependency:

dependencies {
compile 'com.tealium:library:5.2.0'
}

 

1b. Option 2: Manually add dependency

In <PROJECT_ROOT>/build.gradle add flatDir:

allprojects {
    repositories {
        jcenter()
        flatDir {
            dirs 'libs'
        }
    }
}

Add tealium-5.0.x.aar to <PROJECT_ROOT>/<MODULE>/libs

Add reference to <PROJECT_ROOT>/<MODULE>/build.gradle add the dependency 

dependencies {
    compile(name:'tealium-5.2.0', ext:'aar')

2. Initialize: 

In the Application sub-class' onCreate method:

Tealium.Config config = Tealium.Config.create(application, "tealiummobile", "demo", "dev");
Tealium.createInstance("tealium_main", config);

3. Track views:

Track any views the app presents such as the MainActivity or dialogs.

Map<String, Object> data = new HashMap<>(1);
data.put("activity", this.getClass().getName());

Tealium.getInstance("tealium_main")
.trackView("Settings", data);

4. Track events:

Track any events that occur in the app such as button clicks or lifecycle.

Map<String, Object> data = new HashMap<>(2);
data.put("logged_in", false);
data.put("visitor_status", new String[]{"new_user", "unregistered"});

Tealium instance = Tealium.getInstance("tealium_main");
if (instance != null) {
instance.trackEvent("initialization", data); 
}

NOTE: 

v5.1.0 and above: CookieManger is enabled by default

* v5.0.4 and below: CookieManager is disabled by default. Enablement is required if using Tag Management

 

If using Tag Management, add the following after initialization statement:

config.getEventListeners().add(createCookieEnablerListener());

private static WebViewCreatedListener createCookieEnablerListener() {
return new WebViewCreatedListener() {
@Override
public void onWebViewCreated(WebView webView) {
final CookieManager mgr = CookieManager.getInstance();

// Accept all cookies
mgr.setAcceptCookie(true);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mgr.setAcceptThirdPartyCookies(webView, true);
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) {
CookieManager.setAcceptFileSchemeCookies(true);
}

Log.d(TAG, "WebView " + webView + " created and cookies enabled.");
}
};
}

The above code enables the Tag Management Webview to:

6. View Samples & Documentation 

Samples & API