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 カテゴリは、アプリがアナリティクスイベントを送信することを許可するために、バックグラウンドで認証カテゴリを使用します。
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: ^1.0.0 amplify_auth_cognito: ^1.0.0 amplify_flutter: ^1.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); // ...}アナリティクスコンソールを表示
以前のリンクを保存していない場合でも、ターミナルから到達できます。以下のコマンドを実行してコンソールを開きます。
amplify console analytics次のステップ:
おめでとうございます! Analytics バックエンドをプロビジョニングし、Analytics ライブラリをインストールしました。Amplify Analytics のユースケースについては、以下のリンクをご覧ください。