AWS for Industries

Automate Product Inventory Updates to Customers Based on Their Interests

AWS_Animated_Banner_1600x200

In this blog post, I’ll share a solution for automating the process of capturing your customers’ interests in a product category so you can tailor your marketing messages. I use a retail example to demonstrate how you can use AWS services to build sophisticated processes that generate relevant communications to your customers. This solution is modular, so you can configure only those parts that matter to your business and use case.

Use Cases

You can lose an online purchase due to bad timing, specifically if a customer is browsing online for a product that is not yet available but it might be added later as a new arrival. Sending a promotional message every time a new product is added to your inventory would result in spamming your customer base and might lead to a lot of complaints. The ideal situation is to link the product category to the customers who are interested in new product entries. This approach reduces the frequency of communications while taking into consideration the customers’ interests.

With minor changes, you can use the solution for the following use cases, too:

  • Customers who are interested in a product that is currently out of stock can request to be informed when that product is replenished.
  • Companies that run product drops can provide a way for their customers to register their interest and be informed when the product drops.

Solution Overview

The suggested approach allows customers to register their interest in a product category so that they receive notifications about new product arrivals. The registration can be done through a web/mobile app or through a behavioral pattern, a series of customer events on the web or mobile app that indicate a customer’s intention to perform an action. For example, if a customer makes multiple clicks in one session on products that belong to a specific product category, that indicates the customer’s interest in the category.

After the customer registers their interest, the next step is to implement automation that ingests a new product arrival and sends email to the customer to inform them that a new product in their product category of interest is now available online. In this solution, I use email, but you can change the solution code to contact customers through SMS, push notifications, voice outbound, or custom channels.

Amazon Pinpoint and AWS Step Functions

Amazon Pinpoint is a flexible and scalable outbound and inbound marketing communications service. AWS Step Functions is a serverless function orchestrator that makes it easy to sequence AWS Lambda functions and multiple AWS services into business-critical applications.

High-level Architecture

The solution requires each customer to have an Amazon Pinpoint user attribute (for example, interests) that will store the product categories for which the customer registered their interest. When a new product is added to the products database, a payload that contains information such as product_category, product_name, and product_link should be emitted. You can modify the solution to include more information from the products database payload.

AWS Step Functions will orchestrate a process in which a new Amazon Pinpoint segment is created using the Segments resource. This solution uses the AWS SDK for Python (Boto3). The segment contains all users whose user attribute interests contains the same product category as the one in the products database payload that was emitted following a new product entry. After the segment is created, an Amazon Pinpoint email campaign is created and immediately sent using the  boto3 equivalent of the Campaign resource. The email HTML body is stored in the Lambda function code, allowing it to ingest the products database payload variables such as product_name and product_link without the need to use Amazon Pinpoint user attributes. When the Amazon Pinpoint campaign is completed, a success message is emitted through Amazon Simple Notification Service (Amazon SNS) and some metrics are logged into a DynamoDB table. The segment and campaign are deleted after the email campaign is executed successfully. To prevent littering Amazon Pinpoint with thousands of campaigns and segments, this solution deletes the campaign and segment after every execution. This means that campaign metrics won’t be available in the Amazon Pinpoint console. To monitor your campaign’s performance, you will need to enable the streaming of Amazon Pinpoint events to Kinesis and then use Amazon Athena to query the data or Amazon Athena and Amazon QuickSight to visualize the campaign performance. You can deploy a vetted reference architecture using this CloudFormation template, which will enable events streaming, create an S3 bucket to store the events, and allow you to query these events through Amazon Athena using SQL queries.

Prerequisites

  1. An Amazon Pinpoint project with the email channel enabled. For instructions, see Creating an Amazon Pinpoint project with email support in the Amazon Pinpoint User Guide.
  2. A verified email address. For instructions, see Verifying email identities in the Amazon Pinpoint User Guide.
  3. At least one Amazon Pinpoint endpoint with channel email imported. For instructions, see Importing segments in the Amazon Pinpoint User Guide.
  4. The AWS Command Line Interface (AWS CLI) and a profile that is in the same AWS account and Region as the Amazon Pinpoint project you created. For instructions, see Installing, updating, and uninstalling the AWS CLI in the AWS CLI User Guide.

To check which account you are using, run the aws configure list command.

Architecture

An overview of the architecture is below:

product inventory solution architecture

Deploy the Solution

Step 1: Update the interests attribute

Using the AWS CLI, update an Amazon Pinpoint user’s attribute so that the user has the interests attribute with a value of coats. This represents the registered product category interest of the customer.

Open a command prompt (Windows) or Terminal (macOS) and execute the following command. Replace the <application_id> and <endpoint_id> with the Amazon Pinpoint project values.

aws pinpoint update-endpoint --application-id <application_id> --endpoint-id <endpoint_id> --endpoint-request 'User={UserAttributes={interests=[coats]}}'

For more information, see update-endpoint in the AWS CLI Command Reference.

If the request is successful, you should receive a message like the following:

AWS CLI Command Reference

Step 2: Create a stack

The application is deployed as an AWS CloudFormation template.

Note You are responsible for the cost of the AWS services used while running this sample deployment. There is no additional cost for using this sample. For full details, see the pricing pages for each AWS service you will be using in this sample. Prices are subject to change.

  1. Deploy the latest CloudFormation template by following the link below for your preferred AWS region:
  1. If prompted, login using your AWS account credentials.
  2. You should see a screen titled “Create Stack” at the “Specify template” step. The fields specifying the CloudFormation template are pre-populated. Click the Next button at the bottom of the page.
  3. On the “Specify stack details” screen you may customize the following parameters of the CloudFormation stack:
  • Required:
    • PinpointProjectId: Amazon Pinpoint Project ID.
    • EmailFromAddress: Type the email that you would like the campaign email to be sent from. This email address needs to be validated first.
  • MyApp:
    • ResourceTags: Tag resources, which can help you identify and categorize them.
  • Default:
    • Environment: The type of environment to tag your infrastructure with.

When completed, click Next

  1. Configure stack options if desired, then click Next.
  2. On the review screen, you must check the boxes for:
    • I acknowledge that AWS CloudFormation might create IAM resources
    • I acknowledge that AWS CloudFormation might create IAM resources with custom names
    • I acknowledge that AWS CloudFormation might require the following capability: CAPABILITY_AUTO_EXPAND

These are required to allow CloudFormation to create a Role to grant access to the resources needed by the stack and name the resources in a dynamic way.

  1. Click Create Stack
  2. Wait for the CloudFormation stack to launch. Completion is indicated when the “Stack status” is “CREATE_COMPLETE“.
    • You can monitor the stack creation progress in the “Events” tab.

Step 3: Execute the state machine

  1. In the AWS Step Functions console, a state machine named SegmentCampaignStateMachine-<randomID> should appear at the top of the list. Choose this state machine and then choose Start Execution.
  2. Enter a name for the execution, paste the following JSON in the Input field, and then choose Start execution.
{
   "interest":"coats",
   "product_name":"Leather Coat",
   "product_link":"https://www.example.com/coats/leather_coat"
}

start execution

On the Details tab, the Graph inspector flowchart visualizes the steps in the process. Steps that failed are highlighted in red. Steps that have been successfully completed are highlighted in green.

graph inspector flowchart

change email body

To change the email body, in the AWS Lambda console, search for a Lambda function named <stackname>-PinpointCampaignCreate-<randomID>. When the function loads, access index.py. The email body appears between lines 33 and 41. You can also configure the scheduling of the campaign in this Lambda function.

Step 5: Review DynamoDB table entries

  1. Open the DynamoDB console and from the left navigation pane, choose Tables.
  2. Choose the table named <stack_name>-LoggingCampaignSegmentStatus-<randomID>.
  3. On the Items tab, you should see a row with information about the campaign that was executed from the state machine in Step 3, including:
  • OutputDate: This is the DynamoDB primary partition key. It is a concatenation of the possible Step Functions outputs (SEMGENT_FAILED, CAMPAIGN_FAILED, or SEGMENT_CAMPAIGN_SUCCESS) , the product name, and the date time (YYYY-MM-DD_HH-MM-SS).
  • Pinpoint campaign ID: This ID is generated when a campaign is created.
  • Pinpoint campaign status: Possible values are PENDING, INITIALIZING, RUNNING, PAUSED, CANCELLED, and COMPLETED.
  • Pinpoint campaign start and end date: The start and end time, in ISO 8601 format, of the activity.
  • Pinpoint segment ID: This ID is generated when a segment is created.
  • Successful endpoints contacted: The total number of endpoints to which the campaign successfully delivered messages.
  • Total endpoints contacted: The total number of endpoints to which the campaign attempted to deliver messages.
  • Interest, product link, and product name: The values for these three fields are sourced from the payload the step receives from your product inventory when a new product is added.

review DynamoDB table entries

Conclusion

In this blog post, I have demonstrated how your organization can use Amazon Pinpoint and AWS Step Functions to automate a personalized communication process that notifies your customers about new product arrivals. By capturing your customers’ interests, you can tailor your marketing messages appropriately.

Next Steps

Customers should have the ability to unsubscribe or remove their interest for a product category. You can do this through an email preference management system, where customers can subscribe or unsubscribe to specific topics through a landing page.

An approach more specific to this use case would be to include an unsubscribe link in all emails. When customers click this link, they unsubscribe from future communications for the product category.

The link is part of an email template that includes URL parameters specifying the Amazon Pinpoint endpoint ID and interest. The request goes through Amazon API Gateway, is processed by a Lambda function, and eventually redirects the customer to a page on your website. The Lambda function will update the Amazon Pinpoint endpoint interests user attribute by removing the interest specified in the URL parameter.

Here’s an example of the unsubscribe URL:

https://www.api-gateway-endpoint.com/?

endpointId={{Attributes.EndpointId}}&interest={{User.UserAttributes.interests}}

After the link is clicked and the interests user attribute is updated, you can redirect the customer to a page on your website. For information about how to perform this redirect, see the Redirection in a Serverless API with AWS Lambda and Amazon API Gateway blog post.

Pavlos Ioannou Katidis

Pavlos Ioannou Katidis

Pavlos Ioannou Katidis is an Amazon Pinpoint and Amazon Simple Email Service Specialist Solutions Architect at AWS. He loves to dive deep into his customer’s technical issues and help them design communication solutions. In his spare time, he enjoys playing tennis, watching crime TV series, playing FPS PC games, and coding personal projects.