Add Google Analytics in Android Application

Aneh Thakur
Aug 22, 2017 · 2 min read

Google Analytics is very useful to track user in website and also get some website traffic information source and lots of other useful information. Now you can also add Google Analytics in your Android Application you can follow simple steps to install Google Analytics in your Application.

You can follow this Article to read complete Article in Detail :- https://wp.me/p4Y0GO-P6

Step 1. First you need to create Application in your Firebase console.

Step 2. After creating Application you get your google-services.json file save it we need it in our project.

Step 3. Now Create new android project or open project which you add in your Firebase console in Android Studio. Paste google-service.json inside your App folder

Step 4. Now open Project Gradle and add google service library in it

dependencies {classpath ‘com.android.tools.build:gradle:2.3.1’classpath ‘com.google.gms:google-services:3.0.0’// NOTE: Do not place your application dependencies here; they belong// in the individual module build.gradle files}

Step 5. Now open Application gradle and Google analytics library in it apply plugin in it

apply plugin: 'com.android.application'android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.offloo"
minSdkVersion 15
targetSdkVersion 25
versionCode 3
versionName "1.2"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
repositories {
mavenCentral() // jcenter() works as well because it pulls from Maven Central
maven {
url "https://s3-ap-southeast-1.amazonaws.com/godel-release/godel/"
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:design:25.2.0'
compile 'com.instamojo:android-sdk:+'
compile 'com.android.support:cardview-v7:25.2.0'
compile 'com.android.support:support-v4:25.2.0'
compile 'com.android.support:appcompat-v7:25.2.0'
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.android.support.constraint:constraint-layout:1.0.1'
testCompile 'junit:junit:4.12'
compile 'com.google.firebase:firebase-messaging:10.2.1'
compile 'com.google.android.gms:play-services-analytics:10.2.1'
}
apply plugin: 'com.google.gms.google-services'

Step 6. Now create new class in your project which extend Application in this we can initialise our Google Analytics.

package com.offloo;import android.app.Application;import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.Tracker;
public class App extends Application {private static GoogleAnalytics sAnalytics;
private static Tracker sTracker;
@Override
public void onCreate() {
super.onCreate();
sAnalytics = GoogleAnalytics.getInstance(this);}/**
* Gets the default {@link Tracker} for this {@link Application}.
* @return tracker
*/
synchronized public Tracker getDefaultTracker() {
// To enable debug logging use: adb shell setprop log.tag.GAv4 DEBUG
if (sTracker == null) {
sTracker = sAnalytics.newTracker(R.xml.global_tracker);
}
return sTracker;
}
}

Step 7. Now open you manifest.xml file and add your Application class in it.

<application
android:name=".App"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
/* Your code */
</application>

Step 8. Now you can call Google Analytics in your activity

// Analytics
App application = (App) getApplication();
Tracker mTracker = application.getDefaultTracker();

Now to check your App Analytics you can login to Google Analytics Dashboard and select your App.

To read complete Article you can follow this link: Google Analytics integration Android

)
Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade