Baranaba
Baranaba

Reputation: 81

firebase.analytics is not a function

I am trying to add analytics to an already existing web app. I initialize Firebase Analytics as described here.


const firebase = require('firebase');
firebase.initializeApp(config);
firebase.analytics();

I get this error TypeError: firebase.analytics is not a function

What could the problem be?

Upvotes: 8

Views: 13391

Answers (7)

Apatoallo
Apatoallo

Reputation: 55

import firebase from '@react-native-firebase/app'

firebase.analytics()

firebase.analytics() is updated to analytics() if you're using react native you have to import analytics

use

import analytics from '@react-native-firebase/analytics'

analytics()

instead importing from firebase

you need to run npm install or yarn add @react-native-firebase/analytics'

and make sure you run cd ios && pod install

Upvotes: 0

fatihyildizhan
fatihyildizhan

Reputation: 8832

You may check and initialize Firebase Analytics v9+ like this. You may also upgrade your old versions to newer SDK.

import { initializeApp } from "firebase/app";
import { getAnalytics, logEvent, isSupported } from "firebase/analytics";

// Find this config on your firebase project overview
const firebaseConfig = { ... } // your config

// Initialize Firebase
let app = null;
let analytics = null;

isSupported().then((result) => {
    if (result) {
        app = initializeApp(firebaseConfig);
        analytics = getAnalytics(app);
    }
})

// simple event
logEvent(analytics, 'your_event_name');

// firebase defined events like page_view etc.
logEvent(analytics, 'page_view', `your_event_parameter`)

Note: I got an error on Docker build, that's why I call the logEvent like this:

isSupported().then((result) => {
  if (result) {
    logEvent(analytics, title);
  }
})

Upvotes: 7

krsna
krsna

Reputation: 4333

In my case, this worked. I'm using firebase version ^8.1.2

import firebase from 'firebase/app';
import 'firebase/analytics';
import 'firebase/auth';

...

// Initialize Firebase
firebase.initializeApp(firebaseConfig);
firebase.analytics();

Upvotes: 5

Gökhan Geyik
Gökhan Geyik

Reputation: 313

Try to firebase.analytics.Analytics;

const firebase = require('firebase/app')
require('firebase/analytics')
    
firebase.initializeApp(config);
firebase.analytics.Analytics; 

Upvotes: 2

Alejo Dev
Alejo Dev

Reputation: 2586

You have to import 'firebase/analytics' for side effects:

const firebase = require('firebase/app')
require('firebase/analytics')

firebase.initializeApp(config);
firebase.analytics();

That worked for me

Upvotes: 8

Kaushal
Kaushal

Reputation: 123

Analytics is not supported in Node. Check the below link for support on environments. https://firebase.google.com/support/guides/environments_js-sdk

Upvotes: 3

Yu-Min Peng
Yu-Min Peng

Reputation: 91

First, make sure you initialize firebase instance correctly, it won't work if you run it in server-side, then official providers a method to check whether your environment supports analytics or not, call firebase.analytics.isSupported(), this is a promise, so the correct way to use it is

var firebaseConfig... // your config

var firebase.initializeApp(firebaseConfig);

firebase.analytics.isSupported().then((isSupported) => {
    if (isSupported) {
      analytics = firebase.analytics();
    }
})

Upvotes: 6

Related Questions