Page updated Jan 16, 2024

Record events

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.

Record Event

The Amplify Analytics plugin provides a simple interface to record custom events within your app. The plugin handles retry logic in the event the device loses network connectivity, and automatically batches requests to reduce network bandwidth.

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}

The AWS Pinpoint event count updates in minutes after recording your event.

However, it can take upwards of 30 minutes for the event to display in the Filter section, and for its custom attributes to appear in Pinpoint.

Flush Events

Events have default configuration to flush out to the network every 60 seconds. If you would like to change this, update amplifyconfiguration.json with the value you would like for autoFlushEventsInterval like so:

1{
2 "UserAgent": "aws-amplify-cli/2.0",
3 "Version": "1.0",
4 "analytics": {
5 "plugins": {
6 "awsPinpointAnalyticsPlugin": {
7 "pinpointAnalytics": {
8 "appId": "AppID",
9 "region": "Region"
10 },
11 "pinpointTargeting": {
12 "region": "Region"
13 },
14 "autoFlushEventsInterval": 60
15 }
16 }
17 }
18}

Note: If you set autoFlushEventsInterval to 0, you are responsible for calling Amplify.Analytics.flushEvents() to submit the recorded events to the backend.

Authentication events

Indicate how frequently users authenticate with your application.

On the Analytics page, the Users tab displays charts for Sign-ins, Sign-ups, and Authentication failures.

To learn how frequently users authenticate with your app, update your application code so that Pinpoint receives the following standard event types for authentication:

  • _userauth.sign_in
  • _userauth.sign_up
  • _userauth.auth_fail

You can report authentication events by doing either of the following:

  • Managing user sign-up and sign-in with Amazon Cognito user pools.

Cognito user pools are user directories that make it easier to add sign-up and sign-in to your app. As users authenticate with your app, Cognito reports authentication events to Pinpoint. For more information, see Using Amazon Pinpoint Analytics with Amazon Cognito User Pools in the Amazon Cognito Developer Guide. Also update amplifyconfiguration.json by adding the PinpointAppId key under CognitoUserPool.

1"CognitoUserPool": {
2 "Default": {
3 "PoolId": "<poolid>",
4 "AppClientId": "<appclientid>",
5 "Region": "<region>",
6 "PinpointAppId": "<pinpointappid>"
7 }
8}
  • Manually recording events using the recordEvent() API.

    If you don't want to use Cognito user pools, you can use the Pinpoint client to record and submit authentication events, as shown in the following examples. In these examples, the event type is set to _userauth.sign_in, but you can substitute any authentication event type.

1func sendUserSignInEvent() {
2 let event = BasicAnalyticsEvent(
3 name: "_userauth.sign_in"
4 )
5 Amplify.Analytics.record(event: event)
6}

Global Properties

You can register properties which will be used across all Amplify.Analytics.record(event:) calls.

1let globalProperties: AnalyticsProperties = ["globalPropertyKey": "value"]
2Amplify.Analytics.registerGlobalProperties(globalProperties)

To unregister global properties call Amplify.Analytics.unregisterGlobalProperties():

1// when called with no arguments, it unregisters all global properties
2Amplify.Analytics.unregisterGlobalProperties()
3
4// or you can specify properties to unregister
5Amplify.Analytics.unregisterGlobalProperties(["globalPropertyKey1", "globalPropertyKey2"])