facebook twitter hatena line email

Flutter/firebase/Analytics

提供: 初心者エンジニアの簡易メモ
2020年1月27日 (月) 11:19時点におけるAdmin (トーク | 投稿記録)による版

移動: 案内検索

firebaseから設定ファイルをDL

  1. https://console.firebase.google.com
  2. androidとiosのプロジェクトを作成し、google-services.jsonと、GoogleService-Info.plistをDL

インストール

pubspec.yaml

dependencies:
  firebase_core: ^0.4.2
  firebase_analytics: ^5.0.9

androidの準備

google-services.jsonをandroid/appの下へ

android/build.gradle

buildscript {
  repositories {
    google()  // Google's Maven repository
  }
  dependencies {
    classpath 'com.google.gms:google-services:3.2.1'
  }
}
allprojects {
  repositories {
    google()  // Google's Maven repository
  }
}

android/app/build.gradle のファイルの末尾へapply~を追加

dependencies {
}
apply plugin: 'com.google.gms.google-services'  // Google Play services Gradle plugin

iosの準備

xcode でios/Runner.xcworkspaceを開きRunner/RunnerへGoogleService-Info.plistファイルをドラッグする。

サンプル

起動処理にnavigatorObserversを以下を追加する。

app.dart

import 'package:firebase_analytics/firebase_analytics.dart';
import 'package:firebase_analytics/observer.dart';
FirebaseAnalytics analytics = FirebaseAnalytics();
return new MaterialApp(
    title: 'hogeproject',
    home: SplashScreen(),
    navigatorObservers: [
        FirebaseAnalyticsObserver(analytics: analytics),
    ],
);

公式サンプル

https://github.com/FirebaseExtended/flutterfire/tree/master/packages/firebase_analytics/firebase_analytics/example/lib

イベント

  static FirebaseAnalytics analytics = FirebaseAnalytics();
  Future<void> _sendAnalyticsEvent() async {
    await analytics.logEvent(
      name: 'test_event',
      parameters: <String, dynamic>{
        'string': 'string',
        'int': 42,
        'long': 12345678910,
        'double': 42.0,
        'bool': true,
      },
    );
    setMessage('logEvent succeeded');
  }

iosで以下エラーが出る場合

Terminating app due to uncaught exception 'com.firebase.core', reason: '`[FIRApp configure];` (`FirebaseApp.configure()` in Swift) could not find a valid GoogleService-Info.plist in your project. Please download one from https://console.firebase.google.com/.'

xcodeプロジェクトを開きRunner/Runnerへドラッグする。