Amplify has re-imagined the way frontend developers build fullstack applications. Develop and deploy without the hassle.

Page updated May 21, 2024

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.

amplify/backend.ts
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 app
const pinpoint = new CfnApp(analyticsStack, "Pinpoint", {
name: "myPinpointApp",
});
// create an IAM policy to allow interacting with Pinpoint
const 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 roles
backend.auth.resources.authenticatedUserIamRole.attachInlinePolicy(pinpointPolicy);
backend.auth.resources.unauthenticatedUserIamRole.attachInlinePolicy(pinpointPolicy);
// patch the custom Pinpoint resource to the expected output configuration
backend.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:

app/build.gradle.kts
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:

Before calling the Amplify.configure function, make sure to either download the amplify_outputs.json file from the console, or generate it with the following command:

Terminal
npx ampx generate outputs --app-id <app-id> --branch main --out-dir app/src/main/res/raw

Next, be sure the file you generated or downloaded is in the appropriate resource directory for your application (for example, app/src/main/res/raw) in your Android project. Otherwise, you will not be able to compile your application.

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.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(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:

References

Amazon Pinpoint Construct Library