flutterfire icon indicating copy to clipboard operation
flutterfire copied to clipboard

[firebase_analytics]: Analytics does not sends any events from iframe

Open NickNevzorov opened this issue 1 year ago • 12 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues.

Which plugins are affected?

Analytics

Which platforms are affected?

Web

Description

Standard code (from example):

  try {

    await Firebase.initializeApp(options: firebaseOptions);
    analytics = FirebaseAnalytics.instance;
    observer = FirebaseAnalyticsObserver(analytics: analytics!);
    await analytics?.logEvent(name: 'initialize_app', parameters: {
      'app_name': appName.toLowerCase(),
      'app_version': appVersion.toLowerCase(),
      'app_platform': getPlatformName(),
    } );

  } catch (e) {
    print('initFirebaseAnalytics: ' + e.toString());
  }

If app running from direct address, all working. If app running in iframe tag, analytics does not sends any events (and no errors reported)

Reproducing the issue

http://webshop.vsgroup.ua/ - full version http://webshop.vsgroup.ua/test.html - iframe tag

Firebase Core version

2.32.0

Flutter Version

3.22.01

Relevant Log Output

No response

Flutter dependencies

No response

Additional context and comments

No response

NickNevzorov avatar Jun 03 '24 13:06 NickNevzorov

@NickNevzorov Thank you for sharing this issue did you check if the iframe is configured in a way that might make it not work?

Also can you try to test pure js analytics in the same setup and see if it would work?

TarekkMA avatar Jun 05 '24 11:06 TarekkMA

JS version (google analytics) has SameSite cookie settings. It allows working from iframe (by default no).

Firebase Analytics in flutter has no this settings.

P.S. https://stackoverflow.com/questions/74494818/google-analytics-not-working-for-web-site-embeded-in-iframe

NickNevzorov avatar Jun 05 '24 11:06 NickNevzorov

This is google analytics, I don't guess the firebase js (which flutterfire web uses) exposes such an option

TarekkMA avatar Jun 05 '24 12:06 TarekkMA

Code of html page (with iframe) is simple:

<html>

  <body>
            <iframe src="/" style="border:none;" width="100%" height="100%"></iframe>
  </body>

</html>

I don't know what else can be provided. The same application sends messages if it works in the browser directly, but does not send messages if it works in an iframe

NickNevzorov avatar Jun 05 '24 12:06 NickNevzorov

Thank you for reporting this issue. We will be discussing internally how to address this issue.

TarekkMA avatar Jun 05 '24 13:06 TarekkMA

@NickNevzorov Can you try this PR: https://github.com/firebase/flutterfire/pull/12914

dart pub global activate flutterfire_cli 1.0.1-dev.1 --overwrite
flutterfire install git:feat/12877

Then change your call to FirebaseAnalytics using

final analytics = FirebaseAnalytics.instanceFor(app: firebaseApp, options: {cookie_flags: "max-age=7200;secure;samesite=none"});

Lyokone avatar Jun 06 '24 13:06 Lyokone

@Lyokone not compiled:

flutter pub get:

...
! firebase_analytics 11.0.0 from git https://github.com/firebase/flutterfire.git at 7e9dfd in packages/firebase_analytics/firebase_analytics (overridden)
! firebase_core 3.0.0 from git https://github.com/firebase/flutterfire.git at 7e9dfd in packages/firebase_core/firebase_core (overridden)
...

pubspec.yaml:

dependencies:
  firebase_core: 3.0.0
  firebase_analytics: 11.0.0

Code (options changed to webOptions such as in declaration instanceOf):

analytics = FirebaseAnalytics.instanceFor(app: Firebase.app(), webOptions: {"cookie_flags": "max-age=7200;secure;samesite=none"});

Compile error:

Waiting for connection from debug service on Chrome...
/C:/Users/nnevz/AppData/Local/Pub/Cache/git/flutterfire-7e9dfdd5ebc519f8b7878969a583527c5606baec/packages/firebase_analytics/firebase_analytics/lib/src/firebase_analytics.dart:25:57: Error: No named parameter with the name 'webOptions'.
        FirebaseAnalyticsPlatform.instanceFor(app: app, webOptions: webOptions);
                                                        ^^^^^^^^^^
/C:/Users/nnevz/AppData/Local/Pub/Cache/hosted/pub.dev/firebase_analytics_platform_interface-4.0.0/lib/src/platform_interface/platform_interface_firebase_analytics.dart:43:11: Context: Found this candidate, but the arguments don't match.
  factory FirebaseAnalyticsPlatform.instanceFor({required FirebaseApp app}) {
          ^^^^^^^^^^^

NickNevzorov avatar Jun 06 '24 14:06 NickNevzorov

@NickNevzorov Weird it compiles properly with

FirebaseAnalytics.instanceFor(
    app: app,
    webOptions: {'cookie_flags': 'max-age=7200;secure;samesite=none'},
  );

Can you share your pubspec.yaml? I wonder if something has not been overriden properly 🤔 You might need to override _platform_interface and _web as well, which is not supported by the CLI currently (didn't think it was needed)

Lyokone avatar Jun 06 '24 14:06 Lyokone

@Lyokone I have complex structure of projects and library. testing pubspec.yaml is simple:

name: webshop_molders
description: webshop_molders
publish_to: 'none'
version: 1.1.11+1

environment:
  sdk: ">=2.12.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter

  file: ^6.1.4

  vs_flutter:
    path: S:/projects_flutter/lib/vs_flutter/

  webshop:
    path: S:/projects_flutter/wincalc/webshop/

  firebase_core: 3.0.0
  firebase_analytics: 11.0.0

dependency_overrides:

  firebase_analytics:
    git:
      url: https://github.com/firebase/flutterfire.git
      ref: feat/12877
      path: packages/firebase_analytics/firebase_analytics
  firebase_core:
    git:
      url: https://github.com/firebase/flutterfire.git
      ref: feat/12877
      path: packages/firebase_core/firebase_core

flutter:
  uses-material-design: true
  assets:
    - resources/
    - resources/config/
    - resources/kompl/
    - resources/profsys/
    - resources/furnsys/
    - resources/lamtypes/
    - resources/panetypes/
    - resources/panetypes_filters/
    - resources/templates/
    - resources/pdf/
    - resources/inners/
    - resources/rolls_box/
    - resources/model/
    - resources/model_colors/
    - resources/model_params_1/
    - resources/model_params_2/
    - resources/model_params_2_rama/
    - resources/model_params_2_sash/
    - resources/model_params_2_sash/S701/
    - resources/model_params_2_sash/S702/
    - resources/model_params_2_sash/S703/
    - resources/model_params_2_sash/S704/
    - resources/model_params_2_sash/S705/
    - resources/model_params_2_sash/S706/
    - resources/model_params_2_sash/S707/
    - resources/model_params_2_sash/S708/
    - resources/model_params_2_sash/S709/
    - resources/model_params_2_sash/S710/
    - resources/model_params_2_sash/S711/
    - resources/model_params_2_sash/S712/
    - resources/model_params_2_sash/S713/
    - resources/model_params_2_sash/S714/
    - resources/model_params_2_sash/S715/
    - resources/model_params_2_sash/S716/
    - resources/model_params_2_sash/S717/
    - resources/model_params_2_sash/S718/
    - resources/model_params_2_sash/S719/
    - resources/model_params_2_sash/S720/
    - resources/model_params_2_sash/S721/
    - resources/model_params_2_sash/S722/
    - resources/model_params_2_sash/S723/
    - resources/model_params_2_sash/S724/
    - resources/model_params_2_sash/S725/
    - resources/model_params_2_sash/S726/
    - resources/model_params_2_sash/S727/
    - resources/model_params_2_sash/S728/
    - resources/model_params_2_sash/S729/
    - resources/model_params_2_sash/S730/
    - resources/model_params_2_sash/S731/
    - resources/model_params_2_sash/S732/
    - resources/model_params_2_sash/S733/
    - resources/model_params_2_sash/S734/
    - resources/model_params_2_sash/S735/
    - resources/model_params_2_sash/S901/
    - resources/model_params_2_sash/S902/
    - resources/model_params_2_sash/S903/
    - resources/model_params_2_sash/S904/
    - resources/model_params_2_sash/S905/
    - resources/model_params_2_sash/S906/
    - resources/model_params_2_sash/S907/
    - resources/model_params_2_sash/S908/
    - resources/model_params_2_sash/S909/
    - resources/model_params_2_sash/S910/
    - resources/model_params_2_sash/S911/
    - resources/model_params_2_sash/S912/
    - resources/model_params_4/
    - resources/model_params_11/
    - resources/model_params_11/7110/
    - resources/model_params_11/7120/
    - resources/model_params_11/7130/
    - resources/model_params_11/7140/
    - resources/model_params_11/7150/
    - resources/model_params_11/7160/
    - resources/model_params_11/7170/
    - resources/model_params_11/7310/
    - resources/model_params_11/7320/
    - resources/model_params_11/7330/
    - resources/model_params_11/7340/
    - resources/model_params_11/7350/
    - resources/model_params_11/7360/
    - resources/model_params_11/9100/
    - resources/model_params_11/9200/
    - resources/model_params_11/9300/
    - resources/model_params_11/9410/
    - resources/model_params_11/9903/
    - resources/model_params_11/9912/

Full flutter pub get:

Resolving dependencies...
Downloading packages...
  _fe_analyzer_shared 61.0.0 (70.0.0 available)
  analyzer 5.13.0 (6.5.2 available)
  archive 3.1.2 (affected by advisories: [^0], [^1], 3.6.1 available)
  audio_session 0.1.5 (0.1.19 available)
  barcode 2.2.3 (2.2.8 available)
  cached_network_image 3.1.0 (3.3.1 available)
  cached_network_image_platform_interface 1.0.0 (4.0.0 available)
  cached_network_image_web 1.0.1 (1.2.0 available)
  chewie 1.8.0 (1.8.1 available)
  convert 3.0.1 (3.1.1 available)
  csslib 0.17.0 (1.0.0 available)
  dart_style 2.3.2 (2.3.6 available)
  ffi 2.0.2 (2.1.2 available)
  file 6.1.4 (7.0.0 available)
  file_picker 5.3.0 (8.0.3 available)
! firebase_analytics 11.0.0 from git https://github.com/firebase/flutterfire.git at 7e9dfd in packages/firebase_analytics/firebase_analytics (overridden)
! firebase_core 3.0.0 from git https://github.com/firebase/flutterfire.git at 7e9dfd in packages/firebase_core/firebase_core (overridden)
  flutter_blurhash 0.6.0 (0.8.2 available)
  flutter_cache_manager 3.1.2 (3.3.2 available)
  flutter_plugin_android_lifecycle 2.0.14 (2.0.20 available)
  flutter_svg 2.0.9 (2.0.10+1 available)
  flutter_widget_from_html 0.10.6 (0.15.0 available)
  flutter_widget_from_html_core 0.10.6 (0.14.12 available)
  fluttertoast 8.2.5 (8.2.6 available)
  fwfh_cached_network_image 0.7.0+7 (0.14.2 available)
  fwfh_chewie 0.7.1+4 (0.14.7 available)
  fwfh_just_audio 0.9.0+3 (0.14.3 available)
  fwfh_webview 0.9.0+2 (0.15.0 available)
  glob 2.1.0 (2.1.2 available)
  html 0.15.0 (0.15.4 available)
  http 0.13.6 (1.2.1 available)
  http_parser 4.0.0 (4.0.2 available)
  image 4.0.17 (4.2.0 available)
  intl 0.18.1 (0.19.0 available)
  js 0.6.5 (0.7.1 available)
  just_audio 0.9.6 (0.9.38 available)
  just_audio_platform_interface 4.0.0 (4.3.0 available)
  just_audio_web 0.4.1 (0.4.11 available)
  leak_tracker 10.0.4 (10.0.5 available)
  leak_tracker_flutter_testing 3.0.3 (3.0.5 available)
  logging 1.0.2 (1.2.0 available)
  material_color_utilities 0.8.0 (0.11.1 available)
  meta 1.12.0 (1.15.0 available)
  mime 1.0.1 (1.0.5 available)
  octo_image 1.0.0+1 (2.0.0 available)
  open_file 3.3.1 (3.3.2 available)
  package_info_plus 4.2.0 (8.0.0 available)
  package_info_plus_platform_interface 2.0.1 (3.0.0 available)
  path_provider 2.0.11 (2.1.3 available)
  path_provider_android 2.0.20 (2.2.5 available)
  path_provider_linux 2.0.2 (2.2.1 available)
  path_provider_macos 2.0.2 (2.0.7 available)
  path_provider_platform_interface 2.0.1 (2.1.2 available)
  path_provider_windows 2.1.6 (2.2.1 available)
  platform 3.1.0 (3.1.4 available)
  plugin_platform_interface 2.1.4 (2.1.8 available)
  process 4.2.3 (5.0.2 available)
  pub_semver 2.1.1 (2.1.4 available)
  rxdart 0.27.1 (0.27.7 available)
  shared_preferences_android 2.2.2 (2.2.3 available)
  shared_preferences_platform_interface 2.3.1 (2.3.2 available)
  sqflite 2.0.0+3 (2.3.3+1 available)
  sqflite_common 2.0.0+2 (2.5.4 available)
  synchronized 3.0.0 (3.1.0+1 available)
  test_api 0.7.0 (0.7.2 available)
  typed_data 1.3.0 (1.3.2 available)
  universal_html 2.2.2 (2.2.4 available)
  url_launcher 6.2.6 (6.3.0 available)
  url_launcher_android 6.2.2 (6.3.3 available)
  url_launcher_platform_interface 2.3.0 (2.3.2 available)
  uuid 3.0.5 (4.4.0 available)
  vector_graphics 1.1.10+1 (1.1.11+1 available)
  vector_graphics_codec 1.1.10+1 (1.1.11+1 available)
  vector_graphics_compiler 1.1.10+1 (1.1.11+1 available)
  video_player_android 2.4.14 (2.4.16 available)
  video_player_platform_interface 6.2.1 (6.2.2 available)
  video_player_web 2.3.0 (2.3.1 available)
  vm_service 14.2.1 (14.2.3 available)
  wakelock_plus 1.2.1 (1.2.5 available)
  wakelock_plus_platform_interface 1.2.0 (1.2.1 available)
  watcher 1.0.1 (1.1.0 available)
  webview_flutter 4.5.0 (4.8.0 available)
  webview_flutter_android 3.15.0 (3.16.4 available)
  webview_flutter_platform_interface 2.9.0 (2.10.0 available)
  webview_flutter_wkwebview 3.12.0 (3.13.1 available)
  win32 4.1.4 (5.5.1 available)
  xdg_directories 0.2.0 (1.0.4 available)
  yaml 3.1.1 (3.1.2 available)
Got dependencies!
Dependencies are affected by security advisories:
  [^0]: https://github.com/advisories/GHSA-9v85-q87q-g4vg
  [^1]: https://github.com/advisories/GHSA-r285-q736-9v95
86 packages have newer versions incompatible with dependency constraints.
Try `flutter pub outdated` for more information.
Process finished with exit code 0

vs_flutter pubspec.yaml (my main flutter library):

name: vs_flutter
version: 0.0.1
publish_to: none

environment:
  sdk: ">=2.15.0 <4.0.0"

dependency_overrides:
  
  csslib: 0.17.0
  firebase_analytics:
    git:
      url: https://github.com/firebase/flutterfire.git
      ref: feat/12877
      path: packages/firebase_analytics/firebase_analytics
  firebase_core:
    git:
      url: https://github.com/firebase/flutterfire.git
      ref: feat/12877
      path: packages/firebase_core/firebase_core

dependencies:

  flutter:
    sdk: flutter

  file_picker: ^5.3.0

  fluttertoast: ^8.2.4

  firebase_core: 3.0.0 #^2.31.0 #^2.27.1 #^2.17.0
  firebase_analytics: 11.0.0 #^10.10.5 #^10.8.10 #^10.5.1

  package_info_plus: ^4.0.0
  path_provider: ^2.0.11 

  open_file: ^3.2.1

  printing: ^5.12.0 #^5.10.4
  pdf: ^3.10.8 #^3.4.2
  pdf_widget_wrapper: ^1.0.4

  screenshot: ^3.0.0 #^2.5.0 #^2.1.0 #1.3.0
  share: ^2.0.4
  shared_preferences: ^2.2.0

  universal_html: ^2.0.9
  universal_io: ^2.2.0

  xml_layout:
    path: S:/projects_flutter/lib/xml_layout-2.3.11/

  vs_dart:
    path: S:/projects_flutter/lib/vs_dart/

NickNevzorov avatar Jun 06 '24 14:06 NickNevzorov

Can you try to add in your dependency overrides (this is just to test the solution, you'll be able to use the normal versions once it's released)

dependency_overrides:
  firebase_analytics:
    git:
      url: https://github.com/firebase/flutterfire.git
      ref: feat/12877
      path: packages/firebase_analytics/firebase_analytics
  firebase_analytics_platform_interface:
    git:
      url: https://github.com/firebase/flutterfire.git
      ref: feat/12877
      path: packages/firebase_analytics/firebase_analytics_platform_interface
  firebase_analytics_web:
    git:
      url: https://github.com/firebase/flutterfire.git
      ref: feat/12877
      path: packages/firebase_analytics/firebase_analytics_web

Lyokone avatar Jun 06 '24 14:06 Lyokone

From direct address events are sending, but users count is 0 From iframe does not work

users

NickNevzorov avatar Jun 06 '24 15:06 NickNevzorov

Check it now. Users are working fine (but i upgraded flutter to 3.22.02). From iframe does not working

Working project (iframe): https://fensterundmehr.de/konfigurator/Fensterkonfigurator Flutter app site: https://moelders.fen-soft.de/

NickNevzorov avatar Jun 07 '24 10:06 NickNevzorov