Monday, April 4, 2016

How to Integrate Google Analytics SDK Localytics and Mix Panel to Your Android Application


In this tutorial I will explain how to add google analytics, localytics, mix panle in your Android application using Android Studio.

let's start from Google Analytics:

If you have tried adding google analytics from developers.google and getting Gradle version conflict exceptions and other exceptions then this post will be useful for you. It will follow same steps at official Google documentation but with different versions so you don't get errors.

Step 1: Add INTERNET AND ACCESS_NETWORK_STATE permission in manifest file.
Step 2Add the dependency to your project-level build.gradle:


dependencies {
    classpath 'com.android.tools.build:gradle:1.3.0'

    classpath 'com.google.gms:google-services:1.3.0-beta1'
}

Step 3:  Add the plugin to your app-level build.gradle file

  apply plugin: 'com.google.gms.google-services'

Step 4: Now add below dependency in app-level build,gradle file for google analytics

    compile 'com.google.android.gms:play-services-analytics:8.3.0'

Step 5: Download configuration file from your account. And add it in your project "app" folder.


Thats it :)

Now I will guide how to initialize Google Analytics so you get events at your account when user interact with your application.



Create a new class name GlobalClass and add it in your manifest file in name tag under application.
e.g. <application android:name=".GlobalClass"/>

Your GlobalClass should look like below.


public class GlobalClass extends Application {

    /*.............................................................
    * This class contains Localytics, Mix Panel, and Crash Lytics, and Google Analytics .

    * .............................................................    */

    private Tracker mTracker;
    synchronized public Tracker getDefaultTracker() {
        if (mTracker == null) {
            GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
            mTracker = analytics.newTracker(R.xml.global_tracker);
        }
        return mTracker;
    }

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



I will guide you how to add screen tracking for google analytics, for localytics and for mix panel at the END :)

Reference: https://developers.google.com/analytics/devguides/collection/android/v4/#activity-or-fragment

Now we will add Localytics :

Step 1: Download Localytics jar file and add it in libs folder inside app folder of your project.

Step 2:  Add your application ID in manifest file like below:
<meta-data    android:name="LOCALYTICS_APP_KEY"
android:value="54beea42876aa84xxxx1fbb-ec8xxx2c-4b1a-11e5-b7b7-xx13x62afxxx" />

Don't forget to change value(app-id) according to yours.

Step 3: Add below code in onCreate() method of GlobalClass.
registerActivityLifecycleCallbacks(
        new LocalyticsActivityLifecycleCallbacks(this));

Thats it :) Localytics has been integrated in your application :) enjoy :)

Now we will add MixPanel API

Step 1: Add dependency in build.gradle file. This will install MixPanel library for your project.

compile "com.mixpanel.android:mixpanel-android:4.6.4"

Step 2: Initialize library in GlobalClass onCreate() method like below.

String projectToken = "6107e97e4xxxxf22cddc822xxxxxxxx";
MixpanelAPI mixpanel = MixpanelAPI.getInstance(this, projectToken);

**Change your project token**

Now we are done with adding Google Analytics, Mix Panel and Localytics integration. Now I will teach you how to track events in your Android Application.

Let say we want to track following events:






Kotlin Android MVP Dagger 2 Retrofit Tutorial

http://developine.com/building-android-mvp-app-in-kotlin-using-dagger-retrofit/