Firebase

How to use Firebase Analytics to track user behavior in a Flutter app?

Discover the steps to seamlessly add Firebase Analytics to your Flutter app. Track user activity, improve their experience, and gain key insights effortlessly.

Developer profile skeleton
a developer thinking

Overview

Watching what users do in a Flutter app using Firebase Analytics takes a few important steps. First, get Firebase ready for your Flutter project, including dependencies and all the necessary configs. Then, add the Firebase Analytics package from pub.dev to your app. After that's done, start logging events to track different user actions. You can define custom events to monitor specific activities beyond the usual ones. Grasping these steps will let you gather valuable insights about users, aiding in tailoring the app to meet their needs and wants effectively.

Get a Free No-Code Consultation
Meet with Will, CEO at Bootstrapped to get a Free No-Code Consultation
Book a Call
Will Hawkins
CEO at Bootstrapped

How to use Firebase Analytics to track user behavior in a Flutter app?

Step 1: Set Up Firebase Project

  1. Head over to the Firebase Console.
  2. Click "Add project" and just follow the steps to create a new Firebase project.
  3. After your project is ready, add an Android or iOS app by following the given instructions.

Step 2: Add Firebase to Flutter App

  1. Open up the pubspec.yaml file in your Flutter project.
  2. Add these dependencies for Firebase Analytics:
    ```yaml
    dependencies:
    firebase_core: latest_version
    firebase_analytics: latest_version
    ```
  3. Run flutter pub get to install the packages.

Step 3: Configure Firebase for Android and iOS

For Android:

  1. Download the google-services.json file from the Firebase Console and drop it in the android/app directory.
  2. Open android/build.gradle and add:
    ```gradle
    dependencies {
    classpath 'com.google.gms:google-services:4.3.5' // Check for the latest version
    }
    ```
  3. Then, open android/app/build.gradle and add:
    ```gradle
    apply plugin: 'com.google.gms.google-services'
    ```

For iOS:

  1. Download the GoogleService-Info.plist file from the Firebase Console and place it in the ios/Runner directory.

  2. Open ios/Runner/Info.plist and add:
    ```xml
    CFBundleURLTypes

    CFBundleTypeRole Editor CFBundleURLSchemes YOUR_REVERSED_CLIENT\_ID \`\`\`
  3. Make sure to enable the Firebase SDK in the ios/Runner/AppDelegate.swift:
    ```swift
    import Firebase

    @UIApplicationMain
    @objc class AppDelegate: FlutterAppDelegate {
    override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
    ) -> Bool {
    FirebaseApp.configure()
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
    }
    }
    ```

Step 4: Initialize Firebase in Flutter

  1. Open the main.dart file.
  2. Import the necessary Firebase packages:
    ```dart
    import 'package:firebase_core/firebase_core.dart';
    import 'package:firebase_analytics/firebase_analytics.dart';
    import 'package:firebase_analytics/observer.dart';
    ```
  3. Initialize Firebase in the main function:
    ```dart
    void main() async {
    WidgetsFlutterBinding.ensureInitialized();
    await Firebase.initializeApp();
    runApp(MyApp());
    }
    ```

Step 5: Use Firebase Analytics in Flutter

  1. Create a Firebase Analytics instance in the MyApp class:
    ```dart
    class MyApp extends StatelessWidget {
    static FirebaseAnalytics analytics = FirebaseAnalytics();
    static FirebaseAnalyticsObserver observer = FirebaseAnalyticsObserver(analytics: analytics);

    @override
    Widget build(BuildContext context) {
    return MaterialApp(
    navigatorObservers: [observer],
    // other properties
    );
    }
    }
    ```

  2. Log custom events to track user behavior:
    ```dart
    void _sendAnalyticsEvent() {
    analytics.logEvent(
    name: 'test_event',
    parameters: <String, dynamic>{
    'string': 'string',
    'int': 123,
    'long': 12345678910,
    'double': 123.45,
    'bool': true,
    },
    );
    }
    ```

  3. Call _sendAnalyticsEvent method at appropriate places in the app to log events:

```dart
RaisedButton(
onPressed: _sendAnalyticsEvent,
child: Text('Send Test Event'),
);
```

Step 6: Monitor Analytics Data in Firebase Console

  1. Open the Firebase Console.
  2. Navigate to the "Analytics" dashboard under the project.
  3. View user behavior data, custom events, and other metrics tracked in the app.

Explore more Firebase tutorials

Complete Guide to Firebase: Tutorials, Tips, and Best Practices

Explore our Firebase tutorials directory - an essential resource for learning how to create, deploy and manage robust server-side applications with ease and efficiency.

Why are companies choosing Bootstrapped?

40-60%

Faster with no-code

Nocode tools allow us to develop and deploy your new application 40-60% faster than regular app development methods.

90 days

From idea to MVP

Save time, money, and energy with an optimized hiring process. Access a pool of experts who are sourced, vetted, and matched to meet your precise requirements.

1 283 apps

built by our developers

With the Bootstrapped platform, managing projects and developers has never been easier.

hero graphic

Our capabilities

Bootstrapped offers a comprehensive suite of capabilities tailored for startups. Our expertise spans web and mobile app development, utilizing the latest technologies to ensure high performance and scalability. The team excels in creating intuitive user interfaces and seamless user experiences. We employ agile methodologies for flexible and efficient project management, ensuring timely delivery and adaptability to changing requirements. Additionally, Bootstrapped provides continuous support and maintenance, helping startups grow and evolve their digital products. Our services are designed to be affordable and high-quality, making them an ideal partner for new ventures.

Engineered for you

1

Fast Development: Bootstrapped specializes in helping startup founders build web and mobile apps quickly, ensuring a fast go-to-market strategy.

2

Tailored Solutions: The company offers customized app development, adapting to specific business needs and goals, which ensures your app stands out in the competitive market.

3

Expert Team: With a team of experienced developers and designers, Bootstrapped ensures high-quality, reliable, and scalable app solutions.

4

Affordable Pricing: Ideal for startups, Bootstrapped offers cost-effective development services without compromising on quality.

5

Supportive Partnership: Beyond development, Bootstrapped provides ongoing support and consultation, fostering long-term success for your startup.

6

Agile Methodology: Utilizing agile development practices, Bootstrapped ensures flexibility, iterative progress, and swift adaptation to changes, enhancing project success.

Yes, if you can dream it, we can build it.