question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[assigned] FireBaseAnalytics.setCurrentScreen() on Fragment may not be called

See original GitHub issue

Overview (Required)

(日本語で失礼します)

一部のFragmentでFireBaseAnalytics.setCurrentScreenが呼ばれないケースがあります。 そのFragmentとは、AllSessionsFragmentSearchSessionsFragmentと、ViewPagerの中の最初のFragmentです。 一例としてSearchSessionsFragmentのライフサイクルは以下のようになります。

  1. アプリを起動
  2. bottom navigationで検索を選択
  3. SearchSessionsFragmentインスタンス生成
  4. setUserVisibleHint()が呼ばれるが ログは送れない
  5. onAttach()が呼ばれる
  6. onActivityCreated()が呼ばれる(fireBaseAnalyticsインスタンスの初期化)
  7. トピックタブ(SearchTopicsFragment)に移動、トピックタブのログは送れる
  8. 再度セッションタブ(SearchSessionsFragment)に移動、この時のログは送れる

現状setUserVisibleHint()でログを送っていますが、 上記の 3 の時点だとfireBaseAnalytics はまだnullなので実行されません。 たとえfireBaseAnalyticsの初期化タイミングを早めて実行できたとしても、まだこのときはAttachされておらずgetActivity()がnullなのでsetCurrentScreen()を実行できません。

代わりにViewPager.OnPageChangeListener を使って試そうとしましたが、あまりいいやり方にできなかったのでissueにしました 🤔

Links

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:14 (13 by maintainers)

github_iconTop GitHub Comments

3reactions
eneimcommented, Jan 28, 2018

Assigned されたが、コメントだけさせてください 😄, setUserVisibleHint(true) でしかログを送信するようであれば、FragmentStatePagerAdapterを実装する時に、FragmentStatePagerAdapter#setPrimaryItem() をOverrideして、何とかできるかもしれません 😄 (setUserVisibleHint()に「true」が渡されたのはこのメソッドでしか呼ばれないわけですね)

そして fireBaseAnalytics のインスタンスは事前で生成すべきかと思います。SearchFragmentで生成して、PagerAdapterで使うなど 😄

3reactions
kakajikacommented, Jan 28, 2018

少し冗長かもしれませんが、Rxの withLatestFrom を使ってイベントの待ち合わせをするという手もあります。

private val onActivityCreatedStream = PublishSubject.create<Activity>()
private val setUserVisibleHintStream = PublishSubject.create<Boolean>()

override fun onCreate(...) {
    super.onCreate(...)

    // onActivityCreated() が呼ばれた状態で、setUserVisibleHint() が呼ばれるたびに実行
    setUserVisibleHintStream
            .withLatestFrom(onActivityCreatedStream)
            .filter { (isVisibleToUser, _) -> isVisibleToUser }
            .subscribe { (isVisibleToUser, activity) ->
                fireBaseAnalytics?.setCurrentScreen(activity, null, this::class.java.simpleName)
            }
            .addTo(...)
}

override fun onActivityCreated(...) {
    super.onActivityCreated(...)
    activity?.let { onActivityCreatedStream.onNext(it) }
}

override fun setUserVisibleHint(isVisibleToUser: Boolean) {
    super.setUserVisibleHint(isVisibleToUser)
    setUserVisibleHintStream.onNext(isVisibleToUser)
}

ご参考までに 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Firebase Analytics not tracking the name of my app screens
setCurrentScreen() event should be pushed from onResume() method only. Otherwise it will not show name of screen in report, it will only show ......
Read more >
FirebaseAnalytics - Google
If your app does not use a distinct Activity for each screen, you should call ... the current Activity changes or a new...
Read more >
New API for manually tracking screen views in Google Analytics
The latest Google Analytics for Firebase SDK release now makes it possible for you to log the screen_view event manually for both Android ......
Read more >
[GA4] Automatically collected events - Firebase Help
Automatically collected events are triggered by basic interactions with your app and/or site (as indicated under the event name in the table below)....
Read more >
Firebase Analytics quick start - Rock and Null
A common approach is to call the screen tracking method, passing the fragment class name as the screen name on onResume() . If...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found