Flutter Web:Flutter Web应用程序是否支持Firebase Analytics?

时间:2019-12-19 12:04:45

标签: flutter flutter-web

我们可以在Flutter Web应用程序中实施Firebse Analytics吗?如果不是,那么还有其他选择吗?

请帮助我,您对此有所了解。谢谢

4 个答案:

答案 0 :(得分:13)

从firebase 7.0.0(https://pub.dev/packages/firebase/versions/7.0.0)开始,您可以在Flutter Web应用程序中使用分析。

以下是步骤:

  1. 在您的主机页面中初始化Firebase
<body>
  <!-- Initialize Firebase -->
  <script src="/__/firebase/7.6.1/firebase-app.js"></script>
  <script src="/__/firebase/7.6.1/firebase-analytics.js"></script>
  <script src="/__/firebase/init.js"></script>

  <!-- Initialize app -->
  <script src="main.dart.js" type="application/javascript"></script>
</body>
  1. 导入firebase软件包
import 'package:firebase/firebase.dart';
  1. 此时,您可以通过analytics()访问Analytics对象。如果您想自动发送页面浏览量,可以引入一个路线观察者
class AnalyticsRouteObserver extends RouteObserver<PageRoute<dynamic>> {

  final Analytics analytics;

  AnalyticsRouteObserver({@required this.analytics});

  void _sendPageView(PageRoute<dynamic> route) {
    var pageName = route.settings.name;
    if (null != analytics) {
      analytics.setCurrentScreen(pageName);
    } else {
      print('pageName: $pageName');
    }
  }

  @override
  void didPush(Route<dynamic> route, Route<dynamic> previousRoute) {
    super.didPush(route, previousRoute);
    if (route is PageRoute) {
      _sendPageView(route);
    }
  }

  @override
  void didReplace({Route<dynamic> newRoute, Route<dynamic> oldRoute}) {
    super.didReplace(newRoute: newRoute, oldRoute: oldRoute);
    if (newRoute is PageRoute) {
      _sendPageView(newRoute);
    }
  }

  @override
  void didPop(Route<dynamic> route, Route<dynamic> previousRoute) {
    super.didPop(route, previousRoute);
    if (previousRoute is PageRoute && route is PageRoute) {
      _sendPageView(previousRoute);
    }
  }
}
  1. 最后在您的应用中注册路线观察者
import 'dart:js' as js;
...
Widget build(BuildContext context) {
    return MaterialApp(
      navigatorObservers: [AnalyticsRouteObserver(analytics: js.context.hasProperty('firebase')?analytics():null)],
...
}

答案 1 :(得分:7)

首次导入Firebase


from sklearn.neighbors import NearestNeighbors
X = df.to_numpy()
nbrs = NearestNeighbors(n_neighbors=3)
nbrs.fit(X)

def findsuccess(id):
    neighbors_f_id = nbrs.kneighbors_graph([X[id]]).indices
    print  ('Neighbors of id', neighbors_f_id)

更新index.html

import 'package:firebase/firebase.dart' as Firebase;

并记录事件

<body>
  <script src="https://www.gstatic.com/firebasejs/7.9.1/firebase-app.js"></script>
  <script src="https://www.gstatic.com/firebasejs/7.9.1/firebase-analytics.js"></script>

  <script>
    var firebaseConfig = {
      apiKey: "AIzaxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
      authDomain: "xxxxxxxxx.firebaseapp.com",
      databaseURL: "https://xxxxxxxx.firebaseio.com",
      projectId: "xxxxxx",
      storageBucket: "xxxxxxx.appspot.com",
      messagingSenderId: "xxxxxxxxxxxxx",
      appId: "x:xxxxxxxxxxxx:web:xxxxxxxxxxxxxxxx",
      measurementId: "G-xxxxxxxxx"
    };
    firebase.initializeApp(firebaseConfig);
  </script>

  <script src="main.dart.js" type="application/javascript"></script>
</body>

答案 2 :(得分:2)

我用过这个包 - https://pub.dev/packages/firebase_analytics

一件重要的事情 - 编辑 index.html,就像文件 web/index.html 中的 example 一样

很少有脚本需要添加到 <head>

  <!-- The core Firebase JS SDK is always required and must be listed first -->
  <script src="https://www.gstatic.com/firebasejs/7.15.1/firebase-app.js"></script>

  <!-- TODO: Add SDKs for Firebase products that you want to use
      https://firebase.google.com/docs/web/setup#available-libraries -->
  <script src="https://www.gstatic.com/firebasejs/7.15.1/firebase-analytics.js"></script>

  <script>
    // Your web app's Firebase configuration
    var firebaseConfig = {
      apiKey: "AIzaSyAgUhHU8wSJgO5MVNy95tMT07NEjzMOfz0",
      authDomain: "react-native-firebase-testing.firebaseapp.com",
      databaseURL: "https://react-native-firebase-testing.firebaseio.com",
      projectId: "react-native-firebase-testing",
      storageBucket: "react-native-firebase-testing.appspot.com",
      messagingSenderId: "448618578101",
      appId: "1:448618578101:web:772d484dc9eb15e9ac3efc",
      measurementId: "G-0N1G9FLDZE"
    };
    // Initialize Firebase
    firebase.initializeApp(firebaseConfig);
    firebase.analytics();
  </script>

答案 3 :(得分:0)

不。你不能我一直在搜索1个月,到目前为止,您无法做到这一点。