Google Analytics 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.
Before you begin
If you haven't already, add Firebase to your JavaScript project and make sure that Google Analytics is enabled in your Firebase project:
If you're creating a new Firebase project, enable Google Analytics during the project creation workflow.
If you're using an existing Firebase project that doesn't have Google Analytics enabled, go to the Integrations tab of your
to enable it. > Project settings
When you enable Google Analytics in your project, your Firebase web apps are linked to Google Analytics data streams associated with an App + Web property.
Add the Analytics SDK to your app
Depending on how your web application is hosted, your configuration may be handled automatically or you may need to update your Firebase configuration object. If your web app already uses Google Analytics, you may need to do additional setup described in Use Firebase with existing gtag.js tagging.
Check that your Firebase config object in your code contains
measurementId
. This ID is automatically created when you enable Analytics in your Firebase project and register a web app, and it's required to use Analytics.If your app uses Firebase Hosting and uses reserved URLs for the Firebase SDKs:
Firebase automatically handles configuring your application. To complete setup, add the scripts from the Your apps card in your Project settings to the <body> tag of your app, if you haven't already.
If your app does not use reserved URLs: If you're working with an existing web app, update the Firebase config object in your code to ensure the
measurementId
field is present. The config object should look similar to the following example:// For Firebase JavaScript SDK v7.20.0 and later, `measurementId` is an optional field const firebaseConfig = { apiKey: "AIzaSyCGQ0tYppWFJkuSxBhOpkH0xVDmX245Vdc", authDomain: "project-id.firebaseapp.com", databaseURL: "https://project-id.firebaseio.com", projectId: "project-id", storageBucket: "project-id.appspot.com", messagingSenderId: "637908496727", appId: "2:637908496727:web:a4284b4c99e329d5", measurementId: "G-9VP01NDSXJ" };
Initialize Firebase Analytics:
Web v8
const analytics = firebase.analytics();
Web v9
import { getAnalytics } from "firebase/analytics"; const analytics = getAnalytics();
Use Firebase with existing gtag.js tagging
If you previously had Google Analytics running in your app using the gtag.js snippet, your app may require additional setup if you plan to do one of the following:
- Add Google Analytics calls from Firebase to the page but also plan to
continue using
gtag()
calls directly on the same page. - Want to use the same measurement ID between both direct
gtag()
calls and Google Analytics data sent to Firebase.
To ensure your events are available for use by all Firebase services, complete the following additional setup steps:
Remove
[?id=GA_MEASUREMENT_ID](https://www.googletagmanager.com/gtag/js?id=GA_MEASUREMENT_ID)
in the url. Your gtag snippet should be similar to<script async src="https://www.googletagmanager.com/gtag/js"></script>
Remove the line
gtag('config', 'GA_MEASUREMENT_ID');
where theGA_MEASUREMENT_ID
is the same as themeasurementId
in your Firebase config snippet. If you have other IDs for other Analytics properties on the page, you do not need to remove their config line.Make sure you call
firebase.analytics()
before you send any events withgtag()
.
Otherwise, events sent to that ID with gtag()
calls will not be associated
with Firebase and will not be available for targeting in other
Firebase services.
Start logging events
After you have initialized the Analytics service with 'firebase.analytics()', you can begin to log events with the 'logEvent()' method.
Certain events are recommended for all apps; others are recommended for specific business types or verticals. You should send suggested events along with their prescribed parameters, to ensure maximum available detail in your reports and to benefit from future features and integrations as they become available. This section demonstrates logging a pre-defined event, for more information on logging events, see Log events.
The following example demonstrates how to log a recommended event to indicate a user has received a notificationt in your app:
Web v8
firebase.analytics().logEvent('notification_received');
Web v9
import { getAnalytics, logEvent } from "firebase/analytics"; const analytics = getAnalytics(); logEvent(analytics, 'notification_received');
Next steps
- Use the DebugView to verify your events.
- Explore your data in the Firebase console.
- Explore the guides on events and user properties.
- Learn how to export your data to BigQuery.