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 24 (Android 7.0) or above
- For a full example of creating Android project, please follow the project setup walkthrough
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, 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_VERSION' implementation 'com.amplifyframework:aws-auth-cognito:ANDROID_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:
import android.util.Log;import com.amplifyframework.AmplifyException;import com.amplifyframework.analytics.pinpoint.AWSPinpointAnalyticsPlugin;import com.amplifyframework.auth.cognito.AWSCognitoAuthPlugin;import com.amplifyframework.core.Amplify;Amplify.addPlugin(new AWSCognitoAuthPlugin());Amplify.addPlugin(new AWSPinpointAnalyticsPlugin());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()); Amplify.configure(getApplicationContext());
Log.i("MyAmplifyApp", "Initialized Amplify"); } catch (AmplifyException error) { Log.e("MyAmplifyApp", "Could not initialize Amplify", error); } }}import android.util.Logimport com.amplifyframework.AmplifyExceptionimport com.amplifyframework.analytics.pinpoint.AWSPinpointAnalyticsPluginimport com.amplifyframework.auth.cognito.AWSCognitoAuthPluginimport com.amplifyframework.core.AmplifyAmplify.addPlugin(AWSCognitoAuthPlugin())Amplify.addPlugin(AWSPinpointAnalyticsPlugin())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()) Amplify.configure(applicationContext)
Log.i("MyAmplifyApp", "Initialized Amplify") } catch (error: AmplifyException) { Log.e("MyAmplifyApp", "Could not initialize Amplify", error) } }}import android.util.Log;import com.amplifyframework.AmplifyException;import com.amplifyframework.analytics.pinpoint.AWSPinpointAnalyticsPlugin;import com.amplifyframework.auth.cognito.AWSCognitoAuthPlugin;import com.amplifyframework.rx.RxAmplify;RxAmplify.addPlugin(new AWSCognitoAuthPlugin());RxAmplify.addPlugin(new AWSPinpointAnalyticsPlugin());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()); 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:
import com.amplifyframework.analytics.AnalyticsEvent;import com.amplifyframework.core.Amplify;AnalyticsEvent event = AnalyticsEvent.builder() .name("PasswordReset") .addProperty("Channel", "SMS") .addProperty("Successful", true) .addProperty("ProcessDuration", 792) .addProperty("UserAge", 120.3) .build();
Amplify.Analytics.recordEvent(event);import com.amplifyframework.analytics.AnalyticsEventimport com.amplifyframework.core.Amplifyval event = AnalyticsEvent.builder() .name("PasswordReset") .addProperty("Channel", "SMS") .addProperty("Successful", true) .addProperty("ProcessDuration", 792) .addProperty("UserAge", 120.3) .build()
Amplify.Analytics.recordEvent(event)import com.amplifyframework.analytics.AnalyticsEvent;import com.amplifyframework.rx.RxAmplify;AnalyticsEvent event = AnalyticsEvent.builder() .name("PasswordReset") .addProperty("Channel", "SMS") .addProperty("Successful", true) .addProperty("ProcessDuration", 792) .addProperty("UserAge", 120.3) .build();
RxAmplify.Analytics.recordEvent(event);Analytics コンソールを表示
以前にリンクを保存していない場合でも、ターミナルからアクセスできます。コンソールを開くには、以下のコマンドを実行します。
amplify console analytics次のステップ:
おめでとうございます!Analytics のバックエンドをプロビジョニングし、Analytics ライブラリをインストールしました。Amplify Analytics のユースケースについては、以下のリンクをチェックしてください: