firebase-analytics icon indicating copy to clipboard operation
firebase-analytics copied to clipboard

ERROR Error: Uncaught (in promise): Firebase analytics is not initialized. Make sure initializeFirebase() is called once

Open volimpiuloyal opened this issue 1 year ago • 1 comments

Describe the bug Hi there!

I'm using this plugin in my Capacitor app, it works great but every time I start the app y get the following message on console

ERROR Error: Uncaught (in promise): Firebase analytics is not initialized. Make sure initializeFirebase() is called once

To Reproduce Steps to reproduce the behavior:

  1. Open the app
  2. See the console logs

Expected behavior Don't show any error message on the console

Screenshots

Captura de pantalla 2024-05-07 a las 16 42 49

Desktop (please complete the following information):

  • OS: Mac OS 14.4.1 (23E224)
  • Browser Chrome
  • Version 124.0.6367.119

Additional context Plugin version: 4.0.0 Capacitor version: 4.0.0

volimpiuloyal avatar May 07 '24 14:05 volimpiuloyal

If you're running it in the browser while developing it then are running it on the "web" platform so you need to call this.

Alternatively you can wrap you firebase calls to confirm that this is running on device.

For my code, I don't need it on the web, its only for on device builds so I made a service that wraps my firebase calls, and skips it if its not the right platform:

import { Injectable } from '@angular/core';
import { FirebaseAnalytics } from '@capacitor-community/firebase-analytics';
import { Platform } from '@ionic/angular';

@Injectable({
  providedIn: 'root',
})
export class FirebaseAnalyticsLoggerService {
  constructor(private platform: Platform) {}

  async logEvent(name: FirebaseAnalyticsEventTypes, value: any) {
    await this.platform.ready();

    if (!this.platform.is('hybrid')) {
      console.log('Firebase Analytics aborted: not running on a device.');
      return;
    }

    try {
      const result = await FirebaseAnalytics.logEvent({ name, params: value });
      console.log('Firebase Analytics Result: ', result);
    } catch (error) {
      console.error('Firebase Analytics Error: ', error);
    }
  }
}

This is for an ionic / angular app, if you're working in something else then I believe capacitor has its own platform class.

rtpHarry avatar Jul 25 '24 10:07 rtpHarry