skip to Main Content

Firebase analytics problem in JS

Hello I am unable to send lo event in my html and js website.

My code

<html>
    <head>
        <title>Test</title>
    </head>
    <body>


<script type="module">
  import { initializeApp } from "https://www.gstatic.com/firebasejs/9.15.0/firebase-app.js";
  import { getAnalytics } from "https://www.gstatic.com/firebasejs/9.15.0/firebase-analytics.js";

  const firebaseConfig = {
    apiKey: "my data",
    authDomain: "my data",
    databaseURL: "my data",
    projectId: "my data",
    storageBucket: "my data",
    messagingSenderId: "my data",
    appId: "my data",
    measurementId: "my data"
  };

  // Initialize Firebase
  const app = initializeApp(firebaseConfig);
  const analytics = getAnalytics(app);
  
 analytics.logEvent('test_event', { name: 'hiii'});
 
</script>
</body>
</html>

Error

Uncaught TypeError: analytics.logEvent is not a function
    at event.php:25:12

So now how can i solve it?
Maybe i am missing something..

2

Answers


  1. You may check and initialize Firebase Analytics like this:

    const firebase = require('firebase/app')
    require('firebase/analytics')
    
    firebase.initializeApp(config);
    firebase.analytics();
    
    Login or Signup to reply.
  2. Since you are using the modular implementation, you should import logEvent from the analytics module

    import { getAnalytics, logEvent } from "https://www.gstatic.com/firebasejs/9.15.0/firebase-analytics.js";
    

    and send the analytics instance as the first parameter LogEvent

    <html>
        <head>
            <title>Test</title>
        </head>
        <body>
        <script type="module">
          import { initializeApp } from "https://www.gstatic.com/firebasejs/9.15.0/firebase-app.js";
          import { getAnalytics, logEvent } from "https://www.gstatic.com/firebasejs/9.15.0/firebase-analytics.js";
        
          const firebaseConfig = {
            apiKey: "my data",
            authDomain: "my data",
            databaseURL: "my data",
            projectId: "my data",
            storageBucket: "my data",
            messagingSenderId: "my data",
            appId: "my data",
            measurementId: "my data"
          };
        
          // Initialize Firebase
          const app = initializeApp(firebaseConfig);
          const analytics = getAnalytics(app);
          
         logEvent(analytics ,'test_event', { name: 'hiii'});
         
        </script>
        </body>
    </html>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search