Google Ads with Google Analytics for Firebase custom events - Android

Last Updated: 2020-12-14

What you'll build

In this codelab, You will learn how to implement events with Google Analytics for Firebase(GA4F) and launch action campaigns through Google Ads.

This codelab focuses on implementing events with GA4F and importing the events in Google Ads. If you want to learn how to build an android app with Firebase, please refer to Firebase Android Codelab-Build Friendly Chat.

What you'll learn

  • How to create Firebase project
  • How to implement Firebase SDK in Android app
  • How to create events and parameters.
  • How to import events from Firebase to Google Ads
  • Launch Google Ads action campaigns with custom events.

What you'll need

  • Android Studio 3.6 or higher
  • Firebase Account
  • Google Ads Account

To get started with Firebase, you'll need to create and set up a Firebase project.

Create a Firebase project

  1. Sign in to Firebase.

In the Firebase console, click Add Project(or Create a project), and name your Firebase project as you want.

8525e77df3536fbb.png

  1. Click through the project creation options. Accept the Firebase terms if prompted. You should enable Google Analytics for this project, since you need Google Analytics events for tracking action events and conversions.

e58151a081f0628.png

To learn more about Firebase projects, see Understand Firebase projects.

  1. Choose Google Analytics account. If you have one choose that one or create a new one. Click on Create project button.

ac928e5947ba7cbf.png

  1. Now the Firebase project build has finished!

In the next step, you'll learn how to add Firebase to your Android app.

Register app

  1. In the Firebase Console, Select Project Overview in the left nav, then click the Android button under "Get started by adding Firebase to your app"

6cbb33ad3e7c9132.png

  1. Fill out necessary information.

3b7d3b33d81fe8ea.png

For the Android package name, in the Android studio go to the app directory, open a file name build.gradle. In the file, search for applicationId. This value is the Android package name. Copy this value and paste it.

We do not need the SHA-1 key here, unless you plan to use Google Sign In or Firebase Dynamic Links (Note that these are not part of this codelab, for more information about SHA-1 key, please refer to this document.).

  1. Click the Register App button.

Download config file

  1. Click the Download google-services.json button to download the config file google-services.json.

52f08aa18c8d59d0.png

  1. In the Android studio, go to the app directory and move the google-services.json file (that you just downloaded above) into the app directory. Back to the Firebase console, click the Next button.

Enable Firebase in your app

  1. You need the Google Services Gradle plugin to read the google-services.json file that was generated by Firebase.
  2. In Android studio, open <project>/build.gradle, then add the following lines in the file:
buildscript { 
   repositories {
      // Check that you have the following line (if not, add it):
      google() //Google's Maven repository
   }
   dependencies {
      ...
      // Add this line
      classpath 'com.google.gms:google-services:4.3.4'
   }
}

allprojects {
   ...
   repositories {
      // Check that you have the following line (if not, add it):
      google() //Google's Maven repository
      ...
      }
   }
}
  1. Open <project>/<app-module>/build.gradle, then add the following lines in the file:
apply plugin: 'com.android.application'
// Add this line
apply plugin: 'com.google.gms.google-services'

Now, you've completed adding Firebase to your Android app!

In the next step, you'll learn how to add Firebase SDK to your app.

In this step, you'll add Firebase SDK, specifically Firebase SDK for Google Analytics.

We'll use the Firebase Android BoM which enables you to manage Firebase library versions you are using in your app by specifying the BoM's version. To learn which Firebase library versions are mapped to a specific BoM version, check out the release notes for that BoM version.

  1. Open <project>/<app-module>/build.gradle, then add the following lines in the file:
dependencies { 
   // ... 
   
   //Import the Firebase BoM
   implementation platform('com.google.firebase:firebase-bom:26.1.1')
   
   //Declare the dependency for the Firebase SDK for Google Analytics
   implementation 'com.google.firebase:firebase-analytics-ktx'

As you can see, by specifying only the BoM's version, you don't need to specify the version of firebase analytics library. But if you want to use the desired version for your Firebase library no matter which version is designated in the BoM, you just need to specify the desired version like this:

dependencies { 
   // ... 
   
   //Import the Firebase BoM
   implementation platform('com.google.firebase:firebase-bom:26.1.1')
   
   //Declare the dependency for the Firebase SDK for Google Analytics
   implementation 'com.google.firebase:firebase-analytics-ktx:17.0.0'
  1. Now sync your app by clicking the Sync Now button. 937206076c88ae5c.png

Now, you've completed adding Firebase SDK for Google Analytics to your Android app!

In the next step, you'll learn how to log Firebase events in your Android app.

In this step, you'll learn how to log events with Firebase Analytics in the Android app.

There are 2 ways in logging events: Using suggested events or custom events.

Using suggested events

The Firebase Analytics SDK defines a number of suggested events that can be used in various types of apps such as gaming, retail, travel and ecommerce. Also there are some suggested events which require its prescribed parameters and with those parameters you can maximize available details in your Firebase reports. ( Suggested events reference)

  1. Declare FirebaseAnalytics object at the top of the activity:
private lateinit var firebaseAnalytics: FirebaseAnalytics
  1. Initialize the object in the onCreate() method:
firebaseAnalytics = Firebase.analytics
  1. Begin logging events with the logEvent() method. The following code logs a PURCHASE event when a user purchase an item(s) in the app:
firebaseAnalytics.logEvent(FirebaseAnalytics.Event.PURCHASE) {
   param(FirebaseAnalytics.Param.VALUE, 10)
   param(FirebaseAnalytics.Param.CURRENCY, "USE")
}

Using custom events

If you need events not covered by suggested events, you can log your own custom events with custom parameters:

firebaseAnalytics.logEvent("delete_item"){
   param("item name", name)
   param("reason", text)
}

Debug events

You can view aggregated data and reports about your events in the Firebase console. But the updates take place periodically throughout the day, you cannot view those events immediately after you logged them into your app.

For the immediate testing and debugging purposes, you can use Firebase Debugview. DebugView enables you to see event data logged by your app on development devices in near real-time. For DebugView, please refer to the Debugging event section.

Now you've completed logging events to your Android app!

In the next step, you'll learn how to import those events into Google Ads.

Your Firebase-Android setup has finished, and you're ready to launch the app campaign with logged action events. You'll start by linking Firebase to Google Ads. By linking Firebase to Google Ads, app campaigns will be able to learn more about audiences and boost app campaign learnings, which usually lead to better performance and conversions.

  1. Go to Firebase Settings by clicking the button right next to Project Overview.

412830708c61fdc0.png

  1. In the Integrations tab, you will see Google Ads and a Link button. Click Link and Continue.

796c921fb2b873ae.png

  1. Choose Google Ads account you want to link.

Now you've completed linking Firebase and Google Ads. Let's import Firebase events to Google Ads.

Import Firebase events

  1. In the Firebase console, go to the Events menu and mark the event which you want to import as a conversion.

a5f302ba01a1863c.png

  1. Now, Go to Google Ads and login to the account you link with the Firebase.
  2. In Google Ads, go to Tools → Measurement → Conversions 7acf268d7e799a7e.png
  3. Click + button d0eed72946d038d8.png
  4. Choose App → Google Analytics 4 properties (Firebase) and click continue. 983f06edb15724de.png
  5. You can see all of the analytics events that are marked as conversions. Find the event you want to import. In this case choose delete_item event that we've implemented before. Click the Import and Continue button. f9bdee80c0d40999.png
  6. You've successfully imported a conversion event from the Firebase. be75f6a11913236.png

Now you've completed importing Firebase events into Google Ads.

In the next step, you'll learn how to launch an action campaign with the event you've just imported.

  1. Go to the App campaigns tab in your current Google Ads account, and start a new campaign by clicking + button.

19856a65ab999ad7.png

  1. We will launch an App promotion campaign with the App Installs option. In the bottom, choose your app you want to promote and click continue. 8146b5a7a4f961bd.png
  2. In the Bidding section, if you select In-app actions in the dropdown menu you can view events. Choose an event(s) you want to focus on.

b75e410c7dbdfec8.png

  1. Set the rest of campaign settings and done.

Congratulations, you've successfully integrated your Firebase and Google Ads! This will help you boost your campaign performance.

You've learned

  • How to Configure Firebase Analytics for Android
  • How to log events with Firebase Analytics in an Android app.
  • How to import events and use them for action campaigns.