1. Introduction
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
2. Create and set up a Firebase project
To get started with Firebase, you'll need to create and set up a Firebase project.
Create a Firebase project
- Sign in to Firebase.
In the Firebase console, click Add Project(or Create a project), and name your Firebase project as you want.
- 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.
To learn more about Firebase projects, see Understand Firebase projects.
- Choose Google Analytics account. If you have one choose that one or create a new one. Click on Create project button.
- Now the Firebase project build has finished!
In the next step, you'll learn how to add Firebase to your Android app.
3. Add Firebase to your Android app
Register app
- 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"
- Fill out necessary information.
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.).
- Click the Register App button.
Download config file
- Click the Download google-services.json button to download the config file
google-services.json
.
- In the Android studio, go to the
app
directory and move thegoogle-services.json
file (that you just downloaded above) into theapp
directory. Back to the Firebase console, click the Next button.
Enable Firebase in your app
- You need the Google Services Gradle plugin to read the
google-services.json
file that was generated by Firebase. - 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
...
}
}
}
- 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.
4. 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.
- 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'
- Now sync your app by clicking the
Sync Now
button.
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.
5. Log events with Firebase Analytics
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)
- Declare FirebaseAnalytics object at the top of the activity:
private lateinit var firebaseAnalytics: FirebaseAnalytics
- Initialize the object in the
onCreate()
method:
firebaseAnalytics = Firebase.analytics
- 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.
6. Import Firebase 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.
Link Firebase and Google Ads
- Go to Firebase Settings by clicking the button right next to Project Overview.
- In the Integrations tab, you will see Google Ads and a Link button. Click Link and Continue.
- 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
- In the Firebase console, go to the Events menu and mark the event which you want to import as a conversion.
- Now, Go to Google Ads and login to the account you link with the Firebase.
- In Google Ads, go to Tools → Measurement → Conversions
- Click + button
- Choose App → Google Analytics 4 properties (Firebase) and click continue.
- 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. - You've successfully imported a conversion event from the Firebase.
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.
7. Launching app action campaigns with imported events
- Go to the App campaigns tab in your current Google Ads account, and start a new campaign by clicking + button.
- 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.
- 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.
- Set the rest of campaign settings and done.
8. Congratulations
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.