Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 [firebase_analytics] Not working on Android with manual or Dart only initialization #11271

Closed
asterion2nd opened this issue Jul 12, 2023 · 5 comments
Labels
resolution: solution-provided A solution has been provided in the issue. type: bug Something isn't working

Comments

@asterion2nd
Copy link

asterion2nd commented Jul 12, 2023

Bug report

Describe the bug
Firebase Analytics does not work on Android after running flutterfire configure and either using
Firebase.initializeApp()
or
Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform)

I then set
final analyticsInstance = FirebaseAnalytics.instance;
And send events using:
await analyticsInstance?.logEvent(name: eventName, parameters: parameters,);

No error messages shows up. At the same time, Analytics works on iOS and Firebase Cloud Messaging works on both Android and iOS.

Steps to reproduce

Steps to reproduce the behavior:

  1. Add to pubspec.yaml:
    firebase_core: ^2.14.0
    firebase_messaging: ^14.6.4
    firebase_analytics: ^10.4.3

  2. flutterfire configure

  3. Select project

  4. Check if android/app/google-services.json was created

  5. Check if lib/firebase_options.dart was created

  6. Check if android/build.gradle has classpath 'com.google.gms:google-services:4.3.10'

  7. Check if android/app/build.gradle has apply plugin: 'com.google.gms.google-services'

  8. Try to initialize Firebase using either
    Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
    or
    Firebase.initializeApp();

  9. Connect android device

  10. Run $ adb shell setprop debug.firebase.analytics.app <package_id>

  11. Build and run the app

  12. Check Debug View in Firebase

  13. Check if Analytics data from Android deviced get saved after releasing the app.

Expected behavior

Debug View in Firebase should record events from an Android device after installing the app using flutter run or "Run without debugging in VS Code"

Analytics property should record data from Android devices running the latest version published to Play Store (works for iOS devices).


Additional context

pubspec.yaml:
environment:
sdk: ">=2.12.0 <3.0.0"
firebase_core: ^2.14.0
firebase_messaging: ^14.6.4
firebase_analytics: ^10.4.3

android/app/build.gradle:
$ flutterfire configure added the following:
// START: FlutterFire Configuration
apply plugin: 'com.google.gms.google-services'
// END: FlutterFire Configuration

which initially looked like:
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

Then I tried moving apply plugin: 'com.google.gms.google-services' to the bottom.

android/build.gradle:
dependencies {
classpath 'com.android.tools.build:gradle:7.1.2'
// START: FlutterFire Configuration
classpath 'com.google.gms:google-services:4.3.10'
// END: FlutterFire Configuration
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}

Debug View
Executed
$ adb shell setprop debug.firebase.analytics.app <package_id>

And went to Debug View on Firebase. Nothing shows up.

The app is already in production but no Android data get saved.

Also, "android/app/google-services.json" is correct (FCM works on Android) and I double checked all values against my Firebase project.

I tried all workarounds on these threads and nothing seems to work:


Flutter doctor

Run flutter doctor and paste the output below:

Click To Expand
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.3.3, on macOS 12.6.6 21G646 darwin-x64, locale en-US)
[✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0-rc2)
[✓] Xcode - develop for iOS and macOS (Xcode 14.2)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2021.3)
[✓] Android Studio (version 4.1)
[✓] VS Code (version 1.80.0)
[✓] VS Code (version 1.65.0)
[✓] Connected device (2 available)
[✓] HTTP Host Availability

• No issues found!

Flutter dependencies

Run flutter pub deps -- --style=compact and paste the output below:

Click To Expand
**pubspec.yaml:**
environment:
  sdk: ">=2.12.0 <3.0.0"
firebase_core: ^2.14.0
firebase_messaging: ^14.6.4
firebase_analytics: ^10.4.3

- firebase_analytics_platform_interface 3.6.3 [_flutterfire_internals firebase_core flutter meta plugin_platform_interface]
- firebase_analytics_web 0.5.4+3 [_flutterfire_internals firebase_analytics_platform_interface firebase_core firebase_core_web flutter flutter_web_plugins js]
- firebase_core_platform_interface 4.8.0 [collection flutter flutter_test meta plugin_platform_interface]
- firebase_core_web 2.6.0 [firebase_core_platform_interface flutter flutter_web_plugins js meta]
- firebase_messaging_platform_interface 4.5.3 [_flutterfire_internals firebase_core flutter meta plugin_platform_interface]
- firebase_messaging_web 3.5.3 [_flutterfire_internals firebase_core firebase_core_web firebase_messaging_platform_interface flutter flutter_web_plugins js meta]

@asterion2nd asterion2nd added Needs Attention This issue needs maintainer attention. type: bug Something isn't working labels Jul 12, 2023
@asterion2nd
Copy link
Author

asterion2nd commented Jul 12, 2023

Additional info:
If I use http requests provided by "Measurement Protocol (Google Analytics 4)":

import 'package:http/http.dart' as http;
import 'dart:convert' show jsonDecode;
import 'package:firebase_analytics/firebase_analytics.dart'
    show FirebaseAnalytics;

final url = "https://www.google-analytics.com/mp/collect?api_secret=<api_secret>&firebase_app_id=<app_id>

final body = {
      "app_instance_id": await FirebaseAnalytics.instance.appInstanceId,
      "events": [
        {
          "name": "test_event",
        }
      ]
    };
    
    await http.post(
      Uri.parse(url),
      body: jsonEncode(body),
    );

where
<app_id> matches with mobilesdk_app_id in google-services.json
and <api_secret> is generated through Analytics: Admin > Data Streams > choose your stream > Measurement Protocol > Create.

Events show up in real-time in Analytics.

@darshankawar darshankawar added the triage Issue is currently being triaged. label Jul 13, 2023
@darshankawar
Copy link

@asterion2nd
You are not on latest version of the framework. Can you upgrade to latest (3.10.6) and run your scenario to check if the behavior still persist or not ?

@darshankawar darshankawar added blocked: customer-response Waiting for customer response, e.g. more information was requested. and removed Needs Attention This issue needs maintainer attention. labels Jul 13, 2023
@asterion2nd
Copy link
Author

asterion2nd commented Jul 13, 2023

@darshankawar
Thanks for taking a look at this.

It still does not work.

I ran $flutter upgrade, re-installed dart pub global activate flutterfire_cli, ran $ flutterfire configure again after deleting all files it creates and removing its modifications to gradle.build files.

I also checked both Debug View and the realtime page of Analytics.

Also, I printed out the FirebaseApp instance and all parameters match with those in google-sevices.json.

Future main() async {
WidgetsFlutterBinding.ensureInitialized();

  final x = Platform.isIOS
      ? await Firebase.initializeApp(
          options: DefaultFirebaseOptions.currentPlatform)
      : await Firebase.initializeApp();

  print(x.options.asMap);
  ...
  }
  
{apiKey: ***, appId: ***, messagingSenderId: ***, projectId: ***, authDomain: null, databaseURL: https://***, storageBucket: ***.appspot.com, measurementId: null, trackingId: null, deepLinkURLScheme: null, androidClientId: null, iosClientId: null, iosBundleId: null, appGroupId: null}
$ flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.10.6, on macOS 12.6.6 21G646 darwin-x64, locale en-US)
[✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0-rc2)
[✓] Xcode - develop for iOS and macOS (Xcode 14.2)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2021.3)
[✓] Android Studio (version 4.1)
[✓] VS Code (version 1.80.0)
[✓] VS Code (version 1.65.0)
[✓] Connected device (3 available)
[✓] Network resources

• No issues found!

android/app/build.gradle:

...
apply plugin: 'com.android.application'
// START: FlutterFire Configuration
apply plugin: 'com.google.gms.google-services'
// END: FlutterFire Configuration
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
...

android/build.gradle:

dependencies {
        classpath 'com.android.tools.build:gradle:7.1.2'
        // START: FlutterFire Configuration
        classpath 'com.google.gms:google-services:4.3.10'
        // END: FlutterFire Configuration
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }

@google-oss-bot google-oss-bot added Needs Attention This issue needs maintainer attention. and removed blocked: customer-response Waiting for customer response, e.g. more information was requested. labels Jul 13, 2023
@darshankawar
Copy link

Thanks for the update and patience. Can you try the plugin example with your setup and see if using it, you still get the reported behavior or not ?

@darshankawar darshankawar added blocked: customer-response Waiting for customer response, e.g. more information was requested. and removed Needs Attention This issue needs maintainer attention. labels Jul 14, 2023
@asterion2nd
Copy link
Author

asterion2nd commented Jul 18, 2023

@darshankawar
Thanks for your help. Based on the example you shared, I adjusted all sdk and package versions and other small details.
It's working know.

Please go ahead and close this issue.

@google-oss-bot google-oss-bot added Needs Attention This issue needs maintainer attention. and removed blocked: customer-response Waiting for customer response, e.g. more information was requested. labels Jul 18, 2023
@darshankawar darshankawar added resolution: solution-provided A solution has been provided in the issue. and removed Needs Attention This issue needs maintainer attention. triage Issue is currently being triaged. labels Jul 19, 2023
@firebase firebase locked and limited conversation to collaborators Aug 19, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
resolution: solution-provided A solution has been provided in the issue. type: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants