initAnalytics function
Prepares analytics for usage. Doesn't throw errors, in debug mode throws assertions. If Ambilytics fails to initialize isAmbilyticsInitialized returns false.
If the platform is Android, iOS, macOS, or Web, Firebase Analytics will be used (_firebaseAnalytics
instance will be initialized).
Otherwise, GA4 Measurement protocol and custom events will be used (_ambilytics
instance will be initialized).
If fallbackToMP
is true, than Measurement Protocol will be used if Firebase analytics fails to initialize. E.g. you can skip configuring Firebase Analytics in native projects and use MP for all platforms.
If disableAnalytics
is true
, analytics will not be initialized, any analytics calls will be ignored,
_firebaseAnalytics
and _ambilytics
instances will be null. Useful for the scenarios when toy wish to disable analytics.
If sendAppLaunch
is true, "app_launch" will be sent with "platform" param value corresponding runtime platform (i.e. Windows)
firebaseOptions
forwards options (e.g. generated via flutterfire configure
) to Firebase.initializeApp()
.
apiSecret
and measurementId
must be set in order to enable GA4 Measurement protocol and have _ambilytics
initialized.
userId
allows overriding user identifier. If not provided, default user ID will be used by Firebase Analytics OR
or a GUID will be created and put to shared_preferences storage (for Windows and Linux).
Implementation
Future<void> initAnalytics(
{bool disableAnalytics = false,
bool fallbackToMP = false,
bool sendAppLaunch = true,
FirebaseOptions? firebaseOptions,
String? measurementId,
String? apiSecret,
String? userId}) async {
_disabled = disableAnalytics;
if (_initialized) return;
try {
WidgetsFlutterBinding.ensureInitialized();
if (defaultTargetPlatform == TargetPlatform.android ||
defaultTargetPlatform == TargetPlatform.iOS ||
defaultTargetPlatform == TargetPlatform.macOS ||
kIsWeb) {
try {
await Firebase.initializeApp(options: firebaseOptions);
_firebaseAnalytics = FirebaseAnalytics.instance;
if (userId != null) {
await _firebaseAnalytics!.setUserId(id: userId);
}
_initialized = true;
if (sendAppLaunch && !_disabled) {
_sendAppLaunchEvent();
}
return;
} catch (e) {
if (fallbackToMP) {
print(
'Error initializing Firebase Analytics, falling back to Measurement Protocol. \n$e');
} else {
rethrow;
}
}
}
// Use measurement protocol
var ambiUserId = userId;
const userIdField = 'userId';
if (ambiUserId == null) {
SharedPreferences prefs = await SharedPreferences.getInstance();
ambiUserId = prefs.getString(userIdField);
if (ambiUserId == null) {
ambiUserId = const Uuid().v4();
await prefs.setString(userIdField, ambiUserId);
}
}
if (measurementId != null && apiSecret != null) {
_ambilytics =
AmbilyticsSession(measurementId, apiSecret, ambiUserId, false);
}
if (_ambilytics != null || _firebaseAnalytics != null) {
_initialized = true;
if (sendAppLaunch && !_disabled) {
_sendAppLaunchEvent();
}
} else {
_initError =
'Neither Firebase Analytics nor Measurement Protocol have been initialized';
assert(true, _initError);
}
} catch (e) {
_initialized = false;
_initError = e;
assert(false, 'Can\'t init analytics due to error.\n\n$e');
}
}