Name:
interface
Value:
Amplify has re-imagined the way frontend developers build fullstack applications. Develop and deploy without the hassle.

Page updated Apr 29, 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 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 using the latest Amplify Release. As new Xcode and visionOS versions are released, the support will be updated with any necessary fixes on a best effort basis.

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.

amplify add analytics
? Select an Analytics provider (Use arrow keys)
`Amazon Pinpoint`
? Provide your pinpoint resource name:
`yourPinpointResourceName`
? 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)
`Yes`

To deploy your backend, run:

amplify 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:

import Amplify
import AWSCognitoAuthPlugin
import AWSPinpointAnalyticsPlugin

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

init() {
do {
try Amplify.add(plugin: AWSCognitoAuthPlugin())
try Amplify.add(plugin: AWSPinpointAnalyticsPlugin())
try Amplify.configure()
print("Amplify configured with Auth and Analytics plugins")
} catch {
print("Failed to initialize Amplify with \(error)")
}
}

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

import Amplify
import AWSCognitoAuthPlugin
import AWSPinpointAnalyticsPlugin

Add the following code to the application:didFinishLaunchingWithOptions method:

func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
do {
try Amplify.add(plugin: AWSCognitoAuthPlugin())
try Amplify.add(plugin: AWSPinpointAnalyticsPlugin())
try Amplify.configure()
print("Amplify configured with Auth and Analytics plugins")
} catch {
print("Failed to initialize Amplify with \(error)")
}
return true
}

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

Amplify configured with Auth and Analytics plugin

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

let properties: AnalyticsProperties = [
"eventPropertyStringKey": "eventPropertyStringValue",
"eventPropertyIntKey": 123,
"eventPropertyDoubleKey": 12.34,
"eventPropertyBoolKey": true
]
let event = BasicAnalyticsEvent(
name: "eventName",
properties: properties
)
Amplify.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.

amplify 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: