Page updated Mar 25, 2024

Record events

Record event

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

1AnalyticsEvent event = AnalyticsEvent.builder()
2 .name("PasswordReset")
3 .addProperty("Channel", "SMS")
4 .addProperty("Successful", true)
5 .addProperty("ProcessDuration", 792)
6 .addProperty("UserAge", 120.3)
7 .build();
8
9Amplify.Analytics.recordEvent(event);
1val event = AnalyticsEvent.builder()
2 .name("PasswordReset")
3 .addProperty("Channel", "SMS")
4 .addProperty("Successful", true)
5 .addProperty("ProcessDuration", 792)
6 .addProperty("UserAge", 120.3)
7 .build()
8
9Amplify.Analytics.recordEvent(event)
1AnalyticsEvent event = AnalyticsEvent.builder()
2 .name("PasswordReset")
3 .addProperty("Channel", "SMS")
4 .addProperty("Successful", true)
5 .addProperty("ProcessDuration", 792)
6 .addProperty("UserAge", 120.3)
7 .build();
8
9RxAmplify.Analytics.recordEvent(event);

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 30 seconds. If you would like to change this, update amplifyconfiguration.json with the value in milliseconds you would like for autoFlushEventsInterval. This configuration will flush events every 10 seconds:

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 "autoFlushEventsInterval": 10000
11 },
12 "pinpointTargeting": {
13 "region": "Region"
14 }
15 }
16 }
17 }
18}

To manually flush events, call:

1Amplify.Analytics.flushEvents();
1Amplify.Analytics.flushEvents()
1RxAmplify.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

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.

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

Global Properties

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

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

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

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