Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions,
see Getting Started with Amazon Web Services in China
(PDF).
Using the PutEvents operation
After you create an Item interactions dataset and an event tracker for your dataset group,
you are ready to record item interaction events. To record item interaction events, you use the PutEvents API operation. The following sections show how to record a single event,
how to record multiple events with event value data, and how to include
impressions data with an event.
For information on how to record events for anonymous users see Recording events for
anonymous users.
Recording a single item interaction event
The following example shows a PutEvents
operation that passes one item interaction event. The
corresponding schema is shown, along with an example row from the Item interactions dataset.
Your application generates a unique sessionId
when a user first visits your website or uses your
application. You must use the same sessionId
in all events throughout the session. Amazon Personalize uses the
sessionId
to associate events with the user before they log in (is anonymous). For more information, see
Recording events for
anonymous users.
The event list is an array of Event objects. An
eventType
is required for each event. If you don't have event type data, you can provide a placeholder value to satisfy the requirement.
The trackingId
comes from the event tracker you created in Creating an item interaction event tracker. The userId
, itemId
, and sentAt
parameters
map to the USER_ID, ITEM_ID, and TIMESTAMP fields of a corresponding historical Interactions
dataset. For
more information, see Schemas.
Corresponding dataset columns
Dataset columns: USER_ID, ITEM_ID, TIMESTAMP, EVENT_TYPE
Example data: user123, item-xyz, 1543631760, click
Code example
- SDK for Python (Boto3)
-
import boto3
personalize_events = boto3.client(service_name='personalize-events')
personalize_events.put_events(
trackingId = 'tracking_id
',
userId= 'USER_ID
',
sessionId = 'session_id
',
eventList = [{
'sentAt': 1719511760
,
'eventType': 'click
',
'itemId': 'ITEM_ID
'
}]
)
- SDK for JavaScript v3
-
// Get service clients module and commands using ES6 syntax.
import { PutEventsCommand } from "@aws-sdk/client-personalize-events";
import { personalizeEventsClient } from "./libs/personalizeClients.js";
// Or, create the client here.
// const personalizeEventsClient = new PersonalizeEventsClient({ region: "REGION"});
// Convert your UNIX timestamp to a Date.
const sentAtDate = new Date(1613443801 * 1000); // 1613443801 is a testing value. Replace it with your sentAt timestamp in UNIX format.
// Set put events parameters.
var putEventsParam = {
eventList: [
/* required */
{
eventType: "EVENT_TYPE" /* required */,
sentAt: sentAtDate /* required, must be a Date with js */,
eventId: "EVENT_ID" /* optional */,
itemId: "ITEM_ID" /* optional */,
},
],
sessionId: "SESSION_ID" /* required */,
trackingId: "TRACKING_ID" /* required */,
userId: "USER_ID" /* required */,
};
export const run = async () => {
try {
const response = await personalizeEventsClient.send(
new PutEventsCommand(putEventsParam),
);
console.log("Success!", response);
return response; // For unit tests.
} catch (err) {
console.log("Error", err);
}
};
run();
- Amazon CLI
-
aws personalize-events put-events \
--tracking-id tracking_id \
--user-id USER_ID
\
--session-id session_id \
--event-list '[{
"sentAt": 1719511760
,
"eventType": "click
",
"itemId": "ITEM_ID
"
}]'
- SDK for Java 2.x
-
public static void putEvents(PersonalizeEventsClient personalizeEventsClient,
String trackingId,
String sessionId,
String userId,
String itemId,
String eventType) {
try {
Event event = Event.builder()
.sentAt(Instant.ofEpochMilli(System.currentTimeMillis() + 10 * 60 * 1000))
.itemId(itemId)
.eventType(eventType)
.build();
PutEventsRequest putEventsRequest = PutEventsRequest.builder()
.trackingId(trackingId)
.userId(userId)
.sessionId(sessionId)
.eventList(event)
.build();
int responseCode = personalizeEventsClient.putEvents(putEventsRequest)
.sdkHttpResponse()
.statusCode();
System.out.println("Response code: " + responseCode);
} catch (PersonalizeEventsException e) {
System.out.println(e.awsErrorDetails().errorMessage());
}
}
Recording multiple item interaction events with event value data
The following example shows how to record multiple item interaction events with different event types and different event values.
When you configure a solution, if your Item interactions dataset includes EVENT_TYPE and EVENT_VALUE fields, you can set a specific value as a threshold to exclude records from training. For more
information, seeChoosing the item interaction data used for training.
The example also
shows the recording of an extra property, numRatings
, that is
used as metadata by certain recipes.
Dataset columns: USER_ID, ITEM_ID, TIMESTAMP, EVENT_TYPE, EVENT_VALUE, NUM_RATINGS
Item interactions dataset: user123, movie_xyz, 1543531139, rating, 5, 12
user321, choc-ghana, 1543531760, like, 4
user111, choc-fake, 1543557118, like, 3
- Python
-
import boto3
import json
personalize_events = boto3.client(service_name='personalize-events')
personalize_events.put_events(
trackingId = 'tracking_id
',
userId= 'user555',
sessionId = 'session1',
eventList = [{
'eventId': 'event1',
'sentAt': 1553631760,
'eventType': 'like',
'properties': json.dumps({
'itemId': 'choc-panama',
'eventValue': 4,
'numRatings': 0
})
}, {
'eventId': 'event2',
'sentAt': 1553631782,
'eventType': 'rating',
'properties': json.dumps({
'itemId': 'movie_ten',
'eventValue': 3,
'numRatings': 13
})
}]
)
- Amazon CLI
-
aws personalize-events put-events \
--tracking-id tracking_id
\
--user-id user555 \
--session-id session1 \
--event-list '[{
"eventId": "event1",
"sentAt": 1553631760,
"eventType": "like",
"properties": "{\"itemId\": \"choc-panama\", \"eventValue\": \"true\"}"
}, {
"eventId": "event2",
"sentAt": 1553631782,
"eventType": "rating",
"properties": "{\"itemId\": \"movie_ten\", \"eventValue\": \"4\", \"numRatings\": \"13\"}"
}]'
- SDK for Java 2.x
-
public static void putMultipleEvents(PersonalizeEventsClient personalizeEventsClient,
String trackingId,
String sessionId,
String userId,
String event1Type,
Float event1Value,
String event1ItemId,
int event1NumRatings,
String event2Type,
Float event2Value,
String event2ItemId,
int event2NumRatings) {
ArrayList<Event> eventList = new ArrayList<Event>();
try {
Event event1 = Event.builder()
.eventType(event1Type)
.sentAt(Instant.ofEpochMilli(System.currentTimeMillis() + 10 * 60 * 1000))
.itemId(event1ItemId)
.eventValue(event1Value)
.properties("{\"numRatings\": "+ event1NumRatings +"}")
.build();
eventList.add(event1);
Event event2 = Event.builder()
.eventType(event2Type)
.sentAt(Instant.ofEpochMilli(System.currentTimeMillis() + 10 * 60 * 1000))
.itemId(event2ItemId)
.eventValue(event2Value)
.properties("{\"numRatings\": "+ event2NumRatings +"}")
.build();
eventList.add(event2);
PutEventsRequest putEventsRequest = PutEventsRequest.builder()
.trackingId(trackingId)
.userId(userId)
.sessionId(sessionId)
.eventList(eventList)
.build();
int responseCode = personalizeEventsClient.putEvents(putEventsRequest)
.sdkHttpResponse()
.statusCode();
System.out.println("Response code: " + responseCode);
} catch (PersonalizeEventsException e) {
System.out.println(e.awsErrorDetails().errorMessage());
}
}
The properties keys use camel case names that match the fields in
the Interactions schema. For example, if the field 'NUM_RATINGS' is
defined in the Interactions schema, the property key should be
numRatings
.
Recording
impressions data
If you use the User-Personalization
recipe or add the IMPRESSIONS field to your schema for a dataset in a
Domain dataset group, you can record impressions data in your PutEvents
operation. Impressions are lists of items that were visible to a user when
they interacted with (for example, clicked or watched) a particular item.
Amazon Personalize uses impressions data to guide exploration, where recommendations
include items with less interactions data or relevance. For information on
the implicit and explicit
impressions Amazon Personalize can model, see Impressions data.
If you provide conflicting implicit and explicit impression data in
your PutEvents
requests, Amazon Personalize uses the explicit
impressions by default.
To record the Amazon Personalize recommendations you show your user as impressions
data, include the recommendationId
in your
PutEvents request
and Amazon Personalize derives the implicit impressions based on your recommendation
data.
To manually record impressions data for an event, list the impressions
in the PutEvents command's impression
input parameter. The following code
sample shows how to include a recommendationId
and an
impression
in a PutEvents operation with either the
SDK for Python (Boto3) or the SDK for Java 2.x. If you include both, Amazon Personalize uses the explicit
impressions by default.
- SDK for Python (Boto3)
-
import boto3
personalize_events = boto3.client(service_name='personalize-events')
personalize_events.put_events(
trackingId = 'tracking_id
',
userId= 'userId
',
sessionId = 'sessionId
',
eventList = [{
'eventId': 'event1
',
'eventType': 'rating
',
'sentAt': 1553631760
,
'itemId': 'item id
',
'recommendationId': 'recommendation id
',
'impression': ['itemId1
', 'itemId2
', 'itemId3
'
]
}]
)
- SDK for Java 2.x
-
Use the following putEvents
method to record an
event with impressions data and a recommendationId. For the
impressions parameter, pass the list of itemIds as an
ArrayList.
public static void putEvents(PersonalizeEventsClient personalizeEventsClient,
String trackingId,
String sessionId,
String userId,
String eventType,
Float eventValue,
String itemId,
ArrayList<String> impressions,
String recommendationId) {
try {
Event event = Event.builder()
.eventType(eventType)
.sentAt(Instant.ofEpochMilli(System.currentTimeMillis() + 10 * 60 * 1000))
.itemId(itemId)
.eventValue(eventValue)
.impression(impressions)
.recommendationId(recommendationId)
.build();
PutEventsRequest putEventsRequest = PutEventsRequest.builder()
.trackingId(trackingId)
.userId(userId)
.sessionId(sessionId)
.eventList(event)
.build();
int responseCode = personalizeEventsClient.putEvents(putEventsRequest)
.sdkHttpResponse()
.statusCode();
System.out.println("Response code: " + responseCode);
} catch (PersonalizeEventsException e) {
System.out.println(e.awsErrorDetails().errorMessage());
}
}