Search code examples
iosgoogle-analyticsviewdidloadviewwillappear

Where should a navigation-based iOS app track pageviews - viewDidLoad or viewWillAppear?


I have a navigation based application. All of the Google Analytics examples I see have page view tracking in viewDidLoad.

I'm wondering if this makes the most sense. If I want to learn about the true behavior of my users, shouldn't it be in viewWillAppear? What is the rationale behind putting it in viewDidLoad? Is there consensus on the proper way to track pageviews?

Thanks


Solution

  • Neither. UINavigationController can send a delegate messages to inform it of navigation through the hierarchy. Check out the UINavigationControllerDelegate protocol for more information. You can handle everything from one delegate instead of modifying all of your view controllers.

    Having said that, if you did need to do it from the individual view controllers for some reason, you'd need to use the *Appear methods. Using the *Load methods is clearly wrong - that's for loading, not for showing, hence the name.

    For another point of reference, Flurry Analytics uses the UINavigationControllerDelegate protocol.