Page updated Jan 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 Amplify IAM user roles.

An example IAM policy:

{ "Version": "2012-10-17", "Statement": [{ "Effect": "Allow", "Action": "personalize:PutEvents", "Resource": "*" }] }
1{
2 "Version": "2012-10-17",
3 "Statement": [{
4 "Effect": "Allow",
5 "Action": "personalize:PutEvents",
6 "Resource": "*"
7 }]
8}

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

Configure Amazon Personalize:

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 } } });
1import { Amplify } from 'aws-amplify';
2
3Amplify.configure({
4 ...Amplify.getConfig(),
5 Analytics: {
6 Personalize: {
7 // REQUIRED - The trackingId to track the events
8 trackingId: '<TRACKING_ID>',
9
10 // REQUIRED - Amazon Personalize service region
11 region: 'us-east-1',
12
13 // OPTIONAL - The number of events to be deleted from the buffer when flushed.
14 flushSize: 10,
15
16 // OPTIONAL - The interval in milliseconds to perform a buffer check and flush if necessary.
17 flushInterval: 5000 // 5s
18 }
19 }
20});

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>" } });
1import { record } from 'aws-amplify/analytics/personalize';
2
3record({
4 eventType: "Identify",
5 properties: {
6 userId: "<USER_ID>"
7 }
8});

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>" } });
1import { record } from 'aws-amplify/analytics/personalize';
2
3record({
4 eventType: "<EVENT_TYPE>",
5 userId: "<USER_ID>", // optional
6 properties: {
7 itemId: "<ITEM_ID>",
8 eventValue: "<EVENT_VALUE>"
9 }
10});

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_ID>" } });
1import { record } from 'aws-amplify/analytics/personalize';
2
3record({
4 eventType: "MediaAutoTrack",
5 userId: "<USER_ID>", // (optional)
6 properties: {
7 domElementId: "MEDIA DOM ELEMENT ID",
8 itemId: "<ITEM_ID>"
9 }
10});

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();
1import { flushEvents } from 'aws-amplify/analytics/personalize';
2
3flushEvents();