Amazon Personalize Events examples using SDK for JavaScript (v3) - AWS SDK Code Examples

There are more AWS SDK examples available in the AWS Doc SDK Examples GitHub repo.

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 AWS SDK for JavaScript (v3) with Amazon Personalize Events.

Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios.

Each example includes a link to the complete source code, 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 use PutEvents.

SDK for JavaScript (v3)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

// 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. const 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 AWS SDK for JavaScript API Reference.

The following code example shows how to use PutItems.

SDK for JavaScript (v3)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

// Get service clients module and commands using ES6 syntax. import { PutItemsCommand } 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 items parameters. For string properties and values, use the \ character to escape quotes. const putItemsParam = { datasetArn: "DATASET_ARN" /* required */, items: [ /* required */ { itemId: "ITEM_ID" /* required */, properties: '{"PROPERTY1_NAME": "PROPERTY1_VALUE", "PROPERTY2_NAME": "PROPERTY2_VALUE", "PROPERTY3_NAME": "PROPERTY3_VALUE"}' /* optional */, }, ], }; export const run = async () => { try { const response = await personalizeEventsClient.send( new PutItemsCommand(putItemsParam), ); console.log("Success!", response); return response; // For unit tests. } catch (err) { console.log("Error", err); } }; run();
  • For API details, see PutItems in AWS SDK for JavaScript API Reference.

The following code example shows how to use PutUsers.

SDK for JavaScript (v3)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

// 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. const 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 AWS SDK for JavaScript API Reference.