Page updated Jan 16, 2024

Set up Amplify Analytics

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

  • 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
    • 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.18.0 <4.0.0'
3
4dependencies:
5 amplify_analytics_pinpoint: ^1.0.0
6 amplify_auth_cognito: ^1.0.0
7 amplify_flutter: ^1.0.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}

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:

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: