Page updated Mar 12, 2024

Analytics

Under active development: The addOutput method for Amplify Gen 2 is under active development. The experience may change between versions of @aws-amplify/backend. Try it out and provide feedback at https://github.com/aws-amplify/amplify-backend/issues/new/choose

Amplify enables you to collect analytics data for your app. The Analytics category uses Amazon Cognito Identity pools to identify users in your app. The following is an example utilizing the AWS Cloud Development Kit (AWS CDK) to create the an Analytics resource powered by Amazon Pinpoint.

amplify/backend.ts
1import { Policy, PolicyStatement } from "aws-cdk-lib/aws-iam";
2import { CfnApp } from "aws-cdk-lib/aws-pinpoint";
3
4const backend = defineBackend({
5 auth,
6 data,
7 // additional resources
8});
9
10const analyticsStack = backend.createStack("analytics-stack");
11
12// create a Pinpoint app
13const pinpoint = new CfnApp(analyticsStack, "Pinpoint", {
14 name: "myPinpointApp",
15});
16
17// create an IAM policy to allow interacting with Pinpoint
18const pinpointPolicy = new Policy(analyticsStack, "PinpointPolicy", {
19 policyName: "PinpointPolicy",
20 statements: [
21 new PolicyStatement({
22 actions: ["mobiletargeting:UpdateEndpoint", "mobiletargeting:PutEvents"],
23 resources: [pinpoint.attrArn + "/*"],
24 }),
25 ],
26});
27
28// apply the policy to the authenticated and unauthenticated roles
29backend.auth.resources.authenticatedUserIamRole.attachInlinePolicy(pinpointPolicy);
30backend.auth.resources.unauthenticatedUserIamRole.attachInlinePolicy(pinpointPolicy);
31
32// patch the custom Pinpoint resource to the expected output configuration
33backend.addOutput({
34 Analytics: {
35 AWSPinpoint: {
36 appId: pinpoint.ref,
37 region: Stack.of(pinpoint).region,
38 },
39 },
40});

Initialize Analytics

To initialize Analytics you need to configure Amplify with Amplify.configure()

Working with Analytics

Refer to the Analytics documentation to learn how to track events and user sessions in your app.

References

Amazon Pinpoint Construct Library