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

Page updated May 10, 2024

Record events

Record event

The Amplify Analytics plugin makes it easy to record custom events within an app. The plugin handles retry logic in the event that the device loses network connectivity, and it automatically batches requests to reduce network bandwidth.

AnalyticsEvent event = AnalyticsEvent.builder()
.name("PasswordReset")
.addProperty("Channel", "SMS")
.addProperty("Successful", true)
.addProperty("ProcessDuration", 792)
.addProperty("UserAge", 120.3)
.build();
Amplify.Analytics.recordEvent(event);
val event = AnalyticsEvent.builder()
.name("PasswordReset")
.addProperty("Channel", "SMS")
.addProperty("Successful", true)
.addProperty("ProcessDuration", 792)
.addProperty("UserAge", 120.3)
.build()
Amplify.Analytics.recordEvent(event)
AnalyticsEvent event = AnalyticsEvent.builder()
.name("PasswordReset")
.addProperty("Channel", "SMS")
.addProperty("Successful", true)
.addProperty("ProcessDuration", 792)
.addProperty("UserAge", 120.3)
.build();
RxAmplify.Analytics.recordEvent(event);

The Amazon 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 a default configuration to flush out to the network every 30 seconds. You can change this value by passing the autoFlushEventsInterval option to the AWSPinpointAnalyticsPlugin. The option value is measured in milliseconds.

val options = AWSPinpointAnalyticsPlugin.Options {
autoFlushEventsInterval = 60_000
}
Amplify.addPlugin(AWSPinpointAnalyticsPlugin(options))
Amplify.configure(AmplifyOutputs(R.raw.amplify_outputs), this)

To manually flush events, call:

Amplify.Analytics.flushEvents();
Amplify.Analytics.flushEvents()
RxAmplify.Analytics.flushEvents();

When flushing events, a Hub event is sent containing the events which were successfully sent to the Pinpoint service. To receive a list of these events, subscribe to the HubChannel.ANALYTICS channel and handle an event of the type AnalyticsChannelEventName.FLUSH_EVENTS.

Authentication events

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 Amazon 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 amplify_outputs.json by adding the PinpointAppId key under CognitoUserPool.

"CognitoUserPool": {
"Default": {
"PoolId": "<your-user-pool-id>",
"AppClientId": "<your-app-client-id>",
"Region": "<your-app-region>",
"PinpointAppId": "<your-pinpoint-app-id>"
}
}
  • 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.

/**
* Call this method to log an authentication event to the analytics client.
*/
public void logAuthenticationEvent() {
AnalyticsEvent event = AnalyticsEvent.builder()
.name("_userauth.sign_in")
.build();
Amplify.Analytics.recordEvent(event);
}
/**
* Call this method to log an authentication event to the analytics client.
*/
fun logAuthenticationEvent() {
val event = AnalyticsEvent.builder()
.name("_userauth.sign_in")
.build()
Amplify.Analytics.recordEvent(event)
}
/**
* Call this method to log an authentication event to the analytics client.
*/
public void logAuthenticationEvent() {
AnalyticsEvent event = AnalyticsEvent.builder()
.name("_userauth.sign_in")
.build();
RxAmplify.Analytics.recordEvent(event);
}

Global Properties

You can register global properties which will be sent along with all invocations of Amplify.Analytics.recordEvent.

Amplify.Analytics.registerGlobalProperties(
AnalyticsProperties.builder()
.add("AppStyle", "DarkMode")
.build());
Amplify.Analytics.registerGlobalProperties(
AnalyticsProperties.builder()
.add("AppStyle", "DarkMode")
.build())
RxAmplify.Analytics.registerGlobalProperties(
AnalyticsProperties.builder()
.add("AppStyle", "DarkMode")
.build());

To unregister a global property, call Amplify.Analytics.unregisterGlobalProperties():

Amplify.Analytics.unregisterGlobalProperties("AppStyle", "OtherProperty");
Amplify.Analytics.unregisterGlobalProperties("AppStyle", "OtherProperty")
RxAmplify.Analytics.unregisterGlobalProperties("AppStyle", "OtherProperty");