Amplify Analyticsのセットアップ
Analytics カテゴリを使用すると、アプリのアナリティクスデータを収集できます。Analytics カテゴリは Amazon Pinpoint と Amazon Kinesis に組み込みサポートを備えています (Kinesis サポートは現在 Amplify JavaScript ライブラリでのみ利用可能です)。Analytics カテゴリは Amazon Cognito Identity プールを使用してアプリのユーザーを識別します。Cognito を使用すると、アプリの認証済みユーザーと認証されていないユーザーからデータを受け取ることができます。
目標
Amplify Analytics を使用してアプリケーションをセットアップおよび設定し、アナリティクスイベントを記録します。
前提条件
- Install and configure Amplify CLI
- An Android application targeting Android API level 16 (Android 4.1) or above
- For a full example of creating Android project, please follow the project setup walkthrough
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, amplifyconfiguration.json should 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 ライブラリをインストール
Expand Gradle Scripts, open build.gradle (Module :app). You will already have configured Amplify by following the steps in the Project Setup walkthrough.
Add Analytics by adding these libraries into the dependencies block:
dependencies { // Add these lines in `dependencies` implementation 'com.amplifyframework:aws-analytics-pinpoint:ANDROID_V1_VERSION' implementation 'com.amplifyframework:aws-auth-cognito:ANDROID_V1_VERSION'}Click Sync Now.
Amplify Analytics を初期化
To initialize the Amplify Auth and Analytics categories you call Amplify.addPlugin() method for each category. To complete initialization call Amplify.configure().
Add the following code to your onCreate() method in your application class:
Amplify.addPlugin(new AWSCognitoAuthPlugin());Amplify.addPlugin(new AWSPinpointAnalyticsPlugin(this));Your class will look like this:
public class MyAmplifyApp extends Application { @Override public void onCreate() { super.onCreate();
try { // Add these lines to add the AWSCognitoAuthPlugin and AWSPinpointAnalyticsPlugin plugins Amplify.addPlugin(new AWSCognitoAuthPlugin()); Amplify.addPlugin(new AWSPinpointAnalyticsPlugin(this)); Amplify.configure(getApplicationContext());
Log.i("MyAmplifyApp", "Initialized Amplify"); } catch (AmplifyException error) { Log.e("MyAmplifyApp", "Could not initialize Amplify", error); } }}Amplify.addPlugin(AWSCognitoAuthPlugin())Amplify.addPlugin(AWSPinpointAnalyticsPlugin(this))Your class will look like this:
class MyAmplifyApp : Application() { override fun onCreate() { super.onCreate()
try { // Add these lines to add the AWSCognitoAuthPlugin and AWSPinpointAnalyticsPlugin plugins Amplify.addPlugin(AWSCognitoAuthPlugin()) Amplify.addPlugin(AWSPinpointAnalyticsPlugin(this)) Amplify.configure(applicationContext)
Log.i("MyAmplifyApp", "Initialized Amplify") } catch (error: AmplifyException) { Log.e("MyAmplifyApp", "Could not initialize Amplify", error) } }}RxAmplify.addPlugin(new AWSCognitoAuthPlugin());RxAmplify.addPlugin(new AWSPinpointAnalyticsPlugin(this));Your class will look like this:
public class MyAmplifyApp extends Application { @Override public void onCreate() { super.onCreate();
try { // Add these lines to add the AWSCognitoAuthPlugin and AWSPinpointAnalyticsPlugin plugins RxAmplify.addPlugin(new AWSCognitoAuthPlugin()); RxAmplify.addPlugin(new AWSPinpointAnalyticsPlugin(this)); RxAmplify.configure(getApplicationContext());
Log.i("MyAmplifyApp", "Initialized Amplify"); } catch (AmplifyException error) { Log.e("MyAmplifyApp", "Could not initialize Amplify", error); } }}To record an event, create an AnalyticsEvent and call Amplify.Analytics.recordEvent() to send it:
AnalyticsEvent event = AnalyticsEvent.builder() .name("PasswordReset") .addProperty("Channel", "SMS") .addProperty("Successful", true) .addProperty("ProcessDuration", 792) .addProperty("UserAge", 120.3) .build();
Amplify.Analytics.recordEvent(event);val event = AnalyticsEvent.builder() .name("PasswordReset") .addProperty("Channel", "SMS") .addProperty("Successful", true) .addProperty("ProcessDuration", 792) .addProperty("UserAge", 120.3) .build()
Amplify.Analytics.recordEvent(event)AnalyticsEvent event = AnalyticsEvent.builder() .name("PasswordReset") .addProperty("Channel", "SMS") .addProperty("Successful", true) .addProperty("ProcessDuration", 792) .addProperty("UserAge", 120.3) .build();
RxAmplify.Analytics.recordEvent(event);アナリティクスコンソールを表示
以前のリンクを保存していない場合でも、ターミナルから到達できます。以下のコマンドを実行してコンソールを開きます。
amplify console analytics次のステップ:
おめでとうございます! Analytics バックエンドをプロビジョニングし、Analytics ライブラリをインストールしました。Amplify Analytics のユースケースについては、以下のリンクをご覧ください。