Skip to main content

Flutter Firebase Analytics Examples

Flutter Firebase Analytics - How To Examples

Introduction

Flutter Firebase Analytics is a plugin for integrating Firebase Analytics into your Flutter applications. Firebase Analytics allows you to track user interactions and events in your app, providing valuable insights into user behavior and app performance. This tutorial will guide you through the installation process and provide examples of how to use Firebase Analytics in your Flutter app.

Installation

To get started with Flutter Firebase Analytics, you need to follow these steps:

  1. Add the firebase_analytics dependency to your pubspec.yaml file:
dependencies:
flutter:
sdk: flutter
firebase_core: ^1.0.0
firebase_analytics: ^8.0.0
  1. Run the following command to fetch the dependencies:
flutter pub get
  1. Import the firebase_analytics package in your Dart file:
import 'package:firebase_analytics/firebase_analytics.dart';
import 'package:firebase_analytics/observer.dart';
  1. Initialize Firebase Analytics in your app's main function:
void main() {
WidgetsFlutterBinding.ensureInitialized();
Firebase.initializeApp();
runApp(MyApp());
}

Now that you have installed Firebase Analytics, let's explore some examples of how to use it in your Flutter app.

Example 1: Log an Event

To log an event using Firebase Analytics, you can use the logEvent method. This method allows you to track custom events in your app. Here's how you can log a custom event:

FirebaseAnalytics().logEvent(
name: 'custom_event',
parameters: {'param1': 'value1', 'param2': 'value2'},
);

In this example, we log a custom event named 'custom_event' with two parameters: 'param1' and 'param2'. You can replace the event name and parameters with your own values.

Example 2: Set User Properties

You can set user properties to track specific user attributes or characteristics. Firebase Analytics provides the setUserProperty method for this purpose. Here's an example:

FirebaseAnalytics().setUserProperty(
name: 'favorite_genre',
value: 'action',
);

In this example, we set a user property named 'favorite_genre' with the value 'action'. You can set any custom user properties relevant to your app.

Example 3: Log Screen Views

To track screen views in your app, you can use the setCurrentScreen method. This method allows you to log the currently displayed screen. Here's an example:

FirebaseAnalytics().setCurrentScreen(
screenName: 'home_screen',
screenClassOverride: 'HomeScreen',
);

In this example, we log the current screen as 'home_screen' with the class name 'HomeScreen'. You can replace these values with your own screen names and class names.

Example 4: Enable Analytics Collection

By default, Firebase Analytics collects analytics data automatically. However, you can disable or enable this feature at runtime using the setAnalyticsCollectionEnabled method. Here's an example:

FirebaseAnalytics().setAnalyticsCollectionEnabled(false);

In this example, we disable analytics data collection. You can pass true to enable it again.

Example 5: Log Errors

Firebase Analytics allows you to log errors in your app using the logError method. This can be useful for tracking and troubleshooting errors. Here's an example:

FirebaseAnalytics().logError(
name: 'database_error',
message: 'Failed to fetch data from the database',
);

In this example, we log an error with the name 'database_error' and a custom error message. You can replace these values with your own error information.

Example 6: Log User Engagement

You can log user engagement events to track how users interact with your app. Firebase Analytics provides the logUserEngagement method for this purpose. Here's an example:

FirebaseAnalytics().logUserEngagement(
engagementTimeMillis: 5000,
);

In this example, we log a user engagement event with an engagement time of 5000 milliseconds. You can adjust the engagement time based on your app's requirements.

Example 7: Set User ID

To associate a user ID with the analytics data, you can use the setUserId method. This allows you to track specific users across different devices. Here's an example:

FirebaseAnalytics().setUserId('user123');

In this example, we set the user ID to 'user123'. You can replace this with your own user ID.

Example 8: Log Conversion Events

Firebase Analytics allows you to track conversion events, such as purchases or sign-ups. You can use the logConversion method for this purpose. Here's an example:

FirebaseAnalytics().logConversion(
currency: 'USD',
value: 9.99,
);

In this example, we log a conversion event with a currency of USD and a value of 9.99. You can customize the currency and value based on your conversion events.

Example 9: Enable Debug Mode

You can enable debug mode to see verbose logs from Firebase Analytics. This can be helpful for debugging and troubleshooting. Use the setAnalyticsCollectionEnabled method with true to enable debug mode. Here's an example:

FirebaseAnalytics().setAnalyticsCollectionEnabled(true);

In this example, we enable debug mode for Firebase Analytics.

Example 10: Log Social Sharing

To track social sharing events, you can use the logShare method. This allows you to log when users share content from your app. Here's an example:

FirebaseAnalytics().logShare(
contentType: 'image',
itemId: 'image123',
);

In this example, we log a share event with a content type of 'image' and an item ID of 'image123'. You can customize these values based on your app's sharing functionality.

Conclusion

In this tutorial, we explored various examples of how to use Firebase Analytics in Flutter. You learned how to log events, set user properties, track screen views, enable analytics collection, log errors and user engagement, set user IDs, track conversion events, enable debug mode, and log social sharing events. By integrating Firebase Analytics into your Flutter app, you can gain valuable insights into user behavior and improve your app's performance.