Search code examples
firebaseionic-frameworkionic3angularfireangularfire2

How to set firebase analytics events with ionic 3


I'm trying to get my analytics events with firebase but all I get is only session_start event. I'm using the firebase plugin for ionic.

This is my code:

constructor(public navCtrl: NavController, private nav: NavController,
            private firebaseAnalytics: FirebaseAnalytics) {
    this.firebaseAnalytics.setEnabled(true);
    this.firebaseAnalytics.logEvent('home_page_viewed', {page: "dashboard"})
        .then((res: any) => console.log(res))
        .catch((error: any) => console.error(error));
    }

So, I expect to see in firebase console an event that called 'home_page_viewed' but I don't see such event. All I see is only session_start event.

Anyone knows what should I do?


Solution

  • You probably have lazy-loading enabled in your application and a page view does not always create a new page object. So the construct method is NOT called for every page view.

    Further the constructor method of the page class is not the correct place to log a page-view.

    You should move the logEvent call to one of Ionic's lifecycle events.

    public ionViewDidEnter()
    {
      this.firebaseAnalytics.logEvent('home_page_viewed', {page: 'dashboard'})
        .then((res: any) => console.log(res))
        .catch((error: any) => console.error(error));
    }