docs/analytics/_get-started.md
{# This content gets published to the following location: #} {# https://firebase.google.com/docs/analytics/get-started?platform=flutter #}
Google Analytics collects usage and behavior data for your app. The SDK logs two primary types of information:
Analytics automatically logs some events and user properties; you don't need to add any code to enable them.
firebase_core and add the initialization code
to your app if you haven't already.From the root of your Flutter project, run the following command to install the plugin:
flutter pub add firebase_analytics
Once complete, rebuild your Flutter application:
flutter run
Once installed, you can access the firebase_analytics
plugin by importing it in your Dart code:
import 'package:firebase_analytics/firebase_analytics.dart';
Create a new Firebase Analytics instance by accessing the
instance property on
FirebaseAnalytics:
FirebaseAnalytics analytics = FirebaseAnalytics.instance;
After you have created a FirebaseAnalytics instance, you can begin to log
events with the library's log- methods.
Certain events are recommended for all apps; others are recommended for specific business types or verticals. You should send recommended events along with their prescribed parameters, to ensure maximum available detail in your reports and to benefit from future features and integrations as they become available. This section demonstrates logging a predefined event, for more information on logging events, see Log events.
The following code logs a checkout event:
await FirebaseAnalytics.instance
.logBeginCheckout(
value: 10.0,
currency: 'USD',
items: [
AnalyticsEventItem(
itemName: 'Socks',
itemId: 'xjw73ndnw',
price: '10.0'
),
],
coupon: '10PERCENTOFF'
);
If your app doesn't use IDFA, you can use FirebaseAnalyticsWithoutAdIdSupport
instead of the default FirebaseAnalytics iOS dependency to avoid App Store
review questions about advertising identifiers.
Set the FIREBASE_ANALYTICS_WITHOUT_ADID environment variable when building:
FIREBASE_ANALYTICS_WITHOUT_ADID=true flutter build ios
You can also add this variable to your Xcode scheme's environment variables for persistent configuration.
Add this to your app's Podfile:
pod 'FirebaseAnalytics', :modular_headers => true
pod 'FirebaseAnalyticsWithoutAdIdSupport', :modular_headers => true