Page updated Jan 19, 2024

Set up Amplify Analytics

Amplify iOS v1 is now in Maintenance Mode until May 31st, 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 v1.

Please use the latest version (v2) of Amplify Library for Swift to get started.

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

Amplify libraries should be used for all new cloud connected applications. If you are currently using the AWS Mobile SDK for iOS, you can access the documentation here.

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

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

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

  2. Enter the Amplify iOS 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.

To install the Amplify Analytics and Authentication to your application, add both "AmplifyPlugins/AWSPinpointAnalyticsPlugin" and "AmplifyPlugins/AWSCognitoAuthPlugin" to your Podfile (Because IAM credential is required to access AWS Pinpoint Service, "AWSCognitoAuthPlugin" also needs to be installed). Your Podfile should look similar to:

1target 'MyAmplifyApp' do
2 use_frameworks!
3 pod 'Amplify'
4 pod 'AmplifyPlugins/AWSPinpointAnalyticsPlugin'
5 pod 'AmplifyPlugins/AWSCognitoAuthPlugin'
6end

To install, download and resolve these pods, execute the command:

1pod install --repo-update

Now you can open your project by opening the .xcworkspace file using the following command:

1xed .

Initialize Amplify Analytics

To initialize the Amplify Analytics and Authentication categories, you are required to use the Amplify.add() method for each category you want. When you are done calling add() on each category, you finish configuring Amplify by calling Amplify.configure().

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

1import Amplify
2import AWSPinpointAnalyticsPlugin
1import Amplify
2import AmplifyPlugins

Add the following code

Create a custom AppDelegate, and add to your application:didFinishLaunchingWithOptions method

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

Then in the App scene, use UIApplicationDelegateAdaptor property wrapper to use your custom AppDelegate

1@main
2struct MyAmplifyApp: App {
3 @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
4
5 var body: some Scene {
6 WindowGroup {
7 ContentView()
8 }
9 }
10}

Add to your AppDelegate's application:didFinishLaunchingWithOptions method

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

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, specify your event using BasicAnalyticsEvent and call Amplify.Analytics.record()

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

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: