AWS Messaging & Targeting Blog
Target your customers with ML based on their interest in a product or product attribute.
Customer segmentation allows marketers to better tailor their efforts to specific subgroups of their audience. Businesses who employ customer segmentation can create and communicate targeted marketing messages that resonate with specific customer groups. Segmentation increases the likelihood that customers will engage with the brand, and reduces the potential for communications fatigue—that is, the disengagement of customers who feel like they’re receiving too many messages that don’t apply to them. For example, if your business wants to launch an email campaign about business suits, the target audience should only include people who wear suits.
This blog presents a solution that uses HAQM Personalize to generate highly personalized HAQM Pinpoint customer segments. Using HAQM Pinpoint, you can send messages to those customer segments via campaigns and journeys.
Personalizing Pinpoint segments
Marketers first need to understand their customers by collecting customer data such as key characteristics, transactional data, and behavioral data. This data helps to form buyer personas, understand how they spend their money, and what type of information they’re interested in receiving.
You can create two types of customer segments in HAQM Pinpoint: imported and dynamic. With both types of segments, you need to perform customer data analysis and identify behavioral patterns. After you identify the segment characteristics, you can build a dynamic segment that includes the appropriate criteria. You can learn more about dynamic and imported segments in the HAQM Pinpoint User Guide.
Businesses selling their products and services online could benefit from segments based on known customer preferences, such as product category, color, or delivery options. Marketers who want to promote a new product or inform customers about a sale on a product category can use these segments to launch HAQM Pinpoint campaigns and journeys, increasing the probability that customers will complete a purchase.
Building targeted segments requires you to obtain historical customer transactional data, and then invest time and resources to analyze it. This is where the use of machine learning can save time and improve the accuracy.
HAQM Personalize is a fully managed machine learning service, which requires no prior ML knowledge to operate. It offers ready to use models for segment creation as well as product recommendations, called recipes. Using HAQM Personalize USER_SEGMENTATION recipes, you can generate segments based on a product ID or a product attribute.
About this solution
The solution is based on the following reference architectures:
Both of these architectures are deployed as nested stacks along the main application to showcase how contextual segmentation can be implemented by integrating HAQM Personalize with HAQM Pinpoint.
High level architecture
Once training data and training configuration are uploaded to the Personalize data bucket (1) an AWS Step Function state machine is executed (2). This state machine implements a training workflow to provision all required resources within HAQM Personalize. It trains a recommendation model (3a) based on the Item-Attribute-Affinity recipe. Once the solution is created, the workflow creates a batch segment job to get user segments (3b). The job configuration focuses on providing segments of users that are interested in action genre movies
{ "itemAttributes": "ITEMS.genres = \"Action\"" }
When the batch segment job finishes, the result is uploaded to HAQM S3 (3c). The training workflow state machine publishes HAQM Personalize state changes on a custom event bus (4). An HAQM Event Bridge rule listens on events describing that a batch segment job has finished (5). Once this event is put on the event bus, a batch segment postprocessing workflow is executed as AWS Step Function state machine (6). This workflow reads and transforms the segment job output from HAQM Personalize (7) into a CSV file that can be imported as static segment into HAQM Pinpoint (8). The CSV file contains only the HAQM Pinpoint endpoint-ids that refer to the corresponding users from the HAQM Personalize recommendation segment, in the following format:
Id
hcbmnng30rbzf7wiqn48zhzzcu4
tujqyruqu2pdfqgmcgkv4ux7qlu
keul5pov8xggc0nh9sxorldmlxc
lcuxhxpqh/ytkitku2zynrqb2ce
The mechanism to resolve an HAQM Pinpoint endpoint id relies on the user id that is set in HAQM Personalize to be also referenced in each endpoint within HAQM Pinpoint using the user ID attribute.
The workflow ensures that the segment file has a unique filename so that the segments within HAQM Pinpoint can be identified independently. Once the segment CSV file is uploaded to S3 (7), the segment import workflow creates a new imported segment within HAQM Pinpoint (8).
Datasets
The solution uses an artificially generated movies’ dataset called Bingewatch for demonstration purposes. The data is pre-processed to make it usable in the context of HAQM Personalize and HAQM Pinpoint. The pre-processed data consists of the following:
- Interactions’ metadata created out of the Bingewatch ratings.csv
- Items’ metadata created out of the Bingewatch movies.csv
- users’ metadata created out of the Bingewatch ratings.csv, enriched with invented data about e-mail address and age
- HAQM Pinpoint endpoint data
Interactions’ dataset
The interaction dataset describes movie ratings from Bingewatch users. Each row describes a single rating by a user identified by a user id.
The EVENT_VALUE describes the actual rating from 1.0 to 5.0 and the EVENT_TYPE specifies that the rating resulted because a user watched this movie at the given TIMESTAMP, as shown in the following example:
USER_ID,ITEM_ID,EVENT_VALUE,EVENT_TYPE,TIMESTAMP
1,1,4.0,Watch,964982703
2,3,4.0,Watch,964981247
3,6,4.0,Watch,964982224
...
Items’ dataset
The item dataset describes each available movie using a TITLE, RELEASE_YEAR, CREATION_TIMESTAMP and a pipe concatenated list of GENRES, as shown in the following example:
ITEM_ID,TITLE,RELEASE_YEAR,CREATION_TIMESTAMP,GENRES
1,Toy Story,1995,788918400,Adventure|Animation|Children|Comedy|Fantasy
2,Jumanji,1995,788918400,Adventure|Children|Fantasy
3,Grumpier Old Men,1995,788918400,Comedy|Romance
...
Users’ dataset
The users dataset contains all known users identified by a USER_ID. This dataset contains artificially generated metadata that describe the users’ GENDER and AGE, as shown in the following example:
USER_ID,GENDER,E_MAIL,AGE
1,Female,success1@simulator.amazonses.com,21
2,Female,success+2@simulator.amazonses.com,35
3,Male,success+3@simulator.amazonses.com,37
4,Female,success+4@simulator.amazonses.com,47
5,Agender,success+5@simulator.amazonses.com,50
...
HAQM Pinpoint endpoints
To map HAQM Pinpoint endpoints to users in HAQM Personalize, it is important to have a consisted user identifier. The mechanism to resolve an HAQM Pinpoint endpoint id relies that the user id in HAQM Personalize is also referenced in each endpoint within HAQM Pinpoint using the userId attribute, as shown in the following example:
User.UserId,ChannelType,User.UserAttributes.Gender,Address,User.UserAttributes.Age
1,EMAIL,Female,success+1@simulator.amazonses.com,21
2,EMAIL,Female,success+2@simulator.amazonses.com,35
3,EMAIL,Male,success+3@simulator.amazonses.com,37
4,EMAIL,Female,success+4@simulator.amazonses.com,47
5,EMAIL,Agender,success+5@simulator.amazonses.com,50
...
Solution implementation
Prerequisites
To deploy this solution, you must have the following:
- An AWS account
- The AWS CLI on your local machine. For more information see Installing the AWS CLI guide.
- The SAM CLI on your local machine. For more information, see Installing the SAM CLI in the AWS Serverless Application Model Developer Guide.
- The latest version of Boto3. For more information see Installation in the Boto3 Documentation.
Note: This solution creates an HAQM Pinpoint project with the name personalize. If you want to deploy this solution on an existing HAQM Pinpoint project, you will need to perform changes in the YAML template.
Deploy the solution
Step 1: Deploy the SAM solution
Clone the GitHub repository to your local machine (how to clone a GitHub repository). Navigate to the GitHub repository location in your local machine using SAM CLI and execute the command below:
sam deploy --stack-name contextual-targeting --guided
Fill the fields below as displayed. Change the AWS Region to the AWS Region of your preference, where HAQM Pinpoint and HAQM Personalize are available. The Parameter Email is used from HAQM Simple Notification Service (SNS) to send you an email notification when the HAQM Personalize job is completed.
Configuring SAM deploy
======================
Looking for config file [samconfig.toml] : Not found
Setting default arguments for 'sam deploy' =========================================
Stack Name [sam-app]: contextual-targeting
AWS Region [us-east-1]: eu-west-1
Parameter Email []: me@example.com
Parameter PEVersion [v1.2.0]:
Parameter SegmentImportPrefix [pinpoint/]:
#Shows you resources changes to be deployed and require a 'Y' to initiate deploy
Confirm changes before deploy [y/N]:
#SAM needs permission to be able to create roles to connect to the resources in your template
Allow SAM CLI IAM role creation [Y/n]:
#Preserves the state of previously provisioned resources when an operation fails
Disable rollback [y/N]:
Save arguments to configuration file [Y/n]:
SAM configuration file [samconfig.toml]:
SAM configuration environment [default]:
Looking for resources needed for deployment:
Creating the required resources...
[...]
Successfully created/updated stack - contextual-targeting in eu-west-1
======================
Step 2: Import the initial segment to HAQM Pinpoint
We will import some initial and artificially generated endpoints into HAQM Pinpoint.
Execute the command below to your AWS CLI in your local machine.
The command below is compatible with Linux:
SEGMENT_IMPORT_BUCKET=$(aws cloudformation describe-stacks --stack-name contextual-targeting --query 'Stacks[0].Outputs[?OutputKey==`SegmentImportBucket`].OutputValue' --output text)
aws s3 sync ./data/pinpoint s3://$SEGMENT_IMPORT_BUCKET/pinpoint
For Windows PowerShell use the command below:
$SEGMENT_IMPORT_BUCKET = (aws cloudformation describe-stacks --stack-name contextual-targeting --query 'Stacks[0].Outputs[?OutputKey==`SegmentImportBucket`].OutputValue' --output text)
aws s3 sync ./data/pinpoint s3://$SEGMENT_IMPORT_BUCKET/pinpoint
Step 3: Upload training data and configuration for HAQM Personalize
Now we are ready to train our initial recommendation model. This solution provides you with dummy training data as well as a training and inference configuration, which needs to be uploaded into the HAQM Personalize S3 bucket. Training the model can take between 45 and 60 minutes.
Execute the command below to your AWS CLI in your local machine.
The command below is compatible with Linux:
PERSONALIZE_BUCKET=$(aws cloudformation describe-stacks --stack-name contextual-targeting --query 'Stacks[0].Outputs[?OutputKey==`PersonalizeBucketName`].OutputValue' --output text)
aws s3 sync ./data/personalize s3://$PERSONALIZE_BUCKET
For Windows PowerShell use the command below:
$PERSONALIZE_BUCKET = (aws cloudformation describe-stacks --stack-name contextual-targeting --query 'Stacks[0].Outputs[?OutputKey==`PersonalizeBucketName`].OutputValue' --output text)
aws s3 sync ./data/personalize s3://$PERSONALIZE_BUCKET
Step 4: Review the inferred segments from HAQM Personalize
Once the training workflow is completed, you should receive an email on the email address you provided when deploying the stack. The email should look like the one in the screenshot below:
Navigate to the Amazon Pinpoint Console > Your Project > Segments and you should see two imported segments. One named endpoints.csv that contains all imported endpoints from Step 2. And then a segment named ITEMSgenresAction_<date>-<time>.csv that contains the ids of endpoints that are interested in action movies inferred by HAQM Personalize
You can engage with HAQM Pinpoint customer segments via Campaigns and Journeys. For more information on how to create and execute HAQM Pinpoint Campaigns and Journeys visit the workshop Building Customer Experiences with HAQM Pinpoint.
Next steps
Contextual targeting is not bound to a single channel, like in this solution email. You can extend the batch-segmentation-postprocessing workflow to fit your engagement and targeting requirements.
For example, you could implement several branches based on the referenced endpoint channel types and create HAQM Pinpoint customer segments that can be engaged via Push Notifications, SMS, Voice Outbound and In-App.
Clean-up
To delete the solution, run the following command in the AWS CLI.
The command below is compatible with Linux:
SEGMENT_IMPORT_BUCKET=$(aws cloudformation describe-stacks --stack-name contextual-targeting --query 'Stacks[0].Outputs[?OutputKey==`SegmentImportBucket`].OutputValue' --output text)
PERSONALIZE_BUCKET=$(aws cloudformation describe-stacks --stack-name contextual-targeting --query 'Stacks[0].Outputs[?OutputKey==`PersonalizeBucketName`].OutputValue' --output text)
aws s3 rm s3://$SEGMENT_IMPORT_BUCKET/ --recursive
aws s3 rm s3://$PERSONALIZE_BUCKET/ --recursive
sam delete
For Windows PowerShell use the command below:
$SEGMENT_IMPORT_BUCKET=$(aws cloudformation describe-stacks --stack-name contextual-targeting --query 'Stacks[0].Outputs[?OutputKey==`SegmentImportBucket`].OutputValue' --output text)
$PERSONALIZE_BUCKET=$(aws cloudformation describe-stacks --stack-name contextual-targeting --query 'Stacks[0].Outputs[?OutputKey==`PersonalizeBucketName`].OutputValue' --output text)
aws s3 rm s3://$SEGMENT_IMPORT_BUCKET/ --recursive
aws s3 rm s3://$PERSONALIZE_BUCKET/ --recursive
sam delete
HAQM Personalize resources like Dataset groups, datasets, etc. are not created via AWS Cloudformation, thus you have to delete them manually. Please follow the instructions in the official AWS documentation on how to clean up the created resources.
About the Authors