Last updated on August 22nd, 2017 at 01:12 pm
We know how much is Google analytics is important for our website to track users and session and lots of other things. Integration of Google analytics in a website is very easy you simply login to Google analytics and get analytics code and paste in your website page. Now in this post I like to explain you how to integrate Google analytics in your Android application. When you integrate analytics in your application you get various information like No. of users are active in your application, Location of users, Version which are they using and also some other information.
Integrating Google Analytics in Android Application
Step 1. Go to Firebase console and create a new project.
Step 2. Create new project name your project and select country and click on create the project.
Step 3. Now click on Add Firebase to your Android app and name your application package name and your system SHA1 here
Step 4. Now once you complete above step google-services.json is automatically downloaded on your browser if not you can download on link
Setup Google Analytics in App
Step 1. Now you need to move downloaded google-service.json file in your application Plz paste into specific location please follow bellow image
Step 2. Now open your Project Gradle and add google service library in it
// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { repositories { jcenter() } 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 } } allprojects { repositories { jcenter() } } task clean(type: Delete) { delete rootProject.buildDir }
Step 3. Now open your Application gradle and Google analytics library in it apply plugin as shown bellow.
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 4. Now create Application class which extends Application in this class we initialize GoogleAnaytics as shown bellow.
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 5. Now you need to call this in your manifest.xml file as shown bellow
<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 6. You can call analytic inside your activity using bellow code
// Analytics App application = (App) getApplication(); Tracker mTracker = application.getDefaultTracker();
That’s all from mobile application part.
Enable mobile analytics reporting on Google Analytics
Step 1. Login to your google analytics.
Step 2. Click on Admin button at bottom of page
Step 3. After click on admin go to property and add new property
Step 4. You need to select Mobile App and Firebase App and select your application
And save now you can view your analytics in firebase to view analytics you need to select your application
Now go to Streamview menu and click on it now you check active users as shown bellow
That’s all if you follow all steps I am sure you get your users info on google analytics.
I am already using above code in one of my e-commerce offloo.com application.
Hope this blog help you. Please like and share my blog if its help you :).