This guide shows how to add Analytics to your Android app to measure user activity to named screens. If you don't have an application yet and just want to see how Analytics works, take a look at our sample application.
Required: Latest versions of Android Studio and Google Play Services
Set up your project
Update your project's AndroidManifest.xml
file to include the
INTERNET
and ACCESS_NETWORK_STATE
permissions:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.analytics"> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <application android:name="AnalyticsApplication"> ... </application> </manifest>
The Google Services plugin for Gradle parses
configuration information from the google-services.json
file. Add the plugin to your
project by updating your top-level build.gradle
and your app-level
build.gradle
files as follows:
- Add the dependency to your project-level
build.gradle
:classpath 'com.google.gms:google-services:3.0.0'
- Add the plugin to the bottom of your app-level
build.gradle
:apply plugin: 'com.google.gms.google-services'
Now, you need to add a dependency for Google Play Services. Inside your app's
build.gradle
add:
compile 'com.google.android.gms:play-services-analytics:10.0.1'
Get a configuration file
Click the button below to get a configuration file to add to your project.
The configuration file provides service-specific information for your app. To get it, you must select an existing project for your app or create a new one. You'll also need to provide a package name for your app.
Get a Configuration FileAdd the configuration file to your project
Copy the google-services.json
file you just downloaded into the app/
or mobile/
directory of your Android Studio project. Open the Android Studio
Terminal pane:
$ mv path-to-download/google-services.json app/
$ move path-to-download/google-services.json app/
Add screen tracking
Here you’ll send a named screen view to Analytics whenever the user opens or changes screens on your app. Your code should do the following:
- Provide the shared tracker via an Application subclass.
- Override the callback method for the foreground activity.
- Provide a name for the screen and execute tracking.
Application
You should subclass Application
and provide a helper method
that returns your application's tracker.
/* * Copyright Google Inc. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.google.samples.quickstart.analytics; import android.app.Application; import com.google.android.gms.analytics.GoogleAnalytics; import com.google.android.gms.analytics.Tracker; /** * This is a subclass of {@link Application} used to provide shared objects for this app, such as * the {@link Tracker}. */ public class AnalyticsApplication extends Application { private Tracker mTracker; /** * Gets the default {@link Tracker} for this {@link Application}. * @return tracker */ synchronized public Tracker getDefaultTracker() { if (mTracker == null) { GoogleAnalytics analytics = GoogleAnalytics.getInstance(this); // To enable debug logging use: adb shell setprop log.tag.GAv4 DEBUG mTracker = analytics.newTracker(R.xml.global_tracker); } return mTracker; } }
Activity or fragment
Open the Activity that you'd like to track.
You could also track a Fragment
, but ensure that it
correctly represents a screen view.
Override the onCreate
method of the Activity
or
Fragment
you want to track to obtain the shared
Tracker
instance:
// Obtain the shared Tracker instance. AnalyticsApplication application = (AnalyticsApplication) getApplication(); mTracker = application.getDefaultTracker();
Override the appropriate method, such as onResume
for an Activity
or
onPageSelected
for a ViewPager
to log when the screen changes.
Log.i(TAG, "Setting screen name: " + name); mTracker.setScreenName("Image~" + name); mTracker.send(new HitBuilders.ScreenViewBuilder().build());
Add tracking code to every Activity
or Fragment
that represents a screen.
Be sure to set a name inside every Activity
or Fragment
if you want to
differentiate between screen views for your app in Analytics. All activity
recorded on the shared tracker sends the most recent screen name until replaced
or cleared (set to null
).
Send an event
To send an event, set the screen field values on the tracker, then send the hit.
The following example uses the HitBuilders.EventBuilder
to send an
Event
:
mTracker.send(new HitBuilders.EventBuilder() .setCategory("Action") .setAction("Share") .build());
Next steps
- Read the Mobile App Implementation Guide to learn how to use Google Analytics to measure user interactions and answer questions about app usage.
- Review additional configuration options such as sampling, testing and debugging, opt-out settings, etc.
- If your app needs to collect advertising identifiers, enable advertising features for the app.