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

Page updated Apr 16, 2024

Personalized recommendations

Amazon Personalize can create recommendations by using event data, historical data, or a combination of both. The event data can then be used to create recommendations.

To record event data, you need the following:

For more information, see Record Events.

Installation and Configuration

After creating the Amazon Personalize dataset group, you need to add the personalize:PutEvents permission to your AWS Identity and Access Management (IAM) user roles.

An example IAM policy:

{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": "personalize:PutEvents",
"Resource": "arn:aws:personalize:<your-aws-region>:<your-account-id>:event-tracker/<your-resource-name>"
}]
}

You need the tracking ID of your event tracker. For more information, see Get a Tracking ID.

Configure Amazon Personalize:

src/index.js
import { Amplify } from 'aws-amplify';
Amplify.configure({
...Amplify.getConfig(),
Analytics: {
Personalize: {
// REQUIRED - The trackingId to track the events
trackingId: '<tracking-id>',
// REQUIRED - Amazon Personalize service region
region: 'us-east-1',
// OPTIONAL - The number of events to be deleted from the buffer when flushed.
flushSize: 10,
// OPTIONAL - The interval in milliseconds to perform a buffer check and flush if necessary.
flushInterval: 5000 // 5s
}
}
});

Working with the API

You can use the Identify event type to track a user identity. This lets you connect a user to their actions and record traits about them. To identify a user, specify a unique identifier for the userId property. Consider the following user interactions when choosing when and how often to call record with the Identify eventType:

  • After a user registers.
  • After a user logs in.
  • When a user updates their information (For example, changing or adding a new address).
  • Upon loading any pages that are accessible by a logged-in user (optional).
import { record } from 'aws-amplify/analytics/personalize';
record({
eventType: 'Identify',
properties: {
userId: '<user-id>'
}
});

You can send events to Amazon Personalize by calling the record operation. If you already use Identify to track end-user data, you can skip the userId, the SDK will fetch the userId based on current browser session. For information about the properties field, see Put Events.

import { record } from 'aws-amplify/analytics/personalize';
record({
eventType: '<event-type>',
userId: '<user-id>', // optional
properties: {
itemId: '<item-id>',
eventValue: '<event-value>'
}
});

You can track iframe and HTML5 media types by using the MediaAutoTrack event type. MediaAutoTrack tracks all media events of the media DOM element that you bind to. MediaAutoTracker will automatically track Play, Pause, Ended, TimeWatched, and Resume in eventType. The duration of the event compared to the total length of the media is stored as a percentage value in eventValue.

import { record } from 'aws-amplify/analytics/personalize';
record({
eventType: 'MediaAutoTrack',
userId: '<user-id>', // (optional)
properties: {
domElementId: 'media-dom-element-id',
itemId: '<item-d>'
}
});

Flush events

The recorded events are saved in a buffer and sent to the remote server periodically (You can tune it with the flushInterval option). If needed, you have the option to manually clear all the events from the buffer by using the 'flushEvents' API.

import { flushEvents } from 'aws-amplify/analytics/personalize';
flushEvents();