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.

Native implementation not called if imports done in Capacitor v3 style

See original GitHub issue

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",

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:6

github_iconTop GitHub Comments

3reactions
BuffMcBigHugecommented, Jan 4, 2022

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,
    });
  }
});
1reaction
JohnGeorgiadiscommented, Nov 15, 2021

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Capacitor and Ionic native status-bar - Stack Overflow
Yes, my question was why they have given Native plugins with the Capacitor project if it is not compatible. That is a bug....
Read more >
Browser Capacitor Plugin API
The Browser API provides the ability to open an in-app browser and subscribe to browser events.
Read more >
How to create project specific local plugin | Ionic 6 | Capacitor
Source code:- https://github.com/Aashu-Dubey/youtube/tree/ionic/local_pluginCapacitor Documentation:- https://capacitorjs.com/docsℹ️ ...
Read more >
Hardware Back Button for Capacitor & Cordova on Android ...
This is because Capacitor and Cordova expose additional features that are not exposed in a normal web browser. For example, closing overlays and...
Read more >
Advanced Concepts - NativeScript Docs
This approach will not work if application.android.ts requires external modules. ... Calling native APIs that expect Objective-C classes or objects is easy ...
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