From 5cd9d0ab10b5859c2029d08d978fb4ac07658395 Mon Sep 17 00:00:00 2001 From: airkjw Date: Sat, 13 Jun 2026 12:00:45 +0900 Subject: [PATCH] feat(analytics): add FirebaseAnalyticsBackend (firebase wiring pt.1) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds firebase_core + firebase_analytics and a FirebaseAnalyticsBackend that adapts the existing AnalyticsBackend interface to GA4. Kept in its own file so the typed AnalyticsService and DebugAnalyticsBackend stay free of the firebase dependency (unit tests never pull in platform channels). Not yet wired into main() — that needs lib/firebase_options.dart from flutterfire configure (owner step). All 161 tests still green. Co-Authored-By: Claude Fable 5 --- lib/services/firebase_analytics_backend.dart | 33 ++++++++++++ pubspec.lock | 56 ++++++++++++++++++++ pubspec.yaml | 2 + 3 files changed, 91 insertions(+) create mode 100644 lib/services/firebase_analytics_backend.dart diff --git a/lib/services/firebase_analytics_backend.dart b/lib/services/firebase_analytics_backend.dart new file mode 100644 index 0000000..d48777f --- /dev/null +++ b/lib/services/firebase_analytics_backend.dart @@ -0,0 +1,33 @@ +import 'dart:async'; + +import 'package:firebase_analytics/firebase_analytics.dart'; + +import 'analytics_service.dart'; + +/// Routes typed events to Firebase / GA4. Wired in main() for release builds +/// only; debug builds keep the console logger so development traffic never +/// pollutes production analytics. +/// +/// Lives in its own file so [AnalyticsService] and [DebugAnalyticsBackend] +/// stay free of the firebase_analytics dependency (and so unit tests of the +/// typed event surface never pull in platform channels). +class FirebaseAnalyticsBackend implements AnalyticsBackend { + FirebaseAnalyticsBackend([FirebaseAnalytics? analytics]) + : _analytics = analytics ?? FirebaseAnalytics.instance; + + final FirebaseAnalytics _analytics; + + @override + void logEvent(String name, Map params) { + // Fire-and-forget: analytics must never block or break gameplay, and a + // logging failure (offline, quota) is swallowed rather than surfaced. + unawaited( + _analytics + .logEvent( + name: name, + parameters: params.isEmpty ? null : params, + ) + .catchError((Object _) {}), + ); + } +} diff --git a/pubspec.lock b/pubspec.lock index cf6931a..ee4d828 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -9,6 +9,14 @@ packages: url: "https://pub.dev" source: hosted version: "85.0.0" + _flutterfire_internals: + dependency: transitive + description: + name: _flutterfire_internals + sha256: "0d1f0adfabbab9f46a1a80ce84a4d8b852b6e4dbf53ce413b30e0cf7d3631b71" + url: "https://pub.dev" + source: hosted + version: "1.3.72" analyzer: dependency: transitive description: @@ -185,6 +193,54 @@ packages: url: "https://pub.dev" source: hosted version: "7.0.1" + firebase_analytics: + dependency: "direct main" + description: + name: firebase_analytics + sha256: "1f68d8a30265149035e9bb0b73962e43f3bf2debef4a7ae2d1d588361d5858a9" + url: "https://pub.dev" + source: hosted + version: "12.4.2" + firebase_analytics_platform_interface: + dependency: transitive + description: + name: firebase_analytics_platform_interface + sha256: "65f97fb923f036d487aa95be99eab90e0f8f636e4783c94deeb52c0e6f5dbae7" + url: "https://pub.dev" + source: hosted + version: "6.0.2" + firebase_analytics_web: + dependency: transitive + description: + name: firebase_analytics_web + sha256: ec49a95c6abaadbd6a4327cc2680d911f86e62fdf6f2c96a3a326603410e2abf + url: "https://pub.dev" + source: hosted + version: "0.6.1+8" + firebase_core: + dependency: "direct main" + description: + name: firebase_core + sha256: ec46a100a560d3bd5f97f2d89ba7492cb09b6dd0a4a28753d1258f360d6bd9f9 + url: "https://pub.dev" + source: hosted + version: "4.10.0" + firebase_core_platform_interface: + dependency: transitive + description: + name: firebase_core_platform_interface + sha256: "4a120366dbf7d5a8ee9438978530b664b855728fb8dcc3a201017660817e555b" + url: "https://pub.dev" + source: hosted + version: "7.0.1" + firebase_core_web: + dependency: transitive + description: + name: firebase_core_web + sha256: "5ad1be848692ec148f2d6a8ad2a3838cb852ea5f3c9e6479a7afce479e1854f8" + url: "https://pub.dev" + source: hosted + version: "3.8.0" fixnum: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index d120b03..cd828c2 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -42,6 +42,8 @@ dependencies: http: ^1.6.0 crypto: ^3.0.7 path_provider: ^2.1.5 + firebase_core: ^4.10.0 + firebase_analytics: ^12.4.2 dev_dependencies: flutter_test: