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.
Amplify Flutter requires a minimum target platform for iOS (13.0), Android (API level 24), and macOS (10.15). Refer to Flutter's supported deployment platforms when targeting Web, Windows, or Linux.
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
In your Flutter project directory, open pubspec.yaml. Add Analytics by adding these libraries into your dependencies block:
dependencies: amplify_analytics_pinpoint: ^2.0.0 amplify_auth_cognito: ^2.0.0 amplify_flutter: ^2.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 'amplify_outputs.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]);}
Make sure that the amplify_outputs.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 'amplify_outputs.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(amplifyConfig); } 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 'amplify_outputs.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(amplifyConfig); } 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: