Record events
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.
func recordEvents() { let properties: AnalyticsProperties = [ "eventPropertyStringKey": "eventPropertyStringValue", "eventPropertyIntKey": 123, "eventPropertyDoubleKey": 12.34, "eventPropertyBoolKey": true ] let event = BasicAnalyticsEvent(name: "eventName", properties: properties) Amplify.Analytics.record(event: event)}
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:
{ "UserAgent": "aws-amplify-cli/2.0", "Version": "1.0", "analytics": { "plugins": { "awsPinpointAnalyticsPlugin": { "pinpointAnalytics": { "appId": "AppID", "region": "Region" }, "pinpointTargeting": { "region": "Region" }, "autoFlushEventsInterval": 60 } } }}
Note: If you set
autoFlushEventsInterval
to 0, you are responsible for callingAmplify.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
.
"CognitoUserPool": { "Default": { "PoolId": "<poolid>", "AppClientId": "<appclientid>", "Region": "<region>", "PinpointAppId": "<pinpointappid>" }}
-
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.
func sendUserSignInEvent() { let event = BasicAnalyticsEvent( name: "_userauth.sign_in" ) Amplify.Analytics.record(event: event)}
Global Properties
You can register properties which will be used across all Amplify.Analytics.record(event:)
calls.
let globalProperties: AnalyticsProperties = ["globalPropertyKey": "value"]Amplify.Analytics.registerGlobalProperties(globalProperties)
To unregister global properties call Amplify.Analytics.unregisterGlobalProperties()
:
// when called with no arguments, it unregisters all global propertiesAmplify.Analytics.unregisterGlobalProperties()
// or you can specify properties to unregisterAmplify.Analytics.unregisterGlobalProperties(["globalPropertyKey1", "globalPropertyKey2"])