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.

An image that shows the relationship between the various components used in this 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.A push notification that says "We like you too! Tap here to join our Influencers Club and get free stuff!"

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.A push notification that says "We're glad you're enjoying the sale! Tap here to subscribe to our events calendar."

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.A push notification that says "Sorry to hear you're having trouble! :( Tap here to talk to Jake, one of our best support team members."

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.A push notification that says "We agree--delays are annoying! Tap here to get 20% off your next order."

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:

  1. Log in to your Twitter account. If you don’t already have a Twitter account, create one at https://twitter.com/signup.
  2. Go to https://apps.twitter.com/app/new, and then choose Create a new application.
  3. 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.
  4. Choose Create your Twitter application.
  5. Under Your access token, choose Create your access token.
  6. Under Application type, choose Read Only.
  7. 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:

  1. Open Terminal.app. On the command line, navigate to the directory where you want to create your project.
  2. 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/
  3. Type the following command to change to the directory that contains the installation files: cd amazon-pinpoint-social-sentiment/mobile
  4. Type the following command to download the dependencies for this solution: npm install
  5. Type the following command to link the dependencies in the project: react-native link
  6. Type the following command to change into the ios directory: cd ios
  7. 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:

  1. 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.
    Please tell us about your project:
    ? Where is your project's source directory: /
    ? Where is your project's distribution directory that stores build artifacts: /
    ? What is your project's build command: npm run-script build
    ? What is your project's start command for local test run: npm run-script start
    
    ? What awsmobile project name would you like to use: mobile-2018-08-16-03-16-39
  2. Open the file aws-exports.js. This file contains information about the backend configuration of your AWS Mobile Hub project. Take note of the aws_mobile_analytics_app_id key—you’ll use this value in a later step.
  3. In a text editor, open the file pinpoint-social-sentiment/mobile/App.js. Under TwitterAuth.init, next to twitter_key, replace <your key here> with the consumer key that you received when you created your Twitter app in step 1. Then, next to twitter_secret, replace <your secret here> with the consumer secret you received when you created your Twitter app. When you finish, save the file.
  4. In a text editor, open the file amazon-pinpoint-social-sentiment/mobile/ios/MobileCon/AppDelegate.m. Search for the following section:
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
      NSURL *jsCodeLocation;
    
      [[Twitter sharedInstance] startWithConsumerKey:@"<your-consumer-key>" consumerSecret:@"<your-consumer-secret>"];

    In this section, replace <your-consumer-key> with your Twitter consumer key, and replace <your-consumer-secret> with your Twitter consumer secret.

  5. In a text editor, open the file amazon-pinpoint-social-sentiment/mobile/ios/MobileCon/Info.plist. Search for the following section:
    <key>CFBundleURLTypes</key>
        <array>
            <dict>
                <key>CFBundleURLSchemes</key>
                <array>
                    <string>twitterkit-<your-API-key></string>
                </array>
            </dict>
        </array>
        ...

    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.

  1. Sign in to the AWS Management Console, and then open the AWS CloudFormation console at https://console.aws.amazon.com/cloudformation/home.
  2. Use the region selector to select the US East (N. Virginia) region.
  3. Choose Create new stack.
  4. 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.
  5. Under Specify Details, for Stack Name, type a name for the CloudFormation stack.
  6. Under Parameters, do the following:
    1. For AccessToken, type your Twitter access token.
    2. For SecretAccessToken, type your Twitter access token secret.
    3. For AppId, type the app ID that you obtained in Part 3.
    4. For ConsumerKey, type your Twitter consumer key.
    5. For ConsumerSecret, type your Twitter consumer secret.
  7. Choose Next.
  8. 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.aws.amazon.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.aws.amazon.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.aws.amazon.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.

  1. Sign in to the AWS Management Console, and then open the Amazon Pinpoint console at https://console.aws.amazon.com/pinpoint/home/?region=us-east-1.
  2. On the Projects page, choose your app.
  3. In the navigation pane, choose Campaigns, and then choose New Campaign.
  4. For Campaign name, type a name for the campaign, and then choose Next step.
  5. On the Segment page, do the following
    1. Choose Create a new segment.
    2. For Name your segment to reuse it later, type a name for the segment.
    3. 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:A screenshot that shows how to add the Sentiment = Positive attribute to a Pinpoint segment.
    4. Choose Next step.
  6. 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.
  7. 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!