Amplify Analytics をセットアップする
Analytics カテゴリでは、アプリ用の分析データを収集できます。Analytics カテゴリは Amazon Pinpoint と Amazon Kinesis(Kinesis サポートは現在のところ Amplify JavaScript ライブラリでのみ利用可能)の組み込みサポートで提供されています。Analytics カテゴリは Amazon Cognito Identity プールを使用してアプリ内のユーザーを識別します。Cognito により、アプリ内の認証済みユーザーと非認証ユーザーの両方からデータを受け取ることができます。
目的
Amplify Analytics でアプリケーションをセットアップおよび設定し、分析イベントを記録します。
前提条件
Amplify Flutter requires a minimum target platform for iOS (13.0), Android (API level 24), and macOS (10.15). Additional setup is required for some target platforms. Please see the platform setup guide for more details on platform specific setup.
Analytics バックエンドのセットアップ
プロジェクトのルートフォルダで以下のコマンドを実行します。CLI は Analytics カテゴリの設定オプション(Amazon Pinpoint リソース名と分析イベント設定など)のプロンプトを表示します。
Analytics カテゴリは、アプリが分析イベントを送信することを認可するために、バックグラウンドで Authentication カテゴリを利用しています。
amplify add analytics? Select an Analytics provider (Use arrow keys) `Amazon Pinpoint`? Provide your pinpoint resource name: `yourPinpointResourceName`? Apps need authorization to send analytics events. Do you want to allow guests and unauthenticated users to send analytics events? (we recommend you allow this when getting started) `Yes`バックエンドをデプロイするには、以下を実行します:
amplify pushUpon completion, you can see the result as the following that is going to be providing you a link to reach your analytics data:
✔ All resources are updated in the cloud
Pinpoint URL to track events https://<YOUR-PROJECT-RELATED-URL>/analytics/overviewIn addition, amplifyconfiguration.dart will be updated to reference provisioned backend analytics resources. Note that these files should already be a part of your project if you followed the Project setup walkthrough.
Amplify ライブラリをインストール
In your Flutter project directory, open pubspec.yaml.
You will already have configured Amplify by following the steps in the project setup.
Add Analytics by adding these libraries into your dependencies block:
dependencies: amplify_analytics_pinpoint: ^2.0.0 amplify_auth_cognito: ^2.0.0 amplify_flutter: ^2.0.0Amplify Analytics を初期化
Add the Auth and Analytics plugin, along with any other plugins you may have added as described in the Project Setup section;
import 'package:amplify_analytics_pinpoint/amplify_analytics_pinpoint.dart';import 'package:amplify_auth_cognito/amplify_auth_cognito.dart';import 'package:amplify_flutter/amplify_flutter.dart';import 'package:flutter/material.dart';
import 'amplifyconfiguration.dart';
Future<void> _configureAmplify() async { // Add Pinpoint and Cognito Plugins, and any other plugins you want to use final analyticsPlugin = AmplifyAnalyticsPinpoint(); final authPlugin = AmplifyAuthCognito(); await Amplify.addPlugins([analyticsPlugin, authPlugin]);}Make sure that the amplifyconfiguration.dart file generated in the project setup is included and sent to Amplify.configure:
import 'package:amplify_analytics_pinpoint/amplify_analytics_pinpoint.dart';import 'package:amplify_auth_cognito/amplify_auth_cognito.dart';import 'package:amplify_flutter/amplify_flutter.dart';import 'package:flutter/material.dart';
import 'amplifyconfiguration.dart';
Future<void> _configureAmplify() async { // ... await Amplify.addPlugins([analyticsPlugin, authPlugin]);
// Once Plugins are added, configure Amplify // Note: Amplify can only be configured once. try { await Amplify.configure(amplifyconfig); } on AmplifyAlreadyConfiguredException { safePrint( 'Tried to reconfigure Amplify; this can occur when your app restarts on Android.', ); }}Your class will look like this:
import 'package:amplify_flutter/amplify_flutter.dart';import 'package:amplify_analytics_pinpoint/amplify_analytics_pinpoint.dart';import 'package:amplify_auth_cognito/amplify_auth_cognito.dart';import 'package:flutter/material.dart';
import 'amplifyconfiguration.dart';
Future<void> _configureAmplify() async { // Add any Amplify plugins you want to use final analyticsPlugin = AmplifyAnalyticsPinpoint(); final authPlugin = AmplifyAuthCognito(); await Amplify.addPlugins([analyticsPlugin, authPlugin]);
// Once Plugins are added, configure Amplify // Note: Amplify can only be configured once. try { await Amplify.configure(amplifyconfig); } on AmplifyAlreadyConfiguredException { safePrint( 'Tried to reconfigure Amplify; this can occur when your app restarts on Android.', ); }}
Future<void> main() async { WidgetsFlutterBinding.ensureInitialized(); await _configureAmplify(); runApp(const MyApp());}
class MyApp extends StatefulWidget { const MyApp({Key? key}): super(key: key); // ...}Analytics コンソールを表示
以前にリンクを保存していない場合でも、ターミナルからアクセスできます。コンソールを開くには、以下のコマンドを実行します。
amplify console analytics次のステップ:
おめでとうございます!Analytics のバックエンドをプロビジョニングし、Analytics ライブラリをインストールしました。Amplify Analytics のユースケースについては、以下のリンクをチェックしてください: