firebase / flutterfire

🔥 A collection of Firebase plugins for Flutter apps.
https://firebase.google.com/docs/flutter/setup
BSD 3-Clause "New" or "Revised" License
8.74k stars 3.98k forks source link

🐛 [firebase_analytics] SetCurrentScreen saving previous screen #3707

Closed tgpsantos closed 3 years ago

tgpsantos commented 4 years ago

Bug report

Describe the bug I'm trying to use setCurrentScreen method to save screen view events, and thus calculate how much time user spends on each screen. I'm running with DebugView mode.

Steps to reproduce

Steps to reproduce the behavior:

  1. Run this widget which has 2 tabs.
  2. Spend 5 seconds on the LEFT tab (tab0)
  3. Click on the RIGHT tab
  4. Spend 15 seconds there and click in the LEFT tab again
  5. Repeat 2., 3. and 4. (one time should be enough)

What you'll see is that LEFT tab screen_view is being logged with 15 seconds in the engagement_time_msec, whereas RIGHT tab is being logged with 5 seconds in the same field, and the opposite was expected to happen.

There is another strange behaviour happening. In this image you may see that user_engagement events are being automatically saved at the same time as screen_view events, but in user_engagement the engagement_time_msec and firebase_screen is matching to what's expected.

Expected behavior

I expected:

  1. screen_view engagement_time_msec to be nearly 5000 in LEFT tab (firebase_screen: /tab/tab0) and nearly 15000 in RIGHT tab (firebase_screen: /tab/tab0)
  2. screen_view and user_engagement to match the same engagement_time_msec for the same firebase_screen

Flutter doctor

Run flutter doctor and paste the output below:

Click To Expand ``` [√] Flutter (Channel stable, 1.20.4, on Microsoft Windows [Version 10.0.18363.1082], locale en-GB) [√] Android toolchain - develop for Android devices (Android SDK version 29.0.2) [√] Android Studio (version 4.0) [√] VS Code (version 1.49.1) [√] Connected device (1 available) • No issues found! ```

Flutter dependencies

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

Click To Expand ``` Dart SDK 2.9.2 Flutter SDK 1.20.4 dependencies: - camera 0.5.8+99 [flutter provider flutter_localizations flutter_svg firebase_auth cloud_firestore firebase_core flutter_spinkit google_sign_in flutter_facebook_login device_preview chewie video_player connectivity volume_control auto_size_text sensor s webview_flutter preload_page_view clipboard firebase_dynamic_links flutter_tts aud ioplayers firebase_analytics path_provider firebase_storage flutter_ffmpeg wakelock] - e2e 0.2.4+4 [flutter flutter_test] - flutter 0.0.0 [characters collection meta typed_data vector_math sky_engine] - flutter_launcher_icons 0.7.5 [image args yaml] dev dependencies: - flutter_driver 0.0.0 [file json_rpc_2 meta path web_socket_channel vm_service_clie nt webdriver flutter flutter_test fuchsia_remote_debug_protocol archive args async b oolean_selector characters charcode clock collection convert crypto fake_async intl matcher platform process pub_semver source_span stack_trace stream_channel string_sc anner sync_http term_glyph test_api typed_data vector_math] - flutter_test 0.0.0 [flutter test_api path fake_async clock stack_trace vector_math async boolean_selector characters charcode collection matcher meta source_span stre am_channel string_scanner term_glyph typed_data] - pedantic 1.9.0 transitive dependencies: - archive 2.0.13 [crypto args path] - args 1.6.0 - async 2.4.2 [collection] - audioplayers 0.16.1 [uuid path_provider flutter flutter_web_plugins] - auto_size_text 2.1.0 [flutter] - boolean_selector 2.0.0 [source_span string_scanner] - characters 1.0.0 - charcode 1.1.3 - chewie 0.9.10 [open_iconic_flutter video_player wakelock flutter] - clipboard 0.1.2+8 [flutter] - clock 1.0.1 [meta] - cloud_firestore 0.14.0+2 [flutter meta quiver firebase_core firebase_core_platform _interface cloud_firestore_platform_interface cloud_firestore_web] - cloud_firestore_platform_interface 2.0.1 [flutter meta collection firebase_core pl ugin_platform_interface] - cloud_firestore_web 0.2.0+1 [flutter flutter_web_plugins firebase http_parser meta firebase_core cloud_firestore_platform_interface js] - collection 1.14.13 - connectivity 0.4.9+2 [flutter meta connectivity_platform_interface connectivity_ma cos connectivity_for_web] - connectivity_for_web 0.3.1+2 [connectivity_platform_interface flutter_web_plugins flutter] - connectivity_macos 0.1.0+4 [flutter] - connectivity_platform_interface 1.0.6 [flutter meta plugin_platform_interface] - convert 2.1.1 [charcode typed_data] - crypto 2.1.5 [collection convert typed_data] - device_preview 0.4.8 [font_awesome_flutter http freezed_annotation json_annotation flutter flutter_localizations meta shared_preferences shared_preferences_macos] - fake_async 1.1.0 [clock collection] - file 5.2.1 [intl meta path] - firebase 7.3.0 [http http_parser js] - firebase_analytics 6.0.0 [meta flutter firebase_core firebase_analytics_web fireba se_analytics_platform_interface] - firebase_analytics_platform_interface 1.0.3 [flutter meta] - firebase_analytics_web 0.1.1 [flutter flutter_web_plugins firebase firebase_analyt ics_platform_interface meta] - firebase_auth 0.18.0+1 [meta firebase_core firebase_core_platform_interface fireba se_auth_platform_interface firebase_auth_web flutter] - firebase_auth_platform_interface 2.0.1 [flutter meta firebase_core plugin_platform _interface] - firebase_auth_web 0.3.0+1 [flutter flutter_web_plugins firebase meta http_parser i ntl firebase_core firebase_auth_platform_interface js] - firebase_core 0.5.0 [firebase_core_platform_interface flutter quiver meta firebase _core_web] - firebase_core_platform_interface 2.0.0 [flutter meta plugin_platform_interface qui ver] - firebase_core_web 0.2.0 [firebase firebase_core_platform_interface flutter flutter _web_plugins meta js] - firebase_dynamic_links 0.6.0 [flutter firebase_core] - firebase_storage 4.0.0 [flutter firebase_core] - flutter_facebook_login 3.0.0 [flutter collection] - flutter_ffmpeg 0.2.10 [flutter] - flutter_localizations 0.0.0 [flutter intl characters collection meta path typed_da ta vector_math] - flutter_spinkit 4.1.2+1 [flutter] - flutter_svg 0.18.1 [path_drawing xml vector_math meta flutter] - flutter_tts 1.2.7 [flutter flutter_web_plugins] - flutter_web_plugins 0.0.0 [flutter characters collection meta typed_data vector_ma th] - font_awesome_flutter 8.8.1 [flutter] - freezed_annotation 0.11.0+1 [collection json_annotation meta] - fuchsia_remote_debug_protocol 0.0.0 [json_rpc_2 process web_socket_channel flutter _test flutter_driver archive args async boolean_selector charcode clock collection c onvert crypto fake_async file intl matcher meta path platform pub_semver source_span stack_trace stream_channel string_scanner sync_http term_glyph test_api typed_data vector_math vm_service_client webdriver] - google_sign_in 4.5.4 [google_sign_in_platform_interface flutter meta google_sign_i n_web] - google_sign_in_platform_interface 1.1.2 [flutter meta quiver] - google_sign_in_web 0.9.1+1 [google_sign_in_platform_interface flutter flutter_web_ plugins meta js] - http 0.12.2 [http_parser path pedantic] - http_parser 3.1.4 [charcode collection source_span string_scanner typed_data] - image 2.1.14 [archive xml] - intl 0.16.1 [path] - js 0.6.2 - json_annotation 3.0.1 - json_rpc_2 2.2.1 [stack_trace stream_channel] - matcher 0.12.8 [stack_trace] - meta 1.1.8 - nested 0.0.4 [flutter] - open_iconic_flutter 0.3.0 [flutter] - path 1.7.0 - path_drawing 0.4.1+1 [vector_math meta path_parsing flutter] - path_parsing 0.1.4 [vector_math meta] - path_provider 1.6.14 [flutter path_provider_platform_interface path_provider_macos path_provider_linux] - path_provider_linux 0.0.1+2 [path xdg_directories path_provider_platform_interface flutter] - path_provider_macos 0.0.4+3 [flutter] - path_provider_platform_interface 1.0.3 [flutter meta platform plugin_platform_inte rface] - petitparser 3.0.4 [meta] - platform 2.2.1 - plugin_platform_interface 1.0.2 [meta] - preload_page_view 0.1.4 [flutter] - process 3.0.13 [file intl meta path platform] - provider 4.3.2+2 [flutter nested collection] - pub_semver 1.4.4 [collection] - quiver 2.1.3 [matcher meta] - sensors 0.4.2+4 [flutter] - shared_preferences 0.5.10 [meta flutter shared_preferences_platform_interface shar ed_preferences_linux shared_preferences_macos shared_preferences_web] - shared_preferences_linux 0.0.2+2 [file flutter meta path path_provider_linux share d_preferences_platform_interface] - shared_preferences_macos 0.0.1+10 [shared_preferences_platform_interface flutter] - shared_preferences_platform_interface 1.0.4 [meta flutter] - shared_preferences_web 0.1.2+7 [shared_preferences_platform_interface flutter flut ter_web_plugins meta] - sky_engine 0.0.99 - source_span 1.7.0 [charcode collection meta path term_glyph] - stack_trace 1.9.5 [path] - stream_channel 2.0.0 [async] - string_scanner 1.0.5 [charcode meta source_span] - sync_http 0.2.0 - term_glyph 1.1.0 - test_api 0.2.17 [async boolean_selector collection meta path source_span stack_tra ce stream_channel string_scanner term_glyph matcher] - typed_data 1.2.0 [collection] - uuid 2.2.2 [crypto convert] - vector_math 2.0.8 - video_player 0.10.12+2 [meta video_player_platform_interface video_player_web flut ter] - video_player_platform_interface 2.1.1 [flutter meta] - video_player_web 0.1.3+2 [flutter flutter_web_plugins meta video_player_platform_i nterface] - vm_service_client 0.2.6+2 [async collection json_rpc_2 pub_semver source_span stac k_trace stream_channel web_socket_channel] - volume_control 0.1.3 [flutter] - wakelock 0.1.4+2 [flutter] - web_socket_channel 1.1.0 [async crypto stream_channel] - webdriver 2.1.2 [archive matcher path stack_trace sync_http] - webview_flutter 0.3.22+1 [flutter] - xdg_directories 0.1.0 [path process flutter] - xml 4.2.0 [collection convert meta petitparser] - yaml 2.2.1 [charcode collection string_scanner source_span] ```

Thanks for all the help!

TahaTesser commented 4 years ago
code sample ```dart // Copyright 2017 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. import 'package:flutter/material.dart'; import 'package:firebase_analytics/observer.dart'; class TabsPage extends StatefulWidget { TabsPage(this.observer); final FirebaseAnalyticsObserver observer; static const String routeName = '/tab'; @override State createState() => _TabsPageState(observer); } class _TabsPageState extends State with SingleTickerProviderStateMixin, RouteAware { _TabsPageState(this.observer); final FirebaseAnalyticsObserver observer; TabController _controller; int selectedIndex = 0; final List tabs = [ const Tab(text: 'LEFT'), const Tab(text: 'RIGHT'), ]; @override void didChangeDependencies() { super.didChangeDependencies(); observer.subscribe(this, ModalRoute.of(context)); } @override void dispose() { observer.unsubscribe(this); super.dispose(); } @override void initState() { super.initState(); _controller = TabController( vsync: this, length: tabs.length, initialIndex: selectedIndex, ); _controller.addListener(() { setState(() { if (selectedIndex != _controller.index) { selectedIndex = _controller.index; _sendCurrentTabToAnalytics(); } }); }); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( bottom: TabBar( controller: _controller, tabs: tabs, ), ), body: TabBarView( controller: _controller, children: tabs.map((Tab tab) { return Center(child: Text(tab.text)); }).toList(), ), ); } @override void didPush() { _sendCurrentTabToAnalytics(); } @override void didPopNext() { _sendCurrentTabToAnalytics(); } void _sendCurrentTabToAnalytics() { observer.analytics.setCurrentScreen( screenName: '${TabsPage.routeName}/tab$selectedIndex', ); } } ```
  firebase_analytics: ^6.0.1
  firebase_core: ^0.5.0
flutter doctor -v ```bash [✓] Flutter (Channel stable, 1.22.0, on Mac OS X 10.15.7 19H2, locale en-GB) • Flutter version 1.22.0 at /Users/tahatesser/Code/flutter_stable • Framework revision d408d302e2 (7 days ago), 2020-09-29 11:49:17 -0700 • Engine revision 5babba6c4d • Dart version 2.10.0 [✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2) • Android SDK at /Users/tahatesser/Code/sdk • Platform android-30, build-tools 30.0.2 • ANDROID_HOME = /Users/tahatesser/Code/sdk • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593) • All Android licenses accepted. [✓] Xcode - develop for iOS and macOS (Xcode 12.0.1) • Xcode at /Applications/Xcode.app/Contents/Developer • Xcode 12.0.1, Build version 12A7300 • CocoaPods version 1.10.0.rc.1 [✓] Android Studio (version 4.0) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin version 50.0.1 • Dart plugin version 193.7547 • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593) [✓] VS Code (version 1.49.3) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.15.0 [✓] Connected device (1 available) • RMX2001 (mobile) • EUYTFEUSQSRGDA6D • android-arm64 • Android 10 (API 29) • No issues found! ```
russellwheatley commented 3 years ago

Hey @tgpsantos, are you still experiencing this problem? We're a few major versions removed since you opened this issue and I suspect this to be a firebase SDK issue.

tgpsantos commented 3 years ago

hey @russellwheatley, ty for the ping. I have no idea if this problem was solved because I've implemented a workaround. I will close the issue since it seems no one else complained :) Cheers!