AWS Messaging & Targeting Blog
Creating an AI-powered marketing solution for sentiment analysis and engagement
Note: Matt Dombrowski, one of our Solutions Architects, wrote this article. He also developed the sample code that you can use to implement this solution.
September 8, 2021: Amazon Elasticsearch Service has been renamed to Amazon OpenSearch Service. See details.
Marketers know that it’s critical to understand the conversations that customers are having about their brands. The holy grail isn’t just to understand what’s happening on social media, but to distill those conversations into actionable insights. After that, you can scale, automate, and continuously improve your brand’s ability to engage.
In this blog post, we’ll demonstrate how your marketing department can use machine learning to understand social user sentiment and engage with users.
You’ll assume the role of a Marketing Manager at an up-and-coming retail company called Mountain Manhattan. Mountain Manhattan has seen strong growth in recent years, and is now looking for a better way to engage with its Twitter followers. Specifically, Mountain Manhattan wants to know who its advocates and distracters are, what the overall sentiment of the brand is, and who the key influencers are that need the white-glove treatment.
After that, we’ll show you how to quickly deploy a solution for real-time social media sentiment analysis and engagement. This process consists of three basic steps. First, you collect tweets that refer to your brand’s Twitter handle. Next, you use machine learning to assign a score to those tweets. And finally, you use Amazon Pinpoint to engage with your customers based on those scores.
Mountain Manhattan’s challenges
Like many companies, Mountain Manhattan has more data than they can act on. Mountain Manhattan receives over 1,000 tweets a day. That’s more than 365,000 tweets per year! Like most companies with a social media strategy, Mountain Manhattan thinks of each of these tweets as an ‘opportunity to engage.’ One of Mountain Manhattan’s challenges is that they need the tone, voice, response time, and candor of their responses to be clear and consistent—and they need to do so in several different languages.
They tried the brute force approach of reading tweets and manually responding to each one. However, this process quickly became unsustainable (not to mention very expensive) because of limited time and resources. Also, while Mountain Manhattan’s marketing team is rather tech-savvy, they don’t have the time or experience to worry about technical issues like ongoing development or security. Mountain Manhattan needs an engagement solution that’s affordable and effective, and that has industry-leading reliability, scale, and security.
The solution
Mountain Manhattan decided to use several AWS services to create an integrated social media monitoring and customer engagement solution. The marketing team spent about 30 minutes setting up the sophisticated solution described below, which enables testing and iteration on multiple use cases before going live.
This solution monitors a Twitter feed, and sends relevant tweets to an Amazon Kinesis data stream. Then it uses an AWS Lambda function to take the appropriate action. In this case, that action involves first calling Amazon Comprehend to provide a sentiment score, and then using Amazon Pinpoint to engage with the Twitter user. This solution has several benefits for Mountain Manhattan:
- It’s scalable. Mountain Manhattan has flash and holiday sales, targeted campaigns, and various ad campaigns that can lead to spikes in customer tweets. This solution can handle nearly any workload in real time. Furthermore, ingesting every single tweet about their brand helps Mountain Manhattan get a holistic view of customer sentiment.
- It’s easy to use. Mountain Manhattan needs to adapt to their customer needs. This means that they need a solution that’s customizable, user-friendly, and intuitive to use. By using Amazon Pinpoint, Mountain Manhattan’s marketing team was able to set up recurring campaigns based on certain customer characteristics. The daily, automated campaigns send notifications to an ever-updating dynamic segment. This ensures that customers never receive the same campaign message twice.
- It’s cost-effective. Priorities for Mountain Manhattan can change quickly, and long-term contracts are no longer appealing to management. By using AWS services, there are no subscription fees, upfront costs, or long-term commitments. Mountain Manhattan pays only for they use, and they can adjust their marketing spend at any time.
- It lets you own your data. Data is the lifeblood of modern marketing organizations. Companies need to own their data for use across many applications and systems. This solution gives Mountain Manhattan that ownership and flexibility. If it ever becomes necessary, they can change the destination of their Kinesis data streams to nearly any destination, and can export their customer data from Amazon Pinpoint.
How the solution works
The following architecture diagram shows the various AWS services that enable this AI-powered social sentiment marketing solution.
Let’s take a closer look at each of these components. This solution uses the following services and solutions:
- Mobile client: Mountain Manhattan’s mobile app uses the Twitter SDK to authenticate users. The app is implemented in React Native for cross-platform compatibility, and because Mountain Manhattan’s developers are more familiar with JavaScript. Integrating the Twitter SDK enables Mountain Manhattan to map customers’ Twitter handles to specific mobile devices. There are a variety of ways to authenticate users—including authentication services from Facebook, Google, and Amazon. In this example, we focus on Twitter.
- Amazon Kinesis Data Streams: This AWS service transfers tweets from Twitter into AWS Lambda (for sentiment analysis) and Amazon S3 (for long-term archival). Kinesis Data Streams can capture and store terabytes of data per hour from hundreds of thousands of sources. In the future, Mountain Manhattan could expand this solution to analyze data from Facebook, point-of-sale terminals, and website click streams.
- Amazon ElasticSearch: Kinesis Firehose streams the tweets into an ElasticSearch cluster. By using ElasticSearch, Mountain Manhattan can easily search the data, and can visualize it by using Kibana.
- AWS Lambda and Amazon Comprehend: Mountain Manhattan uses AWS Lambda to execute code without having to worry about deploying and maintaining servers. The AWS Lambda function looks at the tweets as they come in and determines the appropriate action to take. In Mountain Manhattan’s case, if the customer who tweeted is known, it calls Amazon Comprehend to perform AI-based sentiment analysis. Based on the results of that sentiment analysis, the Lambda function calls Amazon Pinpoint to begin the customer engagement process.
- Amazon Pinpoint: This solution uses Amazon Pinpoint to handle two essential functions. First, it captures information about endpoints (the unique devices that use the app). Second, it sends targeted campaigns to those endpoints. The AWS Mobile SDK, which is integrated into Mountain Manhattan’s app, automatically associates the customer’s Twitter handle with their endpoint ID in Amazon Pinpoint. Mountain Manhattan also collects some custom attributes for each endpoint. For example, they place each endpoint into one of the following categories: Influencers, Supporters, Detractors, Loyal Shoppers, and CS Support Needed. By categorizing customers in this way, Mountain Manhattan can create more personalized messaging.
Mountain Manhattan’s solution in action
As Mountain Manhattan starts to ingest tweets, they get to know a lot more about their customers than just what they said. Mountain Manhattan can see the number of followers a user has, which is a good way to identify influencers. Additionally, Mountain Manhattan can see the Twitter user’s description, logo, picture, and location (if the user has exposed it). All of this data is fed into the AWS Lambda function, where Mountain Manhattan can take the customized action.
The screenshots in the following sections show the kinds of push notifications that Mountain Manhattan could automatically send to customers based on the content of their tweets.
Identifying influencers and early adopters
Mountain Manhattan’s AWS Lambda function determines how many Twitter followers each user has. If the number of followers is above a certain threshold, the function attaches the Influencer custom attribute to the user’s endpoint, and sends them a push notification.
Tracking and engaging with consumers during events or promotions
During events, Mountain Manhattan can join the conversation with their customers by sending messages in real time based on customers’ tweets.
Proactively engaging with customers having support issues
When Mountain Manhattan determines that the sentiment of a tweet is negative, they can send custom push notifications in an attempt to resolve the issue.
Offering discounts or concessions to unhappy customers
Mountain Manhattan could monitor for certain words or phrases, such as “shipping delays”. When they detect these keywords, Amazon Pinpoint can automatically send a push notification that offers a discount on a future purchase.
Deploying the solution
Now that we’ve seen how this solution works, it’s time to implement it. The coolest part? You can use an AWS CloudFormation template to deploy all of the AWS components of this solution in a few clicks and about 10 minutes of your time.
Note: The procedures for deploying this solution might change over time as we continue to make improvements to it. For the latest procedures, see the Github page for this solution at https://github.com/aws-samples/amazon-pinpoint-social-sentiment/.
Prerequisites
To complete these procedures, you have to have the following:
- A mobile app that uses Twitter’s APIs or SDK for authentication and for ingesting tweets.
- A macOS-based computer and physical iOS device (the Simulator that’s included with Xcode isn’t sufficient for testing this solution).
- Xcode, Node.js, npm, and CocoaPods installed on your macOS-based computer.
- To download Xcode, go to https://developer.apple.com/download/.
- To download Node.js and npm, go to https://nodejs.org/en/. Download the latest Long-Term Support (LTS) version for macOS.
- To download and install CocoaPods, type the following command at the macOS command line:
sudo gem install cocoapods
- The AWS Command Line Interface (AWS CLI) installed and configured on your macOS-based computer. For information about installing the AWS CLI, see Installing the AWS Command Line Interface. For information about setting up the AWS CLI, see Configuring the AWS CLI.
- An AWS account with sufficient permissions to create the resources shown in the architecture diagram in the earlier section. For more information about creating an AWS account, see How do I create and activate a new Amazon Web Services account.
- An Amazon EC2 key pair. You need this to log in to the EC2 instance if you want to modify the Twitter handle that you’re monitoring. For more information, see Creating a Key Pair Using Amazon EC2.
- An Apple Developer account. Note that the approach that we cover in this post focuses exclusively on iOS devices. You can implement this solution on Android devices
Part 1: Create a Twitter application
The first step in this process is to create a Twitter app, which gives you access to the Twitter API. This solution uses the Twitter API to collect tweets in real time.
To create a Twitter application:
- Log in to your Twitter account. If you don’t already have a Twitter account, create one at https://twitter.com/signup.
- Go to https://apps.twitter.com/app/new, and then choose Create a new application.
- Under Application Details, complete the following sections:
- For Name, type the name of your app.
- For Description, type a description of your app.
- For the Website and Callback URL fields, type any fully qualified URL (such as https://www.example.com). You’ll change these values in a later step, so the values you enter at this point aren’t important.
- Choose Create your Twitter application.
- Under Your access token, choose Create your access token.
- Under Application type, choose Read Only.
- Under Oauth settings, note the values next to Consumer key and Consumer secret. Then, under Your access token, note the values next to Access key and Secret access key. You’ll need all of these values in later steps.
Part 2: Install the dependencies
This solution requires you to download and set up some files from a GitHub repository.
To configure the AWS Mobile SDK in your app:
- Open Terminal.app. On the command line, navigate to the directory where you want to create your project.
- On the command line, type the following command to clone the repository that contains the source code that you’re using to configure this solution:
git clone https://github.com/aws-samples/amazon-pinpoint-social-sentiment/
- Type the following command to change to the directory that contains the installation files:
cd amazon-pinpoint-social-sentiment/mobile
- Type the following command to download the dependencies for this solution:
npm install
- Type the following command to link the dependencies in the project:
react-native link
- Type the following command to change into the ios directory:
cd ios
- Type the following command to install CocoaPods into your project:
pod install
Part 3: Set up your app to use the AWS Mobile SDK
To configure your app:
- From the /mobile directory, type the following command to create a backend project for your app and pull the service configuration (aws-exports.js file) into your project:
awsmobile init
. Press Enter at each prompt to accept the default response, as shown in the following example. - Open the file
aws-exports.js
. This file contains information about the backend configuration of your AWS Mobile Hub project. Take note of theaws_mobile_analytics_app_id
key—you’ll use this value in a later step. - In a text editor, open the file
pinpoint-social-sentiment/mobile/App.js
. Under TwitterAuth.init, next totwitter_key
, replace<your key here>
with the consumer key that you received when you created your Twitter app in step 1. Then, next totwitter_secret
, replace<your secret here>
with the consumer secret you received when you created your Twitter app. When you finish, save the file. - In a text editor, open the file
amazon-pinpoint-social-sentiment/mobile/ios/MobileCon/AppDelegate.m
. Search for the following section:In this section, replace
<your-consumer-key>
with your Twitter consumer key, and replace<your-consumer-secret>
with your Twitter consumer secret. - In a text editor, open the file
amazon-pinpoint-social-sentiment/mobile/ios/MobileCon/Info.plist
. Search for the following section:Replace
<your-API-key>
with your Twitter consumer secret.
Part 4: Set up push notifications in your app
Now you’re ready to set up your app to send push notifications. A recent Medium post from Nader Dabit, one of our Developer Advocates, outlines this process nicely. Start at the Apple Developer Configuration section, and complete the remaining steps. After you complete these steps, your app is ready to send push notifications.
Part 5: Launch the AWS CloudFormation template
While your app is building, you can launch the AWS CloudFormation template that sets up the backend components that power this solution.
- Sign in to the AWS Management Console, and then open the AWS CloudFormation console at https://console.thinkwithwp.com/cloudformation/home.
- Use the region selector to select the US East (N. Virginia) region.
- Choose Create new stack.
- Next to Choose a template, choose Specify an Amazon S3 template URL, and then paste the following URL: https://s3.amazonaws.com/mattd-customer-share/twitterdemo.template.yaml. Choose Next.
- Under Specify Details, for Stack Name, type a name for the CloudFormation stack.
- Under Parameters, do the following:
- For AccessToken, type your Twitter access token.
- For SecretAccessToken, type your Twitter access token secret.
- For AppId, type the app ID that you obtained in Part 3.
- For ConsumerKey, type your Twitter consumer key.
- For ConsumerSecret, type your Twitter consumer secret.
- Choose Next.
- On the next page, review your settings, and then choose Next again. On the final page, select the box to indicate that you understand that AWS CloudFormation will create IAM resources, and then choose Create.
When you choose Create, AWS CloudFormation creates the all of the backend components for the application. These include an EC2 instance, networking infrastructure, a Kinesis data stream, a Kinesis Firehose delivery stream, an S3 bucket, an Elasticsearch cluster, and a Lambda function. This process takes about 10 minutes to complete.
Part 6: Send a test tweet
Now you’re ready to test the solution to make sure that all of the components work as expected.
Start by logging in to your Twitter account. Send a tweet to @awsformobile. Your tweet should contain language that has a positive sentiment.
Your EC2 instance, which monitors the Twitter streaming API, captures this tweet. When this happens, the EC2 instance uses the Kinesis data stream to send the tweet to an Amazon S3 bucket for long-term storage. It also sends the tweet to AWS Lambda, which uses Amazon Comprehend to assign a sentiment score to the tweet. If the message is positive, Amazon Pinpoint sends a push notification to the Twitter handle that sent the message.
You can monitor the execution of the Lambda function by using Amazon CloudWatch Logs. You can access the CloudWatch Logs console at https://console.thinkwithwp.com/cloudwatch/home?region=us-east-1#logs. The log should contain an entry that resembles the following example:
On the Amazon Elasticsearch Service (Amazon ES) console, you can watch as Amazon ES catalogs incoming tweets. You can access this console at https://console.thinkwithwp.com/es/home?region=us-east-1. For the Amazon ES domain for the tweets, choose the Kibana URL. You can use Kibana to easily search your incoming tweets, as shown in the following image:
Finally, you can go to your Amazon S3 bucket to view an archive of the tweets that were addressed to you. This bucket is useful for simple archiving, additional analysis, visualization, or even machine learning. You can access the Amazon S3 console at https://s3.console.thinkwithwp.com/s3/home?region=us-east-1#.
Part 7: Create an Amazon Pinpoint campaign
In the real world, you probably don’t want to send messages to users immediately after they send tweets to your Twitter handle. If you did, you might seem too aggressive, and your customers might hesitate to engage with your brand in the future.
Fortunately, you can use the campaign scheduling tools in Amazon Pinpoint to create a recurring campaign.
- Sign in to the AWS Management Console, and then open the Amazon Pinpoint console at https://console.thinkwithwp.com/pinpoint/home/?region=us-east-1.
- On the Projects page, choose your app.
- In the navigation pane, choose Campaigns, and then choose New Campaign.
- For Campaign name, type a name for the campaign, and then choose Next step.
- On the Segment page, do the following
- Choose Create a new segment.
- For Name your segment to reuse it later, type a name for the segment.
- For Filter by user attributes, choose the plus sign (+) icon. Filter by segment to include all endpoints where Sentiment is Positive, as shown in the following image:
- Choose Next step.
- On the Message page, type the message that you want to send, and then choose Next step. To learn more about writing mobile push messages, see Writing a Mobile Push Message in the Amazon Pinpoint User Guide.
- On the Schedule page, choose the date and time when the message will be sent. You can also schedule the campaign to run on a recurring basis, such as every week. To learn more about scheduling campaigns, see Set the Campaign Schedule in the Amazon Pinpoint User Guide.
Final thoughts
In this blog post, we demonstrated how your marketing department can use machine learning to understand social user sentiment and engage with users.
In the interest of transparency, we calculated the total costs associated with running this solution. Our calculation includes a small Elasticsearch cluster, a small EC2 instance, storage costs, compute costs, and messaging costs. Assuming your app has 1 million monthly active users (MAUs), and assuming that 0.5% of those MAUs mention your brand every month on Twitter, running this solution would cost $28.66 per month, or just under four cents an hour.
We think this solution is one of the most affordable and capable social media sentiment analysis tools you’ll find on the market today. The best part about this solution is that it can be a complete solution—or the starting point for your own customized solution.
Need to send push messages on other platforms, such as Firebase Cloud Messaging (FCM) (for most Android devices)? No problem. Just set up your app to send endpoint data to Amazon Pinpoint and to send push notifications, and you’re ready to go! Want to send messages through different channels? If you have other endpoint data for your customers (such as email addresses or mobile phone numbers), you can add channels to your project in Amazon Pinpoint, and then use those channels to send messages.
We’re very excited about this solution, and we can’t wait to see what you build with it!