Set up Amplify Analytics
Amplify enables you to collect analytics data for your app. In order to use Analytics, you will enable Amazon Kinesis or Amazon Pinpoint using the AWS Cloud Development Kit (AWS CDK). 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.
Prerequisites
- An Android application targeting Android API level 24 (Android 7.0) or above
Set up Analytics backend
Use the AWS CDK to create an analytics resource powered by Amazon Pinpoint.
import { defineBackend } from "@aws-amplify/backend"import { auth } from "./auth/resource";import { data } from "./data/resource";import { Policy, PolicyStatement } from "aws-cdk-lib/aws-iam";import { CfnApp } from "aws-cdk-lib/aws-pinpoint";import { Stack } from "aws-cdk-lib/core";
const backend = defineBackend({ auth, data, // additional resources});
const analyticsStack = backend.createStack("analytics-stack");
// create a Pinpoint appconst pinpoint = new CfnApp(analyticsStack, "Pinpoint", { name: "myPinpointApp",});
// create an IAM policy to allow interacting with Pinpointconst pinpointPolicy = new Policy(analyticsStack, "PinpointPolicy", { policyName: "PinpointPolicy", statements: [ new PolicyStatement({ actions: ["mobiletargeting:UpdateEndpoint", "mobiletargeting:PutEvents"], resources: [pinpoint.attrArn + "/*"], }), ],});
// apply the policy to the authenticated and unauthenticated rolesbackend.auth.resources.authenticatedUserIamRole.attachInlinePolicy(pinpointPolicy);backend.auth.resources.unauthenticatedUserIamRole.attachInlinePolicy(pinpointPolicy);
// patch the custom Pinpoint resource to the expected output configurationbackend.addOutput({ analytics: { amazon_pinpoint: { app_id: pinpoint.ref, aws_region: Stack.of(pinpoint).region, } },});
Install Amplify Libraries
Expand Gradle Scripts, open build.gradle.kts (Module :app). You will already have configured Amplify by following the steps in the quickstart guide.
Add Analytics by adding these libraries into the dependencies block:
android { compileOptions { // Support for Java 8 features isCoreLibraryDesugaringEnabled = true sourceCompatibility = JavaVersion.VERSION_1_8 targetCompatibility = JavaVersion.VERSION_1_8 }}
dependencies { // Amplify API dependencies implementation("com.amplifyframework:aws-analytics-pinpoint:ANDROID_VERSION") implementation("com.amplifyframework:aws-auth-cognito:ANDROID_VERSION") // ... other dependencies coreLibraryDesugaring("com.android.tools:desugar_jdk_libs:2.0.3")}
Click Sync Now.
Initialize Amplify Analytics
Import and load the configuration file in your app. It's recommended you add the Amplify configuration step to your app's root entry point.
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;import com.amplifyframework.core.configuration.AmplifyOutputs;
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(AmplifyOutputs.fromResource(R.raw.amplify_outputs), 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.Amplifyimport com.amplifyframework.core.configuration.AmplifyOutputs
Amplify.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(AmplifyOutputs.fromResource(R.raw.amplify_outputs), 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.core.configuration.AmplifyOutputs;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(AmplifyOutputs.fromResource(R.raw.amplify_outputs), getApplicationContext());
Log.i("MyAmplifyApp", "Initialized Amplify"); } catch (AmplifyException error) { Log.e("MyAmplifyApp", "Could not initialize Amplify", error); } }}
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: