firebase_analytics 5.0.11

  • Readme
  • Changelog
  • Example
  • Installing
  • 100

Google Analytics for Firebase #

pub package

A Flutter plugin to use the Google Analytics for Firebase API.

For Flutter plugins for other Firebase products, see README.md.

Usage #

To use this plugin, add firebase_analytics as a dependency in your pubspec.yaml file. You must also configure firebase analytics for each platform project: Android and iOS (see the example folder or https://codelabs.developers.google.com/codelabs/flutter-firebase/#4 for step by step details).

Track PageRoute Transitions #

To track PageRoute transitions, add a FirebaseAnalyticsObserver to the list of NavigatorObservers on your Navigator, e.g. if you're using a MaterialApp:


FirebaseAnalytics analytics = FirebaseAnalytics();

MaterialApp(
  home: MyAppHome(),
  navigatorObservers: [
    FirebaseAnalyticsObserver(analytics: analytics),
  ],
);

You can also track transitions within your PageRoute (e.g. when the user switches from one tab to another) by implementing RouteAware and subscribing it to FirebaseAnalyticsObserver. See example/lib/tabs_page.dart for an example of how to wire that up.

Getting Started #

See the example directory for a complete sample app using Google Analytics for Firebase.

Issues and feedback #

Please file Flutterfire specific issues, bugs, or feature requests in our issue tracker.

Plugin issues that are not specific to Flutterfire can be filed in the Flutter issue tracker.

To contribute a change to this plugin, please review our contribution guide, and send a pull request.

5.0.11 #

  • Fix overrides a deprecated API.
  • Raise minimum required Flutter SDK version to 1.12.13+hotfix.4

5.0.10 #

  • Keep a local registrar to get activity for foreground actions for v1 embedder.

5.0.9 #

  • Remove the deprecated author: field from pubspec.yaml
  • Migrate the plugin to the pubspec platforms manifest.
  • Bump the minimum Flutter version to 1.10.0.

5.0.8 #

  • Update homepage since the package moved for the platform interface migration.

5.0.7 #

  • Formatted setCurrentScreen documentation.

5.0.6 #

  • Updated README instructions for contributing for consistency with other Flutterfire plugins.

5.0.5 #

  • Remove AndroidX warning.

5.0.4 #

  • Include lifecycle dependency as a compileOnly one on Android to resolve potential version conflicts with other transitive libraries.

5.0.3 #

  • Support the v2 Android embedding.

5.0.2 #

  • Fixed setAnalyticsCollectionEnabled on iOS.

5.0.1 #

  • Update documentation to reflect new repository location.
  • Update unit tests to call TestWidgetsFlutterBinding.ensureInitialized.
  • Remove executable bit on LICENSE file.

5.0.0 #

  • Breaking change. Remove deprecated method setMinimumSessionDuration.
  • Breaking change. Removed FirebaseAnalyticsAndroid.setAnalyticsCollectionEnabled. Use FirebaseAnalytics.setAnalyticsCollectionEnabled instead.
  • Update Android gradle plugin and gradle version.
  • Remove redundant casts on Android.

4.0.2 #

  • Update google-services Android gradle plugin to 4.3.0 in documentation and examples.

4.0.1 #

  • Refactor unit tests to use setMockMethodCallHandler.

4.0.0 #

  • Added new tracking events:
    • logLevelStart
    • logLevelEnd
    • logRemoveFromCart
    • logSetCheckoutOption
  • Breaking change. Add new required parameter method to logShare event tracking.
  • Breaking change. The following event names are reserved and cannot be used:
    • ad_activeview
    • ad_click
    • ad_exposure
    • ad_impression
    • ad_query
    • adunit_exposure
    • first_visit
    • screen_view

3.0.3 #

  • Automatically use version from pubspec.yaml when reporting usage to Firebase.

3.0.2 #

  • Bump minimum Flutter version to 1.5.0.
  • Add missing template type parameter to invokeMethod calls.

3.0.1 #

  • Switch to using the FIRAnalytics version of setAnalyticsCollectionEnabled for compatibility with Firebase Analytics iOS CocoaPod version 6.0.
  • Update podspec to ensure availability of setAnalyticsCollectionEnabled.

3.0.0 #

  • Update Android dependencies to latest.

2.1.1+3 #

  • Added an initial integration test.

2.1.1+2 #

  • Fixed errors in code sample for FirebaseAnalyticsObserver.

2.1.1+1 #

  • Added hyperlinks to example app reference in README.md.

2.1.1 #

  • Added screen_view tracking of Navigator.pushReplacement

2.1.0 #

  • Add Login event support

2.0.3 #

  • Add resetAnalyticsData method

2.0.2+1 #

  • Log messages about automatic configuration of the default app are now less confusing.

2.0.2 #

  • Enable setAnalyticsCollectionEnabled support for iOS

2.0.1 #

  • Log a more detailed warning at build time about the previous AndroidX migration.

2.0.0 #

  • Breaking change. Migrate from the deprecated original Android Support Library to AndroidX. This shouldn't result in any functional changes, but it requires any Android apps using this plugin to also migrate if they're using the original support library.

    This was originally incorrectly pushed in the 1.2.0 update.

1.2.0+1 #

  • Revert the breaking 1.2.0 update. 1.2.0 was known to be breaking and should have incremented the major version number instead of the minor. This revert is in and of itself breaking for anyone that has already migrated however. Anyone who has already migrated their app to AndroidX should immediately update to 2.0.0 instead. That's the correctly versioned new push of 1.2.0.

1.2.0 #

  • BAD. This was a breaking change that was incorrectly published on a minor version upgrade, should never have happened. Reverted by 1.2.0+1.

    "Breaking change. Migrate from the deprecated original Android Support Library to AndroidX. This shouldn't result in any functional changes, but it requires any Android apps using this plugin to also migrate if they're using the original support library."

1.1.0 #

  • Allow user to handle PlatformExceptions caught by FirebaseAnalyticsObserver._sendScreenView().

1.0.6 #

  • Allow user ID to be set to null.

1.0.5 #

  • Update the METHOD Android constant used for logSignUp method.

1.0.4 #

  • Bump Android dependencies to latest.

1.0.3 #

  • Updated test and mockito dependencies to pick up Dart 2 support

1.0.2 #

  • Bump Android and Firebase dependency versions.

1.0.1 #

  • Updated Gradle tooling to match Android Studio 3.1.2.

1.0.0 #

  • Bump to released version.

0.3.3 #

  • Updated Google Play Services dependencies to version 15.0.0.

0.3.2 #

  • Updated package channel name

0.3.1 #

  • Simplified podspec for Cocoapods 1.5.0, avoiding link issues in app archives.

0.3.0 #

  • Breaking change. Set SDK constraints to match the Flutter beta release.

0.2.3 #

  • Enabled use in Swift projects.

0.2.2+1 #

  • Updated description to clarify this is 'Google Analytics for Firebase'

0.2.2 #

  • Moved to the io.flutter.plugins organization.

0.2.1 #

  • Simplified and upgraded Android project template to Android SDK 27.
  • Updated package description.

0.2.0 #

  • Breaking change. Upgraded to Gradle 4.1 and Android Studio Gradle plugin 3.0.1. Older Flutter projects need to upgrade their Gradle setup as well in order to use this version of the plugin. Instructions can be found here.
  • Relaxed GMS dependency to [11.4.0,12.0[

0.1.2 #

  • Added FLT prefix to iOS types
  • Change GMS dependency to 11.4.+

0.1.1 #

  • Change GMS dependency to 11.+

0.1.0+1 #

  • Aligned author name with rest of repo.

0.1.0 #

  • Added FirebaseAnalyticsObserver (a NavigatorObserver) to automatically log PageRoute transitions

0.0.5 #

  • Support for long parameter values on Android

0.0.4 #

  • Updated to Firebase SDK to always use latest patch version for 11.0.x builds

0.0.3 #

  • Updated to Firebase SDK Version 11.0.1

0.0.2 #

  • Bumped buildToolsVersion to 25.0.3
  • Updated README.md

0.0.1 #

  • Initial Release

example/lib/main.dart

// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:firebase_analytics/firebase_analytics.dart';
import 'package:firebase_analytics/observer.dart';

import 'tabs_page.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  static FirebaseAnalytics analytics = FirebaseAnalytics();
  static FirebaseAnalyticsObserver observer =
      FirebaseAnalyticsObserver(analytics: analytics);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Firebase Analytics Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      navigatorObservers: <NavigatorObserver>[observer],
      home: MyHomePage(
        title: 'Firebase Analytics Demo',
        analytics: analytics,
        observer: observer,
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title, this.analytics, this.observer})
      : super(key: key);

  final String title;
  final FirebaseAnalytics analytics;
  final FirebaseAnalyticsObserver observer;

  @override
  _MyHomePageState createState() => _MyHomePageState(analytics, observer);
}

class _MyHomePageState extends State<MyHomePage> {
  _MyHomePageState(this.analytics, this.observer);

  final FirebaseAnalyticsObserver observer;
  final FirebaseAnalytics analytics;
  String _message = '';

  void setMessage(String message) {
    setState(() {
      _message = message;
    });
  }

  Future<void> _sendAnalyticsEvent() async {
    await analytics.logEvent(
      name: 'test_event',
      parameters: <String, dynamic>{
        'string': 'string',
        'int': 42,
        'long': 12345678910,
        'double': 42.0,
        'bool': true,
      },
    );
    setMessage('logEvent succeeded');
  }

  Future<void> _testSetUserId() async {
    await analytics.setUserId('some-user');
    setMessage('setUserId succeeded');
  }

  Future<void> _testSetCurrentScreen() async {
    await analytics.setCurrentScreen(
      screenName: 'Analytics Demo',
      screenClassOverride: 'AnalyticsDemo',
    );
    setMessage('setCurrentScreen succeeded');
  }

  Future<void> _testSetAnalyticsCollectionEnabled() async {
    await analytics.setAnalyticsCollectionEnabled(false);
    await analytics.setAnalyticsCollectionEnabled(true);
    setMessage('setAnalyticsCollectionEnabled succeeded');
  }

  Future<void> _testSetSessionTimeoutDuration() async {
    await analytics.android?.setSessionTimeoutDuration(2000000);
    setMessage('setSessionTimeoutDuration succeeded');
  }

  Future<void> _testSetUserProperty() async {
    await analytics.setUserProperty(name: 'regular', value: 'indeed');
    setMessage('setUserProperty succeeded');
  }

  Future<void> _testAllEventTypes() async {
    await analytics.logAddPaymentInfo();
    await analytics.logAddToCart(
      currency: 'USD',
      value: 123.0,
      itemId: 'test item id',
      itemName: 'test item name',
      itemCategory: 'test item category',
      quantity: 5,
      price: 24.0,
      origin: 'test origin',
      itemLocationId: 'test location id',
      destination: 'test destination',
      startDate: '2015-09-14',
      endDate: '2015-09-17',
    );
    await analytics.logAddToWishlist(
      itemId: 'test item id',
      itemName: 'test item name',
      itemCategory: 'test item category',
      quantity: 5,
      price: 24.0,
      value: 123.0,
      currency: 'USD',
      itemLocationId: 'test location id',
    );
    await analytics.logAppOpen();
    await analytics.logBeginCheckout(
      value: 123.0,
      currency: 'USD',
      transactionId: 'test tx id',
      numberOfNights: 2,
      numberOfRooms: 3,
      numberOfPassengers: 4,
      origin: 'test origin',
      destination: 'test destination',
      startDate: '2015-09-14',
      endDate: '2015-09-17',
      travelClass: 'test travel class',
    );
    await analytics.logCampaignDetails(
      source: 'test source',
      medium: 'test medium',
      campaign: 'test campaign',
      term: 'test term',
      content: 'test content',
      aclid: 'test aclid',
      cp1: 'test cp1',
    );
    await analytics.logEarnVirtualCurrency(
      virtualCurrencyName: 'bitcoin',
      value: 345.66,
    );
    await analytics.logEcommercePurchase(
      currency: 'USD',
      value: 432.45,
      transactionId: 'test tx id',
      tax: 3.45,
      shipping: 5.67,
      coupon: 'test coupon',
      location: 'test location',
      numberOfNights: 3,
      numberOfRooms: 4,
      numberOfPassengers: 5,
      origin: 'test origin',
      destination: 'test destination',
      startDate: '2015-09-13',
      endDate: '2015-09-14',
      travelClass: 'test travel class',
    );
    await analytics.logGenerateLead(
      currency: 'USD',
      value: 123.45,
    );
    await analytics.logJoinGroup(
      groupId: 'test group id',
    );
    await analytics.logLevelUp(
      level: 5,
      character: 'witch doctor',
    );
    await analytics.logLogin();
    await analytics.logPostScore(
      score: 1000000,
      level: 70,
      character: 'tiefling cleric',
    );
    await analytics.logPresentOffer(
      itemId: 'test item id',
      itemName: 'test item name',
      itemCategory: 'test item category',
      quantity: 6,
      price: 3.45,
      value: 67.8,
      currency: 'USD',
      itemLocationId: 'test item location id',
    );
    await analytics.logPurchaseRefund(
      currency: 'USD',
      value: 45.67,
      transactionId: 'test tx id',
    );
    await analytics.logSearch(
      searchTerm: 'hotel',
      numberOfNights: 2,
      numberOfRooms: 1,
      numberOfPassengers: 3,
      origin: 'test origin',
      destination: 'test destination',
      startDate: '2015-09-14',
      endDate: '2015-09-16',
      travelClass: 'test travel class',
    );
    await analytics.logSelectContent(
      contentType: 'test content type',
      itemId: 'test item id',
    );
    await analytics.logShare(
        contentType: 'test content type',
        itemId: 'test item id',
        method: 'facebook');
    await analytics.logSignUp(
      signUpMethod: 'test sign up method',
    );
    await analytics.logSpendVirtualCurrency(
      itemName: 'test item name',
      virtualCurrencyName: 'bitcoin',
      value: 34,
    );
    await analytics.logTutorialBegin();
    await analytics.logTutorialComplete();
    await analytics.logUnlockAchievement(id: 'all Firebase API covered');
    await analytics.logViewItem(
      itemId: 'test item id',
      itemName: 'test item name',
      itemCategory: 'test item category',
      itemLocationId: 'test item location id',
      price: 3.45,
      quantity: 6,
      currency: 'USD',
      value: 67.8,
      flightNumber: 'test flight number',
      numberOfPassengers: 3,
      numberOfRooms: 1,
      numberOfNights: 2,
      origin: 'test origin',
      destination: 'test destination',
      startDate: '2015-09-14',
      endDate: '2015-09-15',
      searchTerm: 'test search term',
      travelClass: 'test travel class',
    );
    await analytics.logViewItemList(
      itemCategory: 'test item category',
    );
    await analytics.logViewSearchResults(
      searchTerm: 'test search term',
    );
    setMessage('All standard events logged successfully');
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Column(
        children: <Widget>[
          MaterialButton(
            child: const Text('Test logEvent'),
            onPressed: _sendAnalyticsEvent,
          ),
          MaterialButton(
            child: const Text('Test standard event types'),
            onPressed: _testAllEventTypes,
          ),
          MaterialButton(
            child: const Text('Test setUserId'),
            onPressed: _testSetUserId,
          ),
          MaterialButton(
            child: const Text('Test setCurrentScreen'),
            onPressed: _testSetCurrentScreen,
          ),
          MaterialButton(
            child: const Text('Test setAnalyticsCollectionEnabled'),
            onPressed: _testSetAnalyticsCollectionEnabled,
          ),
          MaterialButton(
            child: const Text('Test setSessionTimeoutDuration'),
            onPressed: _testSetSessionTimeoutDuration,
          ),
          MaterialButton(
            child: const Text('Test setUserProperty'),
            onPressed: _testSetUserProperty,
          ),
          Text(_message,
              style: const TextStyle(color: Color.fromARGB(255, 0, 155, 0))),
        ],
      ),
      floatingActionButton: FloatingActionButton(
          child: const Icon(Icons.tab),
          onPressed: () {
            Navigator.of(context).push(MaterialPageRoute<TabsPage>(
                settings: const RouteSettings(name: TabsPage.routeName),
                builder: (BuildContext context) {
                  return TabsPage(observer);
                }));
          }),
    );
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  firebase_analytics: ^5.0.11

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:firebase_analytics/firebase_analytics.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
99
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
100
Learn more about scoring.

We analyzed this package on Apr 7, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6
  • Flutter: 1.12.13+hotfix.8

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.28.0 <3.0.0
flutter 0.0.0
meta ^1.0.4 1.1.8
Transitive dependencies
collection 1.14.11 1.14.12
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
e2e ^0.2.1
firebase_core ^0.4.0
flutter_driver
flutter_test
mockito 3.0.0