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

An application with Amplify libraries integrated and a minimum target of any of the following:

  • iOS 13.0, using Xcode 14.1 or later.
  • macOS 10.15, using Xcode 14.1 or later.
  • tvOS 13.0, using Xcode 14.3 or later.
  • watchOS 9.0, using Xcode 14.3 or later.
  • visionOS 1.0, using Xcode 15 beta 2 or later. (Preview support - see below for more details.)

For a full example, please follow the project setup walkthrough.

visionOS support is currently in preview and can be used by targeting the visionos-preview branch. As new Xcode 15 beta versions are released, the branch will be updated with any necessary fixes on a best effort basis.

For more information on how to use the visionos-preview branch, see Platform Support.

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, amplifyconfiguration.json should 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

Swift Package Manager

  1. To install Amplify Libraries in your application, open your project in Xcode and select File > Add Packages....

  2. Enter the Amplify Library for Swift GitHub repo URL (https://github.com/aws-amplify/amplify-swift) into the search bar and click Add Package.

Note: Up to Next Major Version should be selected from the Dependency Rule dropdown.

  1. Lastly, choose AWSPinpointAnalyticsPlugin, AWSCognitoAuthPlugin, and Amplify. Then click Add Package.

Initialize Amplify Analytics

To use the Amplify Analytics and Authentication categories in your app, you need to create and configure their corresponding plugins by calling the Amplify.add(plugin:) and Amplify.configure() methods.

Add the following imports to the top of your main App file:

1import Amplify
2import AWSCognitoAuthPlugin
3import AWSPinpointAnalyticsPlugin

Add the following code to its initializer. If there is none, you can create a default init:

1init() {
2 do {
3 try Amplify.add(plugin: AWSCognitoAuthPlugin())
4 try Amplify.add(plugin: AWSPinpointAnalyticsPlugin())
5 try Amplify.configure()
6 print("Amplify configured with Auth and Analytics plugins")
7 } catch {
8 print("Failed to initialize Amplify with \(error)")
9 }
10}

Add the following imports to the top of your AppDelegate.swift file:

1import Amplify
2import AWSCognitoAuthPlugin
3import AWSPinpointAnalyticsPlugin

Add the following code to the application:didFinishLaunchingWithOptions method:

1func application(
2 _ application: UIApplication,
3 didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
4) -> Bool {
5 do {
6 try Amplify.add(plugin: AWSCognitoAuthPlugin())
7 try Amplify.add(plugin: AWSPinpointAnalyticsPlugin())
8 try Amplify.configure()
9 print("Amplify configured with Auth and Analytics plugins")
10 } catch {
11 print("Failed to initialize Amplify with \(error)")
12 }
13
14 return true
15}

Upon building and running this application you should see the following in your console window:

1Amplify configured with Auth and Analytics plugin

To record an event, create a BasicAnalyticsEvent and then call Amplify.Analytics.record(event:)

1let properties: AnalyticsProperties = [
2 "eventPropertyStringKey": "eventPropertyStringValue",
3 "eventPropertyIntKey": 123,
4 "eventPropertyDoubleKey": 12.34,
5 "eventPropertyBoolKey": true
6]
7
8let event = BasicAnalyticsEvent(
9 name: "eventName",
10 properties: properties
11)
12
13Amplify.Analytics.record(event: event)

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: