In this codelab, you'll learn how to log insightful analytics events in a sample Android app. You'll be able to play with the sample app to trigger these events (which ultimately populates your Analytics reports) and you'll validate your instrumentation using debug logs.

What you'll learn

What you'll need

How will you use this tutorial?

Read it through only Read it and complete the exercises

How would rate your experience with building Android apps?

Novice Intermediate Proficient

You can either download all the sample code to your computer...

Download Zip

...or clone the GitHub repository from the command line.

$ git clone

First, familiarize yourself with the sample app. It's a simple eCommerce-style app that lets you shop for icons by adding them to a shopping cart and checking out. With the source code downloaded, the following instructions describe how to open the sample app in Android Studio.

  1. Select File > Open... and browse to the folder where you cloned the icon_shop repository.
  2. Click the Gradle sync button.
  3. Enable USB debugging on your Android device.
  4. With an Android device connected, click the Run button. You should see the Icon Shop App home screen appear after a few seconds.
  5. Verify that you're able to interact with the app, add items to your cart and checkout -- don't worry, this is just a demo app and no charges actually apply.

Frequently Asked Questions

Next, let's get started with Firebase!

  1. Open, and sign in using one of the credential cards that can be found around the codelabs area.
  2. If you see the "Welcome to your new account" prompt, click I accept. Continue to my account.

  1. The project assigned to you should be now visible on screen:

  1. Click on the project to access it. If asked to agree to updates to the Terms of Service, make sure to check Yes and click Accept.

Carefully follow the instructions to Add Firebase to your Android Project to the project you imported in the previous step. Note that when you are prompted to specify your app's package name, you should use com.github.googlecodelabs.icon_shop.

Now that you have a sense of the purpose and flow of the app, it's time to log some events using the Google Analytics for Firebase API so that you can track purchases and identify popular content

First, in order to use the Analytics API, you first need to import the package. Import these packages at the top of :


And that's it - you're ready to start logging events. The following method is called when the user adds an item to their cart. Note that it is defined inside actionButton.setOnClickListener, so if you can't find it, be sure to expand that function call. Let's log an event when this occurs so we can see which content is popular :

public void onClick(View view) {

if (mItem == null) return;


Snackbar.make(view, "Icon \"" + mItem.mName + "\" added to cart", Snackbar.LENGTH_LONG)

.setAction("Action", null).show();


Bundle params = new Bundle();

params.putString( Param.ITEM_ID, mItem.mName );

params.putString( Param.ITEM_CATEGORY, "icon" );

params.putLong( Param.VALUE, mItem.mPrice );

FirebaseAnalytics analytics = FirebaseAnalytics.getInstance( ItemCardActivity.this );

analytics.logEvent( Event.ADD_TO_CART, params );


Next, let's log an event when the user checks out. That happens in Let's import the Analytics packages again :


The following method is called when the user checks out. Note that it is defined inside mFloatingActionButton.setOnClickListener, so if you can't find it, be sure to expand that function call. Let's log an event when a purchase occurs so that we can capture our product revenue :

public void onClick(final View view) {
    Bundle params = new Bundle();
    params.putLong( Param.VALUE, getCartBalance());
    FirebaseAnalytics.getInstance( CartActivity.this ).logEvent( Event.ECOMMERCE_PURCHASE, params );

Now, we're tracking two key interactions in our app that will tell us a lot about the behavior patterns of our users and the popularity of our various products.

At this point, you're logging events that help you understand what content appeals to users and the frequency and value of purchases. These events form the basis for key business insights. But since different groups of users interact differently with your app, it's critically important to identify the characteristics that distinguish your most valuable users from those who are perhaps not very engaged at all. This will help you identify opportunities for optimization or capitalization.

We'll first need to import the Analytics packages before we use the API :


Now, let's set a user property to identify the user's profession :

private void onProfessionSelected(MenuItem item) {
    final String profession = item.getTitle().toString();
    FirebaseAnalytics analytics = FirebaseAnalytics.getInstance( this );
    analytics.setUserProperty( "profession", profession );

And that's it - now the rest of the events that are logged will carry with them this user property so that you can filter your analytics reports accordingly. In this case, you'll be able to correlate the user's profession to their browsing and purchasing behavior.

Now that you've integrated the Firebase SDK and have logged some events and set a user property, it's time to validate your instrumentation by examining the verbose debug output as you use the app and trigger these events:

  1. Make sure your device is still setup for USB debugging as we saw in the previous step.
  2. Execute the following Android Debug Bridge commands to enable Analytics verbose debug logging :
adb shell setprop log.tag.FA VERBOSE
adb shell setprop log.tag.FA-SVC VERBOSE

After running these commands, launch the app again and trigger some analytics events by adding items to the shopping cart and checking out. If you've logged these events correctly, you will see analytics reports within a few hours in the Firebase console in the Analytics tab. To confirm that you have logged the events correctly, compare your output to the output below. You should see output similar to the following :

05-11 21:54:30.871 I/FA      (29704): App measurement is starting up, version: 9060
05-11 21:54:30.872 D/FA      (29704): Debug logging enabled
05-11 21:54:30.873 D/FA      (29704): AppMeasurement singleton hash: 124396820
05-11 21:54:30.937 V/FA      (29704): Collection enabled
05-11 21:54:30.938 V/FA      (29704): App package, google app id: com.github.googlecodelabs.icon_shop, 1:125830372028:android:2a70ae1029a731d9
05-11 21:55:01.934 D/FA      (29704): Setting user property (FE): profession, Engineer
05-11 21:55:02.041 D/FA-SVC  (23882): User property set: profession, Engineer
05-11 21:55:05.227 D/FA      (29704): Logging event (FE): add_to_cart, Bundle[{_o=app, value=40, item_category=icon, item_id=activity}]
05-11 21:55:05.232 V/FA-SVC  (23882): Logging event: origin=app,name=add_to_cart,params=Bundle[mParcelledData.dataSize=160]
05-11 21:55:05.244 V/FA-SVC  (23882): Saving event, name, data size: add_to_cart, 73
05-11 21:55:05.244 V/FA-SVC  (23882): Event recorded: Event{appId='com.github.googlecodelabs.icon_shop', name='add_to_cart', params=Bundle[{_o=app, value=40, item_category=icon, item_id=activity}]}
05-11 21:55:09.685 D/FA      (29704): Logging event (FE): ecommerce_purchase, Bundle[{_o=app, value=40}]
05-11 21:55:09.696 V/FA-SVC  (23882): Event recorded: Event{appId='com.github.googlecodelabs.icon_shop', name='ecommerce_purchase', params=Bundle[{_o=app, value=40}]}

Your app is now instrumented using Google Analytics for Firebase.

What we've covered

Next Steps

Learn More