Setup Personalize

Import Training Data

Create Dataset group

Datasets are organized within Amazon Personalize dataset groups. A dataset group can only have one of each type of dataset. Each dataset must have an associated schema. A schema tells Amazon Personalize about the structure of your data and allows Amazon Personalize to parse the data. A schema has a name key whose value must match the dataset type.

  1. Open the Personalize Console.
  2. In the navigation page, choose Dataset groups.
  3. Choose Create dataset group.
Step 1: Create dataset group
  1. Set Dataset group name, e.g. tradeldeals-dataset-group
  2. Choose Next.
Create a new dataset group in Personalize
Step 2: Create user-item interaction data
  1. Set Dataset name, e.g. traveldeals-interactions
  2. For Schema selection choose Create new schema.
  3. Set New schema name, e.g. traveldeals-interactions-schema
  4. Set Schema definition to:
{
    "type": "record",
    "name": "Interactions",
    "namespace": "com.amazonaws.personalize.schema",
    "fields": [
        {
            "name": "USER_ID",
            "type": "string"
        },
        {
            "name": "ITEM_ID",
            "type": "string"
        },
        {
            "name": "TIMESTAMP",
            "type": "long"
        }
    ],
    "version": "1.0"
}
  1. Choose Next.
Create user-item interaction data for dataset group
Step 3: Import user-item interaction data
  1. Set Dataset import job name, e.g. traveldeals-interactions-import
  2. Create a new Role, that has access to bucket with training data, e.g. traveldeals-12142020
  3. Set Data location to the location of bucket and prefix of User-item interaction data, which was uploaded in Upload Sample data to S3, e.g. s3://traveldeals-12142020/user-item/
  4. Choose Finish to start import.
Import user-item interaction data for dataset group

Import Items

An Items dataset stores metadata about your items. This might include information such as price, genre, or availability. In our case, it will be category.

  1. Open the Personalize Console.
  2. On the Dataset groups page, choose the dataset you currently workin on, e.g tradeldeals-dataset-group
  3. Under Overview and Upload datasets, for Item data choose Import.
Dataset groups import dashboard with User-item interaction creation in progress
Step 1: Configure item schema
  1. Set Dataset name, e.g. traveldeals-items
  2. For Schema selection choose Create new schema.
  3. Set New schema name, e.g. traveldeals-items-schema
  4. Set Schema definition to:
{
	"type": "record",
	"name": "Items",
	"namespace": "com.amazonaws.personalize.schema",
	"fields": [
		{
			"name": "ITEM_ID",
			"type": "string"
		},
		{
			"name": "CATEGORY",
			"type": "string",
			"categorical": true
		}
	],
	"version": "1.0"
}
  1. Choose Next.
Configure item schema for dataset group
Step 2: Configure item dataset import job
  1. Set Dataset import job name, e.g. traveldeals-items-import
  2. Use the role, which was created for the interactions data set.
  3. Set Data location to the location of bucket and prefix of Items data, which was uploaded in Upload Sample data to S3, e.g. s3://traveldeals-12142020/item/
  4. Choose Start Import.

Setup Event ingestion

Amazon Personalize can make recommendations based on real-time event data only, historical imported data (as demonstrated in the Getting Started guides) only, or a mixture of both. To import real-time event data, record user activity (events) in real-time using the AWS python SDK, AWS Amplify or AWS CLI.

  1. Open the Personalize Console.
  2. On the Dataset groups page, choose the dataset you currently working on, e.g tradeldeals-dataset-group
  3. Under Overview and Install event ingestion SDK, for SDK Installation choose Start.
Step 1: Configure tracker
  1. Set Tracker name, e.g. traveldeals-event-tracker
  2. Choose Next.
Configure tracker for dataset group
Step 2: Install the SDK
  1. Copy and save Tracking ID value, we will need it later to run the script.
  2. Choose Finish.
    Finish tracker configuration for dataset

Create Solution

Once you have finished Preparing and Importing Data, you are ready to create a Solution. A Solution refers to the combination of an Amazon Personalize recipe, customized parameters, and one or more solution versions (trained models).

  1. Open the Personalize Console.
  2. On the Dataset groups page, choose the dataset you currently workin on, e.g tradeldeals-dataset-group
  3. Under Overview and Create solutions, for Solution creation choose Start.
Dataset groups import dashboard with Solution creation ready to start
Step 1: Create solution
  1. Set Solution name, e.g. traveldeals-solution
  2. For Recipe choose aws-user-personalization.
  3. Choose Next.

The User-Personalization (aws-user-personalization) recipe is optimized for all personalized recommendation scenarios. It predicts the items that a user will interact with based on Interactions, Items, and Users datasets. When recommending items, it uses automatic item exploration.

Create solution for dataset group
Step 2: Create solution version
  1. Review Solution and choose Next.

Launch Campaign

Once you create a solution with a solution version, you can create a campaign to deploy the solution version and get recommendations. A campaign is used to make recommendations for your users. You create a campaign by deploying a solution version.

Campaign can be launched when solution is created. Solution creation will take some time (with samplate data sets and settings used for this campaign, it took ~30 minutes to complete at the moment of workshop creation).
You can skip this section, proceed with Web Application update and come back to continue later.

  1. Open the Personalize Console.
  2. On the Dataset groups page, choose the dataset you currently workin on, e.g tradeldeals-dataset-group
  3. Under Overview and Launch campaigns, choose Create new campaign.
Dataset groups import dashboard with available button to Create a new campaign
Create new campaign
  1. Set Campaign name, e.g. traveldeals-recommendations-campaign
  2. For Solution select recently created Solution, e.g. traveldeals-solution
  3. Choose Create campaign.
Create a new campaign for dataset group

Wait until campaign changes status to Active.

Test Campaign

Get a user Id from Cognito User Pool
  1. Open User Pool and choose Users and groups, as described in Import Cognito Users.
  2. Choose any user and use value of sub attribute.
Get a user id in Cognito
Run Campaign test
  1. Open the Personalize Console.
  2. On the Dataset groups page, choose the dataset you currently workin on, e.g tradeldeals-dataset-group
  3. Under Overview and Launch campaigns, choose View campaigns.
Dataset groups import dashboard with button to view campaign
  1. Choose the recently created Campaign, e.g. traveldeals-recommendations-campaign
  2. Set User ID to a value copied from Cognito.

Output will be similar to this:

Test recommendation campaign

Track Personalize Events in Web Application

The event ingestion SDK allows you to track user events in your application and feed them to your solutions. We are going to record Personalize events with Amplify.

Update Web Application

Edit src/App.js:

  1. Add AmazonPersonalizeProvider to existing import Amplify Analytics import:
import { Analytics, AmazonPersonalizeProvider } from '@aws-amplify/analytics';
  1. Use Amplify.configure() to AmazonPersonalizeProvider. Use Tracking ID obtained from Setup Event ingestion:
Analytics.addPluggable(new AmazonPersonalizeProvider());

Analytics.configure({
  AmazonPersonalize: {
    trackingId: '', // TODO: Set to Personalize Tracking ID, e.g.: 'efd7edf5-7ddc-4732-b4b3-0374c6b721e9'
    region: awsconfig.aws_project_region
  }
});
  1. Update DealDetails - add tracking of Deal views in loadDealInfo() of React.useEffect:
const user = await Auth.currentAuthenticatedUser();

Analytics.record({
    eventType: 'PageView',
    userId: user.attributes.sub,
    properties: {
        itemId: deal.id
    }
}, 'AmazonPersonalize');

Update policies

As we manually created Personalize Campaign, we will have to set up policies to allow Amplify application to ingest events.

The Amplify CLI provides escape hatches for modifying the backend configurations generated in the form of CloudFormation templates by the CLI. This allows you to use the CLI for common flows, but also any advanced scenarios which aren’t provided in the standard category workflows.

  1. Open traveldeals/amplify/backend/backend-config.json file.
  2. Update analytics to include new traveldealsPersonalize Personalize resource. Updated section for analytics will look like this:
"analytics": {
    "traveldeals": {
        "service": "Pinpoint",
        "providerPlugin": "awscloudformation"
    },
    "traveldealsPersonalize": {
        "service": "Personalize",
        "providerPlugin": "awscloudformation"
    }  
}
  1. In traveldeals/amplify/backend/analytics/ directory, create a new directory for Personalize and call it traveldealsPersonalize.
  2. Create a file parameters.json file in this direcory and paste this code:
{
    "authPolicyName": "personalize_amplify",
    "authRoleName": {
        "Ref": "AuthRoleName"
    }
}
  1. In the same traveldeals/amplify/backend/analytics/traveldealsPersonalize directory, create another file personalize-cloudformation-template.json and paste this code:
{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Description": "Personalize resource stack creation using Amplify CLI",
    "Parameters": {
        "env": {
            "Type":  "String"
        },
        "authPolicyName": {
            "Type": "String"
        },
        "authRoleName": {
            "Type": "String"
        }
    },
    "Resources": {
        "CognitoAuthPolicy": {
            "Type": "AWS::IAM::Policy",
            "Properties": {
                "PolicyName": {
                    "Ref": "authPolicyName"
                },
                "Roles": [
                    {
                        "Ref": "authRoleName"
                    }
                ],
                "PolicyDocument": {
                    "Version": "2012-10-17",
                    "Statement": [
                        {
                            "Effect": "Allow",
                            "Action": "personalize:PutEvents",
                            "Resource": "*"
                        }
                    ]
                }
            }
        }
    }
}
  1. To populate the Amplify CLI runtime files and make it aware of the newly added custom resources run amplify env checkout for current environment, e.g. dev:
amplify env checkout dev
  1. Run status to see all added categories:
amplify status

It will list a new resource traveldealsPersonalize in Analytics category:

Current Environment: dev

| Category  | Resource name          | Operation | Provider plugin   |
| --------- | ---------------------- | --------- | ----------------- |
| Analytics | traveldealsPersonalize | Create    | awscloudformation |
| Auth      | traveldealsXXXXXXXX    | No Change | awscloudformation |
| Api       | traveldeals            | No Change | awscloudformation |
| Hosting   | amplifyhosting         | No Change | awscloudformation |
| Analytics | traveldeals            | No Change | awscloudformation |

  1. Push backend resources updates, build and publish client-side application:
amplify publish

Confirm your decision, when prompted.

Go back to Launch Campaign, if you skipped it before. Check if Solution was created and changed its status to Active.

If not yet, it may be a good time to take a cup of your favorite hot beverage ☕ and explore some other Amazon Personalize Samples while model is trained.

Next sections of this Lab will require created Personalize campaign.

You can go back to check if Pinpoint Events are already available, and start creating Funnel and Campaign.

After this, you can switch to Adding Custom Analytics Lab and come back to finish this Lab later.