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 looses network connectivity and automatically batches requests to reduce network bandwidth.

1Future<void> recordCustomEvent() async {
2 final event = AnalyticsEvent('PasswordReset');
3
4 event.customProperties
5 ..addStringProperty('Channel', 'SMS')
6 ..addBoolProperty('Successful', true);
7
8 // You can also add the properties one by one like the following
9 event.customProperties.addIntProperty('ProcessDuration', 792);
10 event.customProperties.addDoubleProperty('doubleKey', 120.3);
11
12 await Amplify.Analytics.recordEvent(event: event);
13}

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

Note

Setting autoFlushEventsInterval to 0 will disable the automatic flush of events and you will be responsible for submitting them.

To manually flush events, call:

1await Amplify.Analytics.flushEvents();

Global Properties

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

1Future<void> registerGlobalProperties() async {
2 final properties = CustomProperties()
3 ..addStringProperty('AppStyle', 'DarkMode');
4 await Amplify.Analytics.registerGlobalProperties(
5 globalProperties: properties,
6 );
7}

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

1Future<void> unregisterGlobalProperties() async {
2 await Amplify.Analytics.unregisterGlobalProperties(
3 propertyNames: ['AppStyle', 'OtherProperty'],
4 );
5}

Furthermore, you can remove all global properties by calling unregisterGlobalProperties without propertyNames:

1Future<void> unregisterAllGlobalProperties() async {
2 await Amplify.Analytics.unregisterGlobalProperties();
3}