capacitor-community/firebase-analytics

Native implementation not called if imports done in Capacitor v3 style

phfeustel opened this issue ยท 7 comments

Describe the bug
When I use the Capacitor v3 style of imports and run it as iOS app, not the native implementation but the web implementation is called.

To Reproduce
Import the plugin and use it like

import { FirebaseAnalytics } from "@capacitor-community/firebase-analytics";

FirebaseAnalytics.logEvent({
  name: "select_content",
  params: {
    content_type: "image",
    content_id: "P12453",
    items: [{ name: "Kittens" }],
  },
});

Then I get the error message: Firebase analytics is not initialized. Make sure initializeFirebase() is called once

Additionally, I debugged a bit in Xcode and see that the plugin is registered(CapacitorBridge.registerPlugin() successfully and also exported via JSExport.exportJS().

If I use the "old" style, everything works fine, even when using the same capacitor version; it is marked as deprecated though:

import "@capacitor-community/firebase-analytics";
import { Plugins } from '@capacitor/core';

const {FirebaseAnalytics} = Plugins;

FirebaseAnalytics.logEvent({
  name: "select_content",
  params: {
    content_type: "image",
    content_id: "P12453",
    items: [{ name: "Kittens" }],
  },
});

Unfortunately, I didn't find any clue what's going wrong.

Expected behavior
The native implementation should be called, also when using the Capacitor v3 style imports.

Smartphone (please complete the following information):

  • Device: iPhone6s, Xcode Simulator
  • OS: iOS 14.7.1, iOS 15
  • Browser: Ionic WkWebview

Additional context
For other plugins like @capacitor/share the new style imports are working just fine.

Versions used:

    "@capacitor-community/firebase-analytics": "^0.3.3",
    "@capacitor/core": "3.2.3",
    "@capacitor/ios": "3.2.3",
    "@capacitor/share": "^1.0.4",
    "@ionic/react": "^5.5.0",

I can confirm that I get the same error. I recently migrated from capacitor v2 to v3, followed the guide about importing capacitor plugins like import { FirebaseAnalytics } from "@capacitor-community/firebase-analytics"; and I get Firebase analytics is not initialized. Make sure initializeFirebase() is called once on web.

Hi, I am experiencing the same issue when migrating to capacitor 3 and moving to the v3 style imports on macOS safari

Versions being used:

"@capacitor-community/firebase-analytics": "^1.0.0",
"@capacitor/app": "^1.0.6",
"@capacitor/browser": "^1.0.6",
"@capacitor/core": "^3.3.1",

@bellispmo I had to downgrade to "@capacitor-community/firebase-analytics": "^0.3.3" and import it like that

import '@capacitor-community/firebase-analytics';
import { Plugins } from '@capacitor/core';

until they fix this. I hope this will help.

@bellispmo I had to downgrade to "@capacitor-community/firebase-analytics": "^0.3.3" and import it like that

import '@capacitor-community/firebase-analytics';
import { Plugins } from '@capacitor/core';

until they fix this. I hope this will help.

@JohnGeorgiadis Thanks, I have switched to v0.3.3 and it is working now.

When this can be fixed? I used Capacitor 3 and only followed the updated documentation, but looks analysis not working it shows me there are 0 users online even tracking screen names not working. I am testing it on iOS Simulator.

Update: The plugin works well on a real device, I am using Capacitor v3, "@capacitor-community/firebase-analytics" v1.0.0, and using the steps from plugin updated documentation.

For those of you having issues, I was able to solve it by ensuring the FirebaseAnalytics calls are async.

import { Device } from '@capacitor/device';
import { FirebaseAnalytics } from '@capacitor-community/firebase-analytics';

Device.getInfo().then(async device => {
  window.device = device;

  // Firebase
  if (process.env.NODE_ENV === "production") {
    // Only need to initialize on web
    if (!Capacitor.isNativePlatform()) {
      await FirebaseAnalytics.initializeFirebase(firebaseConfig);
    }

    await FirebaseAnalytics.setCollectionEnabled({
      enabled: true,
    });
  }
});

This is no longer an issue in version 4 of the plugin.