0
0
Firebasecloud~5 mins

Google Analytics integration in Firebase - Commands & Configuration

Choose your learning style9 modes available
Introduction
Google Analytics integration helps you track how users interact with your app. It solves the problem of understanding user behavior to improve your app's experience.
When you want to see how many people open your app each day.
When you want to know which features users like the most.
When you want to track if users complete important actions like signing up.
When you want to measure the success of marketing campaigns driving users to your app.
When you want to find and fix problems by seeing where users drop off.
Config File - firebase.json
firebase.json
{
  "hosting": {
    "public": "public",
    "ignore": ["firebase.json", "**/.*", "**/node_modules/**"],
    "rewrites": [
      {
        "source": "**",
        "destination": "/index.html"
      }
    ]
  }
}

This file configures Firebase Hosting for your app. The hosting section sets up your app's public files and routing. Google Analytics is enabled via the Firebase console and SDK, not directly in this config file.

Commands
This command initializes Google Analytics in your Firebase project, setting up necessary files and linking your app to Analytics.
Terminal
firebase init analytics
Expected OutputExpected
=== Initializing Google Analytics in your Firebase project ✔ Google Analytics has been enabled for your project. ✔ Analytics configuration files have been created.
This command deploys your app to Firebase Hosting with Google Analytics enabled, making tracking active for your live app.
Terminal
firebase deploy --only hosting
Expected OutputExpected
=== Deploying to 'your-project-id' ✔ hosting[your-project-id]: Successful upload of public directory files. ✔ hosting[your-project-id]: Release complete.
--only hosting - Deploy only the hosting part without affecting other Firebase services.
This command enables debug mode for Google Analytics to test and verify events during development.
Terminal
firebase analytics:debug --app your-app-id
Expected OutputExpected
Debug mode enabled for app your-app-id. Analytics events will be logged in the console.
Key Concept

If you remember nothing else from this pattern, remember: enabling Google Analytics in Firebase lets you see real user actions to improve your app.

Common Mistakes
Not enabling Analytics in the Firebase console before running commands.
Without enabling Analytics in the console, tracking won't start even if you deploy the app.
Go to the Firebase console, select your project, and enable Google Analytics before initializing it locally.
Forgetting to deploy hosting after enabling Analytics.
Changes in configuration only take effect after deployment, so no data will be collected if not deployed.
Always run 'firebase deploy --only hosting' after changing Analytics settings.
Not using debug mode during development to verify events.
Without debug mode, you can't see if Analytics events are sent correctly, making troubleshooting hard.
Use 'firebase analytics:debug' to test events before going live.
Summary
Run 'firebase init analytics' to set up Google Analytics in your Firebase project.
Deploy your app with 'firebase deploy --only hosting' to activate Analytics tracking.
Use 'firebase analytics:debug' to test and verify Analytics events during development.