Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
230 views
in Technique[技术] by (71.8m points)

javascript - 如何启动Firebase Analytics在Web上工作?(How to initiate Firebase Analytics to work on Web?)

I have initiated a FB SDK in the project following the documentation :

(我已经按照文档在项目中启动了FB SDK:)

Getting started with Analytics is easy.

(使用Analytics(分析)入门很容易。)

Just add the Firebase SDK to your new or existing app, and data collection begins automatically.

(只需将Firebase SDK添加到您的新应用或现有应用中,数据收集就会自动开始。)

You can view analytics data in the Firebase console within hours.

(您可以在几个小时内在Firebase控制台中查看分析数据。)

Here is my main.js

(这是我的main.js)

 // Production Firebase configuration
  firebaseConfig = {
    apiKey: <AKI_LEY>,
    authDomain: <DOMAIN>,
    databaseURL: <DB_URL>,
    projectId: <PROJECT_ID>,
    storageBucket: "",
    messagingSenderId: <SENDER_ID>,
    appId: <APP_ID>,
    measurementId: <TRACKING_ID>
  };

firebase.initializeApp(firebaseConfig);
// missing firebase.analytics(); but calling this in main will throw exception. Calling this in index.html will also throw exception as initializeApp has to be called before.

This allows the application to work with firebase Storage and Auth services but analytics is still not registering anything.

(这允许该应用程序使用Firebase存储和身份验证服务,但分析仍未注册任何内容。)

What am I missing?

(我想念什么?)

EDIT

(编辑)

After reading more documentation and double checking my index, I added the following <scripts> but with no prevail.

(在阅读了更多文档并仔细检查了我的索引之后,我添加了以下<scripts>但没有成功。)

<!-- Firebase App (the core Firebase SDK) is always required and must be listed first -->
<script src="https://www.gstatic.com/firebasejs/7.5.0/firebase-app.js"></script>
<script src='https://www.gstatic.com/firebasejs/7.5.0/firebase-analytics.js'></script>

The problem Default webpack firebase import doesn't have firebase analytics libraries, that's why you need to import using CDN.

(问题默认的WebPack火力进口没有火力分析库,这就是为什么你需要使用CDN进口。)

After import in index I still need to run firebase.analytics() but that has to follow firebase.initializeApp().

(导入索引后,我仍然需要运行firebase.analytics(),但是必须遵循firebase.initializeApp()。)

How can one call both in the main.js so that init functions are not separated between index and main?

(如何在main.js中同时调用两者,以使init函数不会在index和main之间分开?)

Also my main includes more logic to decide on the firebase config setup (have multiple projects) so I cannot just move all the firebase actions into index.html.

(我的主要工具还包括更多逻辑来决定Firebase配置设置(具有多个项目),因此我不能仅将所有Firebase操作移到index.html中。)

Thanks.

(谢谢。)

  ask by KasparTr translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For Analytics to work in your Firebase app, you'll need to include the SDK.

(为了让Analytics(分析)可以在Firebase应用程式中运作,您需要加入SDK。)

A common way to do this is with:

(常用的方法是:)

<script src='https://www.gstatic.com/firebasejs/7.5.0/firebase-analytics.js'>

But of course any other way of including the necessary file is fine too.

(但是,当然,包含必要文件的任何其他方式也可以。)

Be sure to check what the latest version of the SDK is, under available libraries in the documentation.

(请确保在文档中的可用库下检查SDK的最新版本。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...