Analytics And Logging

Google Analytics

Introduction

Analytics is used to understand user behavior and optimizing our application. By leveraging analytics, we can gather insights that help us make data-driven decisions, improve user experiences, and run A/B testing to see what changes lead to better outcomes.

Why We Use Analytics

In our application, analytics allows us to track user interactions, monitor feature usage, and measure the success of various experiments (A/B testing). This data is vital for tailoring our app to meet user needs and ensuring that we're focusing on the right areas for improvement.

The Analytics Services We Use

We use Google Analytics through Firebase Analytics to track events within our application. Firebase Analytics serves as the foundation, collecting event data, which is then fed into Google Analytics for more advanced analysis and reporting.

Tracking Events in the Codebase

To track user interactions, we've implemented a function called _logEvent in analytics_repository.dart, which is responsible for sending event data to Firebase Analytics.

void _logEvent(String eventName, Map<String, dynamic> parameters) {
  // Log the event with Firebase Analytics
  FirebaseAnalytics.instance.logEvent(
    name: eventName,
    parameters: parameters,
  );
}

Creating a New Event

If you need to create a new event, follow these steps:

  1. Add new Event: Create a new function in analytics_repository.dart and decide what action or interaction you want to track. This could be anything from a button click to a screen view.
  2. Call the _logEvent Function: Use the _logEvent function in your newly created function and pass the event name and analytics data in _logEvent. For example, if you're tracking a "User Signup" event, you might add the following code:
    void trackUserSignup({required String method}) {
      _logEvent('user_signup', {
        'method': method,  // or 'google', 'facebook', etc.
      });
    }
    

Using the Event in Bloc or View

Once you've created the event, you can easily call it into your Bloc or View layer. For example, in a Bloc:

void onUserSignup() {
  ...
  _analyticsRepository.trackUserSignup(method: 'email');  // Call the event tracking function
}

or

In your View, you might trigger the event when a user interacts with the UI:

ElevatedButton(
  onPressed: () {
    _analyticsRepository.trackUserSignup(method: 'email'); // Track the event when the button is pressed
    Navigator.pushNamed(context, '/home');  // Navigate to the home screen
  },
  child: Text('Sign Up'),
)

Copyright © 2026