In this blog,
I have shown, How to track your android Application by a great and free analytics tool.
There are many analytics tools which can be used the like Segment.io, MixPanel, Localytics, Amazon Mobile Analytics etc. But They are not totally free and easily understandable.
So, here I am showing how to implement Google Analytics v4 in your application with a lot of key features like create multiple views according to different countries times, real-time users, bug report, active users and many more.
This is absolutely free of cost and easy to implement.
The Screen-shot of the real time users of Mobikul-OC-Demo App.
How to get the analytics credentials
There are some easy steps to setup the google analytics to your app
- Sign In to your Google Analytics account.
- Select Mobile App in the new property form.
- Enter Account Name.
- Enter the App Name.
- Select Industry Category & Report Time Zone and click on Get Tracking ID.
- In the next screen you will be shown your property Tracking ID.
How to add Analytics to Your Android App
Step 1: Add permission to your manifest.xml
1 2 3 4 5 6 7 8 9 10 |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.mobikul.analytics"> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <application android:name="MobikulApplication"> ... </application> </manifest> |
Step 2: Add a plugin to your project in your top-level build.gradle
and your app-level build.gradle
files as follows:
- Add the dependency to your project-level
build.gradle
:
1classpath 'com.google.gms:google-services:1.5.0-beta2' - Add the plugin to your app-level
build.gradle
:
1apply plugin: 'com.google.gms.google-services' - And you have to add a dependency for Google Play Services. Inside your app’s
build.gradle
add:
1compile 'com.google.android.gms:play-services-analytics:8.3.0'
Step 3: Get a configuration file by this link.
- After click to continue blue button if your have already registered to analytics then enable the analytics api for your App.
- If not, then first register and enable the analytics.
Step 4: Copy the google-services.json
file into your app/
or mobile/
directory of your Android Studio project.
How to track your Activity
There is some block of code to track your application’s current activity
- Provide the shared tracker via an Application subclass.
12345678910111213141516171819202122232425public class MobikulApplication extends Application {private Tracker mTracker;synchronized public Tracker getDefaultTracker() {if (mTracker == null) {GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);// To enable debug logging use: adb shell setprop log.tag.GAv4 DEBUGmTracker = analytics.newTracker(R.xml.global_tracker);}return mTracker;}public void trackException(Exception e) {if (e != null) {mTracker.send(new HitBuilders.ExceptionBuilder().setDescription(new StandardExceptionParser(this, null).getDescription(Thread.currentThread().getName(), e)).setFatal(false).build());}}} - Override the callback method for the foreground activity.
12MobikulApplication mMobikulApplication = (MobikulApplication) getApplication();mTracker = mMobikulApplication.getDefaultTracker();
- Provide a name for the screen(your activity) and execute tracking.
123String name = this.getClass().getName();mTracker.setScreenName("" + name);mTracker.send(new HitBuilders.ScreenViewBuilder().build());
Sources: