facebook twitter hatena line email

「Flutter/firebase/Analytics」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(firebaseのanalyticsのサンプル)
(同じ利用者による、間の18版が非表示)
行3: 行3:
 
#androidとiosのプロジェクトを作成し、google-services.jsonと、GoogleService-Info.plistをDL
 
#androidとiosのプロジェクトを作成し、google-services.jsonと、GoogleService-Info.plistをDL
  
==設定ファイルの設定==
+
==インストール==
#google-services.jsonをandorid/appの下へ
+
#GoogleService-Info.plistをios/Runnerの下へ
+
 
+
==firebaseのanalyticsをインストール==
+
 
pubspec.yaml
 
pubspec.yaml
 
<pre>
 
<pre>
行14: 行10:
 
   firebase_analytics: ^5.0.9
 
   firebase_analytics: ^5.0.9
 
</pre>
 
</pre>
 +
 +
==androidの準備==
 +
google-services.jsonをandroid/appの下へ
 +
 +
android/build.gradle
 +
<pre>
 +
buildscript {
 +
  repositories {
 +
    google()  // Google's Maven repository
 +
  }
 +
  dependencies {
 +
    classpath 'com.google.gms:google-services:3.2.1'
 +
  }
 +
}
 +
allprojects {
 +
  repositories {
 +
    google()  // Google's Maven repository
 +
  }
 +
}
 +
</pre>
 +
 +
android/app/build.gradle のファイルの末尾へapply~を追加
 +
<pre>
 +
dependencies {
 +
}
 +
apply plugin: 'com.google.gms.google-services'  // Google Play services Gradle plugin
 +
</pre>
 +
 +
==iosの準備==
 +
xcode でios/Runner.xcworkspaceを開きRunner/RunnerへGoogleService-Info.plistファイルをドラッグする。
  
 
==サンプル==
 
==サンプル==
行32: 行58:
 
</pre>
 
</pre>
  
==イベント処理サンプル==
+
==公式サンプル==
 +
https://github.com/FirebaseExtended/flutterfire/tree/master/packages/firebase_analytics/firebase_analytics/example/lib
 +
 
 +
==イベント==
 
<pre>
 
<pre>
observer.analytics.setCurrentScreen(
+
  static FirebaseAnalytics analytics = FirebaseAnalytics();
   screenName: '${TabsPage.routeName}/tab$selectedIndex',
+
   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');
 +
  }
 +
</pre>
 +
 
 +
==iosで以下エラーが出る場合==
 +
<pre>
 +
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/.'
 
</pre>
 
</pre>
 +
xcodeプロジェクトを開きRunner/Runnerへドラッグする。

2020年1月27日 (月) 11:19時点における版

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へドラッグする。