Get started with Google Analytics


This quickstart shows you how to addGoogle Analytics to your app andbegin logging events.

Google Analytics collects usage and behavior data for your app. The SDKlogs two primary types of information:

  • Events: What is happening in your app, such as user actions, systemevents, or errors.
  • User properties: Attributes you define to describe segments of youruser base, such as language preference or geographic location.

Analytics automatically logs someevents anduser properties;you don't need to add any code to enable them.

Before you begin

If you haven't already,add Firebase to your JavaScriptproject and make sure thatGoogle Analytics is enabledin your Firebase project:

  • If you're creating a new Firebase project, enableGoogle Analyticsduring the project creation workflow.

  • If you're using an existing Firebase project that doesn't haveGoogle Analytics enabled, go to theIntegrationstab of your >Project settings to enable it.

Warning: Any Firebase project created before July 31, 2019 must be upgraded tothe fullGoogle Analytics 4 experienceif it hasn't already. (Banners display in theAnalytics dashboard if anupgrade is required.) The associated Terms of Service must beaccepted by February 15, 2022to ensure data collection continues. See the Support FAQ on the upgrade for helpfinding project Owners.

When you enableGoogle Analytics in your project, your Firebase web appsare linked toGoogle Analytics data streams associated with anApp + Web property.

Add theAnalytics SDK to your app

Depending on how your web application is hosted, your configuration may behandled automatically or you may need to update yourFirebase configuration object.If your web app already uses Google Analytics, you may need to do additionalsetup described inUse Firebase with existing gtag.js tagging.

  1. Check that your Firebase config object in your code containsmeasurementId. This ID is automatically created when you enableAnalytics in your Firebase project and register a web app, and it'srequired to useAnalytics.

    Note: For apps using theFirebaseJavaScript SDK v7.20.0 and later,Firebase dynamically fetches themeasurementId when your app initializesAnalytics. Having this ID in your config object is optional, but it doesserve as a fallback in the rare case that the dynamic fetch fails.
    • If your app usesFirebase Hostingand usesreserved URLs for the Firebase SDKs:

      Firebase automatically handles configuring your application. To completesetup, add the scripts from theYour apps card in yourProject settingsto 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 configobject in your code to ensure themeasurementId field is present. Theconfig object should look similar to the following example:

      // ForFirebaseJavaScript SDK v7.20.0 and later, `measurementId` is an optional fieldconstfirebaseConfig={apiKey:"API_KEY",authDomain:"PROJECT_ID.firebaseapp.com",databaseURL:"https://PROJECT_ID.firebaseio.com",projectId:"PROJECT_ID",storageBucket:"PROJECT_ID.firebasestorage.app",messagingSenderId:"SENDER_ID",appId:"APP_ID",measurementId:"G-GA_MEASUREMENT_ID"};
  2. If you haven't already,install the Firebase JS SDK and initialize Firebase.

  3. Add theAnalytics JS SDK and initializeAnalytics:

Web

Learn more about thetree-shakeable modular web API andupgrade from the namespaced API.

import{initializeApp}from"firebase/app";import{getAnalytics}from"firebase/analytics";//TODO:Replacethefollowingwithyourapp's Firebase project configuration//See:https://firebase.google.com/docs/web/learn-more#config-objectconstfirebaseConfig={//...};//InitializeFirebaseconstapp=initializeApp(firebaseConfig);//InitializeAnalyticsandgetareferencetotheserviceconstanalytics=getAnalytics(app);

Web

Learn more about thetree-shakeable modular web API andupgrade from the namespaced API.

importfirebasefrom"firebase/compat/app";import"firebase/compat/analytics";//TODO:Replacethefollowingwithyourapp's Firebase project configuration//See:https://firebase.google.com/docs/web/learn-more#config-objectconstfirebaseConfig={//...};//InitializeFirebasefirebase.initializeApp(firebaseConfig);//InitializeAnalyticsandgetareferencetotheserviceconstanalytics=firebase.analytics();

Use Firebase with existing gtag.js tagging

If you previously had Google Analytics running in your app using thegtag.js snippet,your app may require additional setup if you plan to do one of the following:

  • AddGoogle Analytics calls from Firebase to the page but also plan tocontinue usinggtag() calls directly on the same page.
  • Want to use the same measurement ID between both directgtag() calls andGoogle Analytics data sent to Firebase.

To ensure your events are available for use by all Firebase services, completethe following additional setup steps:

  • Remove the linegtag('config', 'GA_MEASUREMENT_ID');where theGA_MEASUREMENT_ID is themeasurementId of yourFirebase web app. If you have other IDs for other Analytics propertieson the page, you do not need to remove their config line.
  • Make sure you callfirebase.analytics() before you send any eventswithgtag().

Otherwise, events sent to that ID withgtag() calls will not be associatedwith Firebase and will not be available for targeting in otherFirebase services.

Start logging events

After you have initialized theAnalytics service, you canbegin to log events with thelogEvent()method.

Certain events arerecommended for all apps;others are recommended for specific business types or verticals. You should sendsuggested events along with their prescribed parameters, to ensure maximumavailable detail in your reports and to benefit from future features andintegrations as they become available. This section demonstrates logging apre-defined event, for more information on logging events, seeLog events.

The following example demonstrates how to log a recommended event to indicate auser has received a notification in your app:

Web

import{getAnalytics,logEvent}from"firebase/analytics";constanalytics=getAnalytics();logEvent(analytics,'notification_received');

Web

firebase.analytics().logEvent('notification_received');

Next steps

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2026-02-18 UTC.