Enable Google Analytics for all Flutter platforms: Android, iOS, macOS, Web, Windows, and Linux.
The official Flutter plugin for Firebase Analytics (which uses Google Analytics behind the scenes) works with 4 platforms (Android, iOS, macOS, and Web). For Windows and Linux, there's no out-of-the-box support.
Ambilytics Flutter plugin solves this issue by enabling Google Analytics 4 (GA4) Measurement Protocol for unsupported platforms (Windows and Linux) and creating a unified interface that abstracts away interaction with 2 different analytics backends:
By creating Firebase project, configuring Google Analytics account, importing the package (and optionally setting navigation observer) you get the capability to send standard and custom events to Google Analytics and see data in Reports.
Product analytics made easy (and ambient).
app_start
custom event is sent during initialization, platform
parameter contains name of the platformAmbilyticsObserver
is configured screen_view
is sent on Firebase platforms along with screen_view_cust
via Measurement Protocol (either for all platforms or only Windows/Linux, configurable)showDialog()
actions as navigation eventsCheck /example
folder for usage detail.
Measurement Protocol can't be a complete replacement for the default Google Analytics backend:
screen_view
events sent via Firebase, a custom event screen_view_cust
is sent (with screen name param).
Hence the philosophy of this package: complete analytics for the platfroms supported by Firebase and essential events from unsupported platfroms (Windows and Linux).
Historically Firebase Analytics was used for app analytics on iOS/Android and Google Analytics for web. Now they are integrated. In order to proceed you'll need a Google Account. Using this account you will set-up a project in Firebase Console which will be linked to an account/property in Google Analytics. All reports will be available in Google Analytics Console.
The workflow has 2 steps:
Bellow you can find instructions for 2 scenarios:
The below instructions are based on this manual: https://firebase.google.com/docs/flutter/setup?platform=ios
firebase login
logut
command and than repeat the login
command to change the accountdart pub global activate flutterfire_cli
export PATH="$PATH":"$HOME/.pub-cache/bin"
for macOSflutterfire configure
macos\Runner\DebugProfile.entitlements
and macos\Runner\Release.entitlements
and add com.apple.security.network.client
, e.g.:
...
<key>com.apple.security.network.client</key>
<true/>
</dict>
</plist>
firebase_options.dart
to initialize Ambilytics
(see here)
static FirebaseOptions get currentPlatform {
if (kIsWeb) {
return web;
}
switch (defaultTargetPlatform) {
case TargetPlatform.android:
return android;
case TargetPlatform.iOS:
return ios;
case TargetPlatform.macOS:
return macos;
default:
return macos;
}
}
Analytics->Dashboard
tab to the left, click Enable Google Analytics
button. You will be presented with a wizard that will link Google Analytics account to Firebase project. At the last step Add the Google Analytics SDK
just click Finish
button as flutterfire configure
command above has already taken care of that.At this point you have Firebase/Google Analytics setup for up-to 4 platforms (Android, iOS, macOS and web). Next you need to Configure Measurement Protocol to cover Windows and Linux and Start using Ambilytics in your app and start sending events.
Notes:
firebase_options.dart
fileGiven you have been using Firebase Analytics plugin, native platforms are already set (the above a) step is not necessary), you are using FirebaseAnalytics.logEvent()
to send events and FirebaseAnalyticsObserver
to plug-in to navigation events. In this case in order to switch to Ambilytics
you have to:
FirebaseAnalytics.logEvent()
with sendEvent()
from this package and replace FirebaseAnalyticsObserver
with AmbilyticsObserver
. See using Ambilytics in your app To enable essential data sent from Windows
and Linux
you will have to create a new Web stream in Google Analytics to accept events sent via Measurement Protocol.
Add wtream
button -> Web
Measurement ID
Measurement Protocol API secrets
in the same view below. Create a new API secret and copy it.init()
method to get Measurement Protocol working (see next section)firebase_core
and ambilytics
packages to dependencies
section of pubspec.yaml
:
dependencies:
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^1.0.2
firebase_core:
ambilytics:
import 'package:ambilytics/ambilytics.dart' as ambilytics;
main
function add initialization call:
void main() async {
// This one
await ambilytics.initAnalytics(
measurementId: 'G-6R363DDKTZ',
apiSecret: 'uzUv6h_iRS6hEt_sIVtTTA',
firebaseOptions: DefaultFirebaseOptions.currentPlatform);
runApp(const MyApp());
}
firebase_options.dart
generated by flutterfire
(see above) with DefaultFirebaseOptions
class defined theremeasurementId
and apiSecret
MaterialApp
, e.g.:class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
// Here
navigatorObservers: ambilytics.isAmbilyticsInitialized
? [
ambilytics.AmbilyticsObserver(
routeFilter: ambilytics.anyRouteFilter,
alwaySendScreenViewCust: true)
]
: [],
routes: {
'/': (context) => HomeScreen(
title: 'Home',
analyticsError: ambilytics.initError,
),
'/color/red': (context) => const ColorScreen(),
'/color/yellow': (context) => const ColorScreen()
},
);
}
}
sendEvent
function:
void _incrementCounter() {
setState(() {
_counter++;
// Here
ambilytics.sendEvent(counterClicked, null);
});
}
The reports can be seen in both Google Analytics Console (https://analytics.google.com/analytics/web/) and Firebase Console (https://console.firebase.google.com/).
The package automatically sends 2 custom events:
app_launch
- sent upon initialization of Ambilytics. Contains platform
parameters with the name of the platform where the app is built.screen_view_cust
- sent via navigation observer upon navigation events (such as pushing a new route via Navigator API). Since Google Analytics doesn't allow sending predefined events (such as screen_view
) via its' logEvent()
this event is used for Windows and Linux as substitute for standard event. The events has screen_name
which is set to route name (Route.settings.name
)
AmbilyticsObserver.alwaySendScreenViewCust
parameter to have this custom event sent along with standard screen_view
with any of the Firebase platformsParameters of custom events (the above 2 and all sent via sendEvent()
) by default can only be seen in Realtime view. For other reports a custom dimension must be created:
app_launch
and screen_view_cust
app_launch_platform
dimension with platform
param and screen_view_cust_screen
with screen_name
parameterQuote from Google docs: To see the event parameter values, you must create a custom dimension or metric for each event parameter. Once Analytics processes the custom dimension or metric, you can add it to your custom reports, audiences, explorations, and segments, and as secondary dimensions in standard reports."
Note that up to 1 day might be required to get updated data in reports outside Realtime view.
After that you should be able to see custom events and their params in reports, e.g. pick Engagement -> Events
and click + sign in the Event name
column and choose Custom -> [Dimension you created]
In terminal go to macOS or iOS folder, remove Podfile.lock
and run pod repo update
If you get error
Execution failed for task ':app:mapReleaseSourceSetPaths'.
> Error while evaluating property 'extraGeneratedResDir' of task ':app:mapReleaseSourceSetPaths'
Go to android/build.gradle
and update com.google.gms:google-services
version, e.g.:
dependencies {
classpath 'com.android.tools.build:gradle:7.3.0'
// START: FlutterFire Configuration
classpath 'com.google.gms:google-services:4.3.15'
// END: FlutterFire Configuration
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
Go to android/app/build.gradle
and change minSdkVersion
version from flutter.minSdkVersion
to 19
.
All reports outside Realtime
can take up to a day to be in sync
For mobile platforms (Android, iOS) Firebase Analytics uploads/processes data in batches, i.e. it takes some time to collect and than send and display it (presumably for battery saving). As a result you get jagged events (i.e. some come right away, some take time).
Check the above manual for Fix macOS and update DebugProfile.entitlements
and Release.entitlements
A Firebase App named "[DEFAULT]" already exists
errorLikely reason for that is that you already used FirebaseAnalytics with configured native projects and started using FirebaseOptions.
You can try deleting android/app/src/google-services.json
, ios/Runner/GoogleService-Info.plist
and macos/Runner/GoogleService-Info.plist
files and rerun flutterfire configure