Page updated Jan 19, 2024

Set up Amplify Analytics

Amplify Flutter v0 is now in Maintenance Mode until July 19th, 2024. This means that we will continue to include updates to ensure compatibility with backend services and security. No new features will be introduced in v0.

Please use the latest version (v1) of Amplify Flutter to get started.

If you are currently using v0, follow these instructions to upgrade to v1.

The Analytics category enables you to collect analytics data for your App. The Analytics category comes with built-in support for Amazon Pinpoint and Amazon Kinesis (Kinesis support is currently only available in the Amplify JavaScript library). 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.

Goal

To setup and configure your application with Amplify Analytics and record an analytics event.

Prerequisites

  • Install and configure Amplify CLI
  • A Flutter application targeting Flutter SDK >= 2.10.0 (stable version) with Amplify libraries integrated
    • An iOS configuration targeting at least iOS 11.0
    • An Android configuration targeting at least Android API level 21 (Android 5.0) or above
    • For a full example please follow the project setup walkthrough

Set up Analytics backend

Run the following command in your project's root folder. The CLI will prompt configuration options for the Analytics category such as Amazon Pinpoint resource name and analytics event settings.

The Analytics category utilizes the Authentication category behind the scenes to authorize your app to send analytics events.

1amplify add analytics
1? Select an Analytics provider (Use arrow keys)
2 `Amazon Pinpoint`
3? Provide your pinpoint resource name:
4 `yourPinpointResourceName`
5? Apps need authorization to send analytics events. Do you want to allow guests and unauthenticated users to send analytics events? (we recommend you allow this when getting started)
6 `Yes`

To deploy your backend, run:

1amplify push

Upon completion, you can see the result as the following that is going to be providing you a link to reach your analytics data:

1✔ All resources are updated in the cloud
2
3Pinpoint URL to track events https://<YOUR-PROJECT-RELATED-URL>/analytics/overview

In addition, amplifyconfiguration.dart will be updated to reference provisioned backend analytics resources. Note that these files should already be a part of your project if you followed the Project setup walkthrough.

Install Amplify Libraries

In your Flutter project directory, open pubspec.yaml.

You will already have configured Amplify by following the steps in the project setup.

Add Analytics by adding these libraries into your dependencies block:

1environment:
2 sdk: '>=2.15.0 <3.0.0'
3
4dependencies:
5 # Should already be added during Project Setup walkthrough
6 amplify_flutter: ^0.6.0
7
8 # Add these lines in `dependencies` if you have not added it earlier during the Project Setup
9 amplify_auth_cognito: ^0.6.0
10 amplify_analytics_pinpoint: ^0.6.0

Initialize Amplify Analytics

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

1import 'package:amplify_analytics_pinpoint/amplify_analytics_pinpoint.dart';
2import 'package:amplify_auth_cognito/amplify_auth_cognito.dart';
3import 'package:amplify_flutter/amplify_flutter.dart';
4import 'package:flutter/material.dart';
5
6import 'amplifyconfiguration.dart';
7
8Future<void> _configureAmplify() async {
9 // Add Pinpoint and Cognito Plugins, and any other plugins you want to use
10 final analyticsPlugin = AmplifyAnalyticsPinpoint();
11 final authPlugin = AmplifyAuthCognito();
12 await Amplify.addPlugins([analyticsPlugin, authPlugin]);
13}

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

1import 'package:amplify_analytics_pinpoint/amplify_analytics_pinpoint.dart';
2import 'package:amplify_auth_cognito/amplify_auth_cognito.dart';
3import 'package:amplify_flutter/amplify_flutter.dart';
4import 'package:flutter/material.dart';
5
6import 'amplifyconfiguration.dart';
7
8Future<void> _configureAmplify() async {
9 // ...
10 await Amplify.addPlugins([analyticsPlugin, authPlugin]);
11
12 // Once Plugins are added, configure Amplify
13 // Note: Amplify can only be configured once.
14 try {
15 await Amplify.configure(amplifyconfig);
16 } on AmplifyAlreadyConfiguredException {
17 safePrint(
18 'Tried to reconfigure Amplify; this can occur when your app restarts on Android.',
19 );
20 }
21}

Your class will look like this:

1import 'package:amplify_flutter/amplify_flutter.dart';
2import 'package:amplify_analytics_pinpoint/amplify_analytics_pinpoint.dart';
3import 'package:amplify_auth_cognito/amplify_auth_cognito.dart';
4import 'package:flutter/material.dart';
5
6import 'amplifyconfiguration.dart';
7
8Future<void> _configureAmplify() async {
9 // Add any Amplify plugins you want to use
10 final analyticsPlugin = AmplifyAnalyticsPinpoint();
11 final authPlugin = AmplifyAuthCognito();
12 await Amplify.addPlugins([analyticsPlugin, authPlugin]);
13
14 // Once Plugins are added, configure Amplify
15 // Note: Amplify can only be configured once.
16 try {
17 await Amplify.configure(amplifyconfig);
18 } on AmplifyAlreadyConfiguredException {
19 safePrint(
20 'Tried to reconfigure Amplify; this can occur when your app restarts on Android.',
21 );
22 }
23}
24
25Future<void> main() async {
26 WidgetsFlutterBinding.ensureInitialized();
27 await _configureAmplify();
28 runApp(const MyApp());
29}
30
31class MyApp extends StatefulWidget {
32 const MyApp({Key? key}): super(key: key);
33
34 // ...
35}

View Analytics console

If you have not saved the link from before, you can still reach it from the terminal. Run the following command for opening the console.

1amplify console analytics

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: