Set up Amplify Analytics
Amplify enables you to collect analytics data for your app. In order to use Analytics, you will enable Amazon Kinesis or Amazon Pinpoint using the AWS Cloud Development Kit (AWS CDK). 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.
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.)
Set up Analytics backend
Use the AWS CDK to create an analytics resource powered by Amazon Pinpoint.
import { defineBackend } from "@aws-amplify/backend"import { auth } from "./auth/resource";import { data } from "./data/resource";import { Policy, PolicyStatement } from "aws-cdk-lib/aws-iam";import { CfnApp } from "aws-cdk-lib/aws-pinpoint";import { Stack } from "aws-cdk-lib/core";
const backend = defineBackend({ auth, data, // additional resources});
const analyticsStack = backend.createStack("analytics-stack");
// create a Pinpoint appconst pinpoint = new CfnApp(analyticsStack, "Pinpoint", { name: "myPinpointApp",});
// create an IAM policy to allow interacting with Pinpointconst pinpointPolicy = new Policy(analyticsStack, "PinpointPolicy", { policyName: "PinpointPolicy", statements: [ new PolicyStatement({ actions: ["mobiletargeting:UpdateEndpoint", "mobiletargeting:PutEvents"], resources: [pinpoint.attrArn + "/*"], }), ],});
// apply the policy to the authenticated and unauthenticated rolesbackend.auth.resources.authenticatedUserIamRole.attachInlinePolicy(pinpointPolicy);backend.auth.resources.unauthenticatedUserIamRole.attachInlinePolicy(pinpointPolicy);
// patch the custom Pinpoint resource to the expected output configurationbackend.addOutput({ analytics: { amazon_pinpoint: { app_id: pinpoint.ref, aws_region: Stack.of(pinpoint).region, } },});
Install Amplify Libraries
-
To install the Amplify Libraries in your application, open your project in Xcode and select File > Add Packages....
-
Enter the Amplify Library for Swift GitHub repo URL (
https://github.com/aws-amplify/amplify-swift
) into the search bar and click Add Package.
- Lastly, add AWSPinpointAnalyticsPlugin, AWSCognitoAuthPlugin, and Amplify to your target. Then click Add Package.
Initialize Amplify Analytics
Import and load the configuration file in your app. It's recommended you add the Amplify configuration step to your app's root entry point.
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(with:)
methods.
Add the following imports to the top of your main App
file:
import Amplifyimport AWSCognitoAuthPluginimport 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(with: .amplifyOutputs) 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 Amplifyimport AWSCognitoAuthPluginimport 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(with: .amplifyOutputs) 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
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: