Amazon Personalize Events examples using SDK for JavaScript V3 - Amazon SDK for JavaScript
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.

The Amazon SDK for JavaScript V3 API Reference Guide describes in detail all the API operations for the Amazon SDK for JavaScript version 3 (V3).

Starting October 1, 2022, Amazon SDK for JavaScript (v3) will end support for Internet Explorer 11 (IE 11). For details, see Announcing the end of support for Internet Explorer 11 in the Amazon SDK for JavaScript (v3).

Amazon Personalize Events examples using SDK for JavaScript V3

The following code examples show you how to perform actions and implement common scenarios by using the Amazon SDK for JavaScript V3 with Amazon Personalize Events.

Actions are code excerpts that show you how to call individual Amazon Personalize Events functions.

Scenarios are code examples that show you how to accomplish a specific task by calling multiple Amazon Personalize Events functions.

Each example includes a link to GitHub, where you can find instructions on how to set up and run the code in context.

Topics

Actions

The following code example shows how to import real-time interaction event data into Amazon Personalize Events.

SDK for JavaScript V3
Tip

To learn how to set up and run this example, see GitHub.

// 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();
  • For API details, see PutEvents in Amazon SDK for JavaScript API Reference.

The following code example shows how to incrementally import a user into Amazon Personalize Events Events.

SDK for JavaScript V3
Tip

To learn how to set up and run this example, see GitHub.

// Get service clients module and commands using ES6 syntax. import { PutUsersCommand } from "@aws-sdk/client-personalize-events"; import { personalizeEventsClient } from "./libs/personalizeClients.js"; // Or, create the client here. // const personalizeEventsClient = new PersonalizeEventsClient({ region: "REGION"}); // Set the put users parameters. For string properties and values, use the \ character to escape quotes. var putUsersParam = { datasetArn: "DATASET_ARN", users: [ { 'userId': 'USER_ID', 'properties': "{\"PROPERTY1_NAME\": \"PROPERTY1_VALUE\"}" } ] }; export const run = async () => { try { const response = await personalizeEventsClient.send(new PutUsersCommand(putUsersParam)); console.log("Success!", response); return response; // For unit tests. } catch (err) { console.log("Error", err); } }; run();
  • For API details, see PutUsers in Amazon SDK for JavaScript API Reference.