Try Analytics for Android

Use our Android sample app to see how Analytics works, or add Analytics to your existing app.

Required: The latest versions of Android Studio and Google Play Services.

Get the project

If this is your first time using a Google services sample, check out the google-services repository.

$ git clone https://github.com/googlesamples/google-services.git

Open Android Studio.

Select File > Open, browse to where you cloned the google-services repository, and open google-services/android/analytics.

Create global_tracker.xml

Create the file app/src/res/xml/global_tracker.xml with the following content:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string name="ga_trackingId" translatable="false">${YOUR_TRACKING_ID}</string>
</resources>

Replace ${YOUR_TRACKING_ID} with your tracking ID.

Run the sample

Now you're ready to build the sample and run it from Android Studio.

Build the sample and click the run button and select a connected device or emulator.

Look for log messages that record Analytics activity. You can record activity on different screens by swiping left and right.

Go to Analytics and select the Analytics account you registered the app with. If you're unsure, look for the Analytics tracking ID in the google-services.json file you added to your project earlier. Copy that ID to the Analytics account search in the report drop-down. Select the Real-Time > Overview report for your property.

Now you can view your app's activity in real-time! The Screen Views graph displays screen views, and you can trigger more by clicking tabs in the application. The real-time report displays activity as it reaches the servers, so you can use this to quickly test your setup.

How it works

Like web tracking, Analytics for mobile collects user activity to your app with an Analytics beacon that sends data to the Analytics servers. Each unique view in your app corresponds to an Analytics screen view. The report aggregates all interaction data within the last 30 minutes.

This sample uses the Tracker interface, shown as mTracker in the code sample, to retrieve the screen name and send it as a screen view to the Analytics account associated with your app.

Note: Unlike web tracking, the Analytics Mobile SDK uploads activity signals in batches, both to conserve the device's radio and to handle periods of offline user activity.

Log.i(TAG, "Setting screen name: " + name);
mTracker.setScreenName("Image~" + name);
mTracker.send(new HitBuilders.ScreenViewBuilder().build());
  

Next steps

Learn how to use Google Analytics to collect more app usage info, such as taps to a UI element with Event tracking, or Google Play Campaign activity with Campaign tracking.

Add Analytics to your app

Did you have a good experience? Run into trouble? Let us know!