Page updated Jan 16, 2024

Set up Amplify Analytics

The Analytics category enables you to collect analytics data for your App. The Analytics category comes with built-in support for Amazon Pinpoint and Amazon Kinesis (Kinesis support is currently only available in the Amplify JavaScript library). The Analytics category uses Amazon Cognito Identity pools to identify users in your App. Cognito allows you to receive data from authenticated, and unauthenticated users in your App.

Goal

To setup and configure your application with Amplify Analytics and record an analytics event.

Prerequisites

Set up Analytics backend

Run the following command in your project's root folder. The CLI will prompt configuration options for the Analytics category such as Amazon Pinpoint resource name and analytics event settings.

The Analytics category utilizes the Authentication category behind the scenes to authorize your app to send analytics events.

1amplify add analytics
1? Select an Analytics provider (Use arrow keys)
2 `Amazon Pinpoint`
3? Provide your pinpoint resource name:
4 `yourPinpointResourceName`
5? 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)
6 `Yes`

To deploy your backend, run:

1amplify push

Upon 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.

Install Amplify Libraries

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:

1dependencies {
2 // Add these lines in `dependencies`
3 implementation 'com.amplifyframework:aws-analytics-pinpoint:ANDROID_VERSION'
4 implementation 'com.amplifyframework:aws-auth-cognito:ANDROID_VERSION'
5}

Click Sync Now.

Initialize 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:

1import android.util.Log;
2import com.amplifyframework.AmplifyException;
3import com.amplifyframework.analytics.pinpoint.AWSPinpointAnalyticsPlugin;
4import com.amplifyframework.auth.cognito.AWSCognitoAuthPlugin;
5import com.amplifyframework.core.Amplify;
1Amplify.addPlugin(new AWSCognitoAuthPlugin());
2Amplify.addPlugin(new AWSPinpointAnalyticsPlugin());

Your class will look like this:

1public class MyAmplifyApp extends Application {
2 @Override
3 public void onCreate() {
4 super.onCreate();
5
6 try {
7 // Add these lines to add the AWSCognitoAuthPlugin and AWSPinpointAnalyticsPlugin plugins
8 Amplify.addPlugin(new AWSCognitoAuthPlugin());
9 Amplify.addPlugin(new AWSPinpointAnalyticsPlugin());
10 Amplify.configure(getApplicationContext());
11
12 Log.i("MyAmplifyApp", "Initialized Amplify");
13 } catch (AmplifyException error) {
14 Log.e("MyAmplifyApp", "Could not initialize Amplify", error);
15 }
16 }
17}
1import android.util.Log
2import com.amplifyframework.AmplifyException
3import com.amplifyframework.analytics.pinpoint.AWSPinpointAnalyticsPlugin
4import com.amplifyframework.auth.cognito.AWSCognitoAuthPlugin
5import com.amplifyframework.core.Amplify
1Amplify.addPlugin(AWSCognitoAuthPlugin())
2Amplify.addPlugin(AWSPinpointAnalyticsPlugin())

Your class will look like this:

1class MyAmplifyApp : Application() {
2 override fun onCreate() {
3 super.onCreate()
4
5 try {
6 // Add these lines to add the AWSCognitoAuthPlugin and AWSPinpointAnalyticsPlugin plugins
7 Amplify.addPlugin(AWSCognitoAuthPlugin())
8 Amplify.addPlugin(AWSPinpointAnalyticsPlugin())
9 Amplify.configure(applicationContext)
10
11 Log.i("MyAmplifyApp", "Initialized Amplify")
12 } catch (error: AmplifyException) {
13 Log.e("MyAmplifyApp", "Could not initialize Amplify", error)
14 }
15 }
16}
1import android.util.Log;
2import com.amplifyframework.AmplifyException;
3import com.amplifyframework.analytics.pinpoint.AWSPinpointAnalyticsPlugin;
4import com.amplifyframework.auth.cognito.AWSCognitoAuthPlugin;
5import com.amplifyframework.rx.RxAmplify;
1RxAmplify.addPlugin(new AWSCognitoAuthPlugin());
2RxAmplify.addPlugin(new AWSPinpointAnalyticsPlugin());

Your class will look like this:

1public class MyAmplifyApp extends Application {
2 @Override
3 public void onCreate() {
4 super.onCreate();
5
6 try {
7 // Add these lines to add the AWSCognitoAuthPlugin and AWSPinpointAnalyticsPlugin plugins
8 RxAmplify.addPlugin(new AWSCognitoAuthPlugin());
9 RxAmplify.addPlugin(new AWSPinpointAnalyticsPlugin());
10 RxAmplify.configure(getApplicationContext());
11
12 Log.i("MyAmplifyApp", "Initialized Amplify");
13 } catch (AmplifyException error) {
14 Log.e("MyAmplifyApp", "Could not initialize Amplify", error);
15 }
16 }
17}

To record an event, create an AnalyticsEvent and call Amplify.Analytics.recordEvent() to send it:

1import com.amplifyframework.analytics.AnalyticsEvent;
2import com.amplifyframework.core.Amplify;
1AnalyticsEvent event = AnalyticsEvent.builder()
2 .name("PasswordReset")
3 .addProperty("Channel", "SMS")
4 .addProperty("Successful", true)
5 .addProperty("ProcessDuration", 792)
6 .addProperty("UserAge", 120.3)
7 .build();
8
9Amplify.Analytics.recordEvent(event);
1import com.amplifyframework.analytics.AnalyticsEvent
2import com.amplifyframework.core.Amplify
1val event = AnalyticsEvent.builder()
2 .name("PasswordReset")
3 .addProperty("Channel", "SMS")
4 .addProperty("Successful", true)
5 .addProperty("ProcessDuration", 792)
6 .addProperty("UserAge", 120.3)
7 .build()
8
9Amplify.Analytics.recordEvent(event)
1import com.amplifyframework.analytics.AnalyticsEvent;
2import com.amplifyframework.rx.RxAmplify;
1AnalyticsEvent event = AnalyticsEvent.builder()
2 .name("PasswordReset")
3 .addProperty("Channel", "SMS")
4 .addProperty("Successful", true)
5 .addProperty("ProcessDuration", 792)
6 .addProperty("UserAge", 120.3)
7 .build();
8
9RxAmplify.Analytics.recordEvent(event);

The AWS Pinpoint event count updates in minutes after recording your event.

However, it can take upwards of 30 minutes for the event to display in the Filter section, and for its custom attributes to appear in Pinpoint.

View Analytics console

If you have not saved the link from before, you can still reach it from the terminal. Run the following command for opening the console.

1amplify console analytics

Next Steps:

Congratulations! Now that you have Analytics' backend provisioned and Analytics library installed. Check out the following links to see Amplify Analytics use cases: