Firebase

advertisement
Introduction
Free in Firebase –
1. A/B Testing,
2. Analytics,
3. App Indexing,
4. Authentication (except Phone Auth),
5. Cloud Messaging (FCM),
6. Crashlytics,
7. Dynamic Links,
8. Invites,
9. Performance Monitoring,
10. Predictions, and
11. Remote Config.
Setup
Automatic
First make sure you have installed Google Repository version 26 or higher, using the following steps:
1.
2.
3.
4.
5.
Click Tools > SDK Manager.
Click the SDK Tools tab.
Check the Google Repository checkbox, and click OK.
Click OK to install.
Click Background to complete the installation in the background, or wait for the installation to
complete and click Finish.
You can now open and use the Assistant window in Android Studio by following these steps:
1. Click Tools > Firebase to open the Assistant window.
2. Click to expand one of the listed features (for example, Analytics), then click the Get Started tutorial
to connect to Firebase and add the necessary code to your app.
Manually
To do this you'll need a Firebase project and a Firebase configuration file for your app.
To create a Firebase project:
1. Create a Firebase project in the Firebase console, if you don't already have one. Click Add project. If
you already have an existing Google project associated with your mobile app, select it from
the Project name drop down menu. Otherwise, enter a project name to create a new project.
2. Optional: Edit your Project ID. Your project is given a unique ID automatically, and it's used in
publicly visible Firebase features such as database URLs and your Firebase Hosting subdomain. You
can change it now if you want to use a specific subdomain.
3. Follow the remaining setup steps and click Create project (or Add Firebase if you're using an existing
project) to begin provisioning resources for your project. This typically takes a few minutes. When
the process completes, you'll be taken to the project overview.
Now that you have a project, you can add your Android app to it:
1. Click Add Firebase to your Android app and follow the setup steps. If you're importing an existing
Google project, this may happen automatically and you can just download the config file.
2. When prompted, enter your app's package name. It's important to enter the package name your app
is using; this can only be set when you add an app to your Firebase project.
3. During the process, you'll download a google-services.json file. You can download this file again at
any time.
4. After you add the initialization code, run your app to send verification to the Firebase console that
you've successfully installed Firebase.
If you would like to integrate the Firebase libraries into one of your own projects, you need to
perform a few basic tasks to prepare your Android Studio project. You may have already done this as
part of adding Firebase to your app.
First, add rules to your root-level build.gradle file, to include the google-services plugin and the
Google's Maven repository:
buildscript {
// ...
dependencies {
// ...
classpath 'com.google.gms:google-services:4.0.1' // google-services plugin
}
}
allprojects {
// ...
repositories {
// ...
google() // Google's Maven repository
}
}
Then, in your module Gradle file (usually the app/build.gradle), add the apply plugin line at the
bottom of the file to enable the Gradle plugin:
apply plugin: 'com.android.application'
android {
// ...
}
dependencies {
// ...
implementation 'com.google.firebase:firebase-core:16.0.1'
// Getting a "Could not find" error? Make sure you have
// added the Google maven respository to your root build.gradle
}
// ADD THIS AT THE BOTTOM
apply plugin: 'com.google.gms.google-services'
You should also add the dependencies for the Firebase SDKs you want to use. We recommend
starting with com.google.firebase:firebase-core
Authenticating Your Client
Certain Google Play services (such as Google Sign-in and App Invites) require you to provide the SHA1 of your signing certificate so we can create an OAuth2 client and API key for your app. To get your
SHA-1, follow these instructions:
Open a terminal and run the keytool utility provided with Java to get the SHA-1 fingerprint of the
certificate. You should get both the release and debug certificate fingerprints.
To get the release certificate fingerprint:
keytool -exportcert -list -v \
-alias <your-key-name> -keystore <path-to-production-keystore>
To get the debug certificate fingerprint:
keytool -exportcert -list -v \
-alias androiddebugkey -keystore %USERPROFILE%\.android\debug.keystore
The keytool utility prompts you to enter a password for the keystore. The default password for the
debug keystore is android. The keytool then prints the fingerprint to the terminal. For example:
Certificate fingerprint: SHA1: DA:39:A3:EE:5E:6B:4B:0D:32:55:BF:EF:95:60:18:90:AF:D8:07:09
Google Analytics
Google Analytics for Firebase is a free app measurement solution that provides insight on app usage
and user engagement.
At the heart of Firebase is Google Analytics for Firebase, a free and unlimited analytics solution.
Analytics integrates across Firebase features and provides you with unlimited reporting for up to 500
distinct events that you can define using the Firebase SDK. Analytics reports help you understand
clearly how your users behave, which enables you to make informed decisions regarding app
marketing and performance optimizations.
Google Analytics for Firebase collects usage and behavior data for your app. The SDK logs two
primary types of information:


Events: What is happening in your app, such as user actions, system events, or errors.
User properties: Attributes you define to describe segments of your user base, such as language
preference or geographic location.
Analytics automatically logs some events and user properties; you don't need to add any code to
enable them.
Automatically collected events
Firebase collects these events by default.
Automatically collected events are triggered by basic interactions with your app. As long as you use
the Firebase SDK, you don't need to write any additional code to collect these events. To see event
reports, click Events in the report navigation in Google Analytics, or click the Events tab in the
Firebase console.
Event name
Triggered...
ad_click
when a user clicks an ad.
ad_exposure
when at least one ad served by the Mobile Ads SDK is on screen.
ad_impression
when a user sees an ad impression.
ad_query
when an ad request is made by the Mobile Ads SDK.
ad_reward
when a reward is granted by a rewarded ad served by the Mobile Ads
SDK.
adunit_exposure
when an ad unit served by the Mobile Ads SDK is on screen.
app_clear_data
when the user resets/clears the app data, removing all settings and
sign-in data.
app_exception
when the app crashes or throws an exception.
app_remove
when an application package is removed or "uninstalled" from an
Android device.
This event is different from the Daily uninstalls by device and Daily
uninstalls by user metrics, which are both reported by Google Play
Developer Console. The app_remove event counts the removal of
application packages, regardless of the installation source, and the
count changes depending on the date range you are using for the
report. The Daily uninstalls by device and Daily uninstalls by
user metrics count the removal of application packages only when
they were installed from Google Play, and are reported on a daily
basis.
app_update
when the app is updated to a new version and launched again. The
previous app version id is passed as a parameter.
This event is conceptually different from the Daily upgrades by device
metric, which is reported by Google Play Developer Console. An
upgrade refers to the updating of the application binary, whereas an
app_update event is triggered upon the subsequent launch of the
upgraded app.
dynamic_link_app_open
when a user re-opens the app via a dynamic link.
dynamic_link_app_update
when the app is updated to a new version and is opened via a
dynamic link. Android apps only.
dynamic_link_first_open
when a user opens the app for the first time via a dynamic link.
first_open
the first time a user launches an app after installing or re-installing it.
This event is not triggered when a user downloads the app onto a
device, but instead when he or she first uses it. To see raw download
numbers, look in Google Play Developer Console or in iTunesConnect.
in_app_purchase
when a user completes an in-app purchase that is processed by the
App Store on iTunes or by Google Play. The product ID, product name,
currency, and quantity are passed as parameters.
To see in_app_purchase data for Android apps, you need to link
Firebase to Google Play.
This event is triggered only by versions of your app that include the
Firebase SDK. Note: paid app-purchase revenue, subscription revenue
(Android only), and refunds are not automatically tracked. Your
reported revenue may differ from the values you see in the Google
Play Developer Console. Events that are flagged as being invalid or as
sandbox (test) are ignored. Only iOS events are flagged as
sandbox. Learn more about testing Google Play billing.
notification_dismiss
when a user dismisses a notification sent by Firebase Notifications.
Android apps only.
notification_foreground
when a notification sent by Firebase Notifications is received while
the app is in the foreground.
notification_open
when a user opens a notification sent by Firebase Notifications.
notification_receive
when a notification sent by Firebase Notifications is received by a
device when the app is in the background. Android apps only.
os_update
when the device operating system is updated to a new version. The
previous operating system version id is passed as a parameter.
screen_view
when a screen transition occurs and any of the following criteria are
met:




No screen was previously set
The new screen name differs from the previous screen name
The new screen-class name differs from the previous screen-class
name
The new screen id differs from the previous screen id
session_start
when a user engages the app for more than the minimum session
durationafter a period of inactivity that exceeds the session timeout
duration.
user_engagement
periodically, while the app is in the foreground.
Automatically collected user properties
As long as you use the Firebase SDK, you don't need to write any additional code to collect a number
of user properties automatically. All of these user properties are available for use in Audience
conditions, and some of them are also available as general report filters.
User property
Type
Description
Age
Text
Identifies users by six categories: 18-24, 25-34, 35-44, 45-54, 55-64,
and 65+.
App Store
Text
The store from which the app was downloaded and installed.
App Version
Text
The versionName (Android) or the Bundle version (iOS).
Country
Text
The country the user resides in.
Device Brand
Text
The brand name of the mobile device (e.g., Motorola, LG, or
Samsung).
Device Category
Text
The category of the mobile device (e.g., mobile or tablet).
Device Model
Text
The mobile device model name (e.g., iPhone 5s or SM-J500M).
First Open Time
Number
The time (in milliseconds, UTC) at which the user first opened the
app, rounded up to the next hour.
Gender
Text
Identifies users as either male or female.
Interests
Text
Lists the interests of the user (e.g., "Arts & Entertainment, Games,
Sports").
Language
Text
The language setting of the device OS (e.g., en-us or pt-br).
New/Established
N/A
New: First opened the app within the last 7 days.
Established: First opened the app more than 7 days ago.
OS Version
Text
The version of the device OS (e.g., 9.3.2 or 5.1.1).
Add Analytics to your app
Add the dependency for Google Analytics for Firebase to your app-level build.gradle file:
implementation 'com.google.firebase:firebase-core:16.0.1'
Declare the com.google.firebase.analytics.FirebaseAnalytics object at the top of your activity
private FirebaseAnalytics mFirebaseAnalytics;
MainActivity.java
Then initialize it in the onCreate() method
// Obtain the FirebaseAnalytics instance.
mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
MainActivity.java
Log events
Once you have created a FirebaseAnalytics instance, you can use it to log either predefined or
custom events with the logEvent() method. You can explore the predefined events and parameters
in the FirebaseAnalytics.Eventand FirebaseAnalytics.Param reference documentation.
The following code logs a SELECT_CONTENT Event when a user clicks on a specific element in your
app.
Bundle bundle = new Bundle();
bundle.putString(FirebaseAnalytics.Param.ITEM_ID, id);
bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, name);
bundle.putString(FirebaseAnalytics.Param.CONTENT_TYPE, "image");
mFirebaseAnalytics.logEvent(FirebaseAnalytics.Event.SELECT_CONTENT, bundle);
MainActivity.java
If your application has specific needs not covered by a suggested event type, you can log your own
custom events as shown in this example:
Bundle params = new Bundle();
params.putString("image_name", name);
params.putString("full_text", text);
mFirebaseAnalytics.logEvent("share_image", params);
Confirm Events
You can enable verbose logging to monitor logging of events by the SDK to help verify that events
are being logged properly. This includes both automatically and manually logged events.
You can enable verbose logging with a series of adb commands:
adb shell setprop log.tag.FA VERBOSE
adb shell setprop log.tag.FA-SVC VERBOSE
adb logcat -v time -s FA FA-SVC
This command displays your events in the Android Studio logcat, helping you immediately verify that
events are being sent.
User properties
Set user properties to identify static or slow-changing attributes of your users, such as
"spender=true", "profession=engineer", or "favorite_team=Real Madrid". User properties are
effectively sticky event parameters that are automatically logged when you call logEvent. After you
create and register user-property values, you can use them to filter your reports. For example, you
can see how the game-play habits of spenders differ from non-spenders.
Each Firebase project can have up to 25 uniquely named (case-sensitive) user properties.
When you set user properties, be sure to never include personally identifiable information such as
names, social security numbers, or email addresses, even in hashed form.
Summary table
The summary table lists the user properties that you set in your Analytics tracking code and then
register in the Analytics interface.
Set and register a user property
Set
You set user properties in your Analytics tracking code code.
For information about calling setUserProperty (Android) or setUserPropertyString (iOS) in your
tracking code, refer to the developer documentation.
Register
In order to apply a user property as a report filter, you need to register it in the Analytics interface.
Filtering is possible on data received from the day of registration onwards. To register a new user
property:
1.
2.
3.
4.
In Analytics, navigate to your app.
Click User Properties.
Click NEW USER PROPERTY.
Enter a name and description for the user property, then click CREATE.
Download
Related flashcards
Create Flashcards