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.
Future<void> recordCustomEvent() async { final event = AnalyticsEvent('PasswordReset');
event.customProperties ..addStringProperty('Channel', 'SMS') ..addBoolProperty('Successful', true);
// You can also add the properties one by one like the following event.customProperties.addIntProperty('ProcessDuration', 792); event.customProperties.addDoubleProperty('doubleKey', 120.3);
await Amplify.Analytics.recordEvent(event: event);}
Flush events
Events have default configuration to flush out to the network every 30 seconds. To modify the default value, assign the autoFlushEventsInterval
property to your desired duration. The example below configures the plugin to flush events every 10 seconds.
{ "UserAgent": "aws-amplify-cli/2.0", "Version": "1.0", "analytics": { "plugins": { "awsPinpointAnalyticsPlugin": { "pinpointAnalytics": { "appId": "AppID", "region": "Region" }, "pinpointTargeting": { "region": "Region" }, "autoFlushEventsInterval": 10 } } }}
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:
await Amplify.Analytics.flushEvents();
Global Properties
You can register global properties which will be sent along with all invocations of Amplify.Analytics.recordEvent
.
Future<void> registerGlobalProperties() async { final properties = CustomProperties() ..addStringProperty('AppStyle', 'DarkMode'); await Amplify.Analytics.registerGlobalProperties( globalProperties: properties, );}
To unregister a global property, call Amplify.Analytics.unregisterGlobalProperties()
:
Future<void> unregisterGlobalProperties() async { await Amplify.Analytics.unregisterGlobalProperties( propertyNames: ['AppStyle', 'OtherProperty'], );}
Furthermore, you can remove all global properties by calling unregisterGlobalProperties
without propertyNames
:
Future<void> unregisterAllGlobalProperties() async { await Amplify.Analytics.unregisterGlobalProperties();}