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

Page updated May 10, 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.

  • A Flutter application targeting Flutter SDK >=3.3.0 with Amplify libraries integrated

    The following are also required, depending on which platforms you are targeting:

    • An iOS configuration targeting at least iOS 13.0 and XCode version >=13.2
    • An Android configuration targeting at least Android API level 24 (Android 7.0) or above
    • Any browser supported by Flutter for Web (you can check the list of supported browsers here)
    • Any Windows OS meeting Flutter minimums
    • macOS version 10.15 or higher
    • Any Ubuntu Linux distribution meeting Flutter minimums

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

In your Flutter project directory, open pubspec.yaml. Add Analytics by adding these libraries into your dependencies block:

environment:
sdk: '>=2.18.0 <4.0.0'
dependencies:
amplify_analytics_pinpoint: ^1.0.0
amplify_auth_cognito: ^1.0.0
amplify_flutter: ^1.0.0

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.

Add the Auth and Analytics plugin, along with any other plugins you may have added as described in the Project Setup section;

import 'package:amplify_analytics_pinpoint/amplify_analytics_pinpoint.dart';
import 'package:amplify_auth_cognito/amplify_auth_cognito.dart';
import 'package:amplify_flutter/amplify_flutter.dart';
import 'package:flutter/material.dart';
import 'amplifyconfiguration.dart';
Future<void> _configureAmplify() async {
// Add Pinpoint and Cognito Plugins, and any other plugins you want to use
final analyticsPlugin = AmplifyAnalyticsPinpoint();
final authPlugin = AmplifyAuthCognito();
await Amplify.addPlugins([analyticsPlugin, authPlugin]);
}

When running your app on MacOS you will need to enable keychain sharing in Xcode, as described in the Project setup guide.

Make sure that the amplifyconfiguration.dart file generated in the project setup is included and sent to Amplify.configure:

import 'package:amplify_analytics_pinpoint/amplify_analytics_pinpoint.dart';
import 'package:amplify_auth_cognito/amplify_auth_cognito.dart';
import 'package:amplify_flutter/amplify_flutter.dart';
import 'package:flutter/material.dart';
import 'amplifyconfiguration.dart';
Future<void> _configureAmplify() async {
// ...
await Amplify.addPlugins([analyticsPlugin, authPlugin]);
// Once Plugins are added, configure Amplify
// Note: Amplify can only be configured once.
try {
await Amplify.configure(outputs);
} on AmplifyAlreadyConfiguredException {
safePrint(
'Tried to reconfigure Amplify; this can occur when your app restarts on Android.',
);
}
}

Your class will look like this:

import 'package:amplify_flutter/amplify_flutter.dart';
import 'package:amplify_analytics_pinpoint/amplify_analytics_pinpoint.dart';
import 'package:amplify_auth_cognito/amplify_auth_cognito.dart';
import 'package:flutter/material.dart';
import 'amplifyconfiguration.dart';
Future<void> _configureAmplify() async {
// Add any Amplify plugins you want to use
final analyticsPlugin = AmplifyAnalyticsPinpoint();
final authPlugin = AmplifyAuthCognito();
await Amplify.addPlugins([analyticsPlugin, authPlugin]);
// Once Plugins are added, configure Amplify
// Note: Amplify can only be configured once.
try {
await Amplify.configure(outputs);
} on AmplifyAlreadyConfiguredException {
safePrint(
'Tried to reconfigure Amplify; this can occur when your app restarts on Android.',
);
}
}
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await _configureAmplify();
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}): super(key: key);
// ...
}

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