Using AWS as a Back End: The Data Store API

In this tutorial, you’ll extend the Isolation Nation app from the previous tutorial, adding analytics and real-time chat functionality using AWS Pinpoint and AWS Amplify DataStore. By Tom Elliott.

Leave a rating/review
Download materials
Save for later

In this tutorial, you’ll pick up the Isolation Nation app where you left off at the end of Part 1, Authentication & API. You’ll extend the app, using AWS Pinpoint and AWS Amplify DataStore to add analytics and real-time chat functionality.

Before you begin, log in to the AWS Console.

You can use the project you created in the first tutorial as the starting point for this next one. However, if you are using the starter project from this tutorial, you will need to pull your Amplify back-end configuration in to the starter project’s Xcode workspace.

From the Amplify console for your project, select the Backend environments tab, click the Edit backend link and then Copy the pull command.

Pulling your project configuration from AWS

Navigate to the starter project in your terminal and paste the command you just copied from the Amplify console. When prompted, select the profile you set up before, if applicable. Select no default editor, an iOS app, and answer Y to modifying the back end from the list of options in the terminal.

Next generate the Amplify configuration files by running the following command:

amplify-app --platform ios

Open the IsolationNation.xcworkspace file in Xcode and edit the amplifytools.xcconfig file to reflect the following settings:

Note: A quick and easy way to open the correct file is enter the command:
xed .
xed .

Finally, run the following command in Terminal:

pod update

When the update finishes, build your app. You may need to build it twice, as Amplify needs to generate the User model files the first time around.

Note: Although you’re encouraged to follow the previous tutorial first, it’s not necessary. If you’re starting afresh, download the project materials using the Download Materials button at the top or bottom of this tutorial. Before you start, you must set up AWS Amplify on your computer. And you must add a new Amplify project with both Cognito Auth and an AppSync API with a user model. See the instructions in the previous tutorial.

Getting Started

Isolation Nation is an app for people who are self-isolating due to COVID-19. It lets them request help from others in their local community. At the end of the previous tutorial, the app was using AWS Cognito to allow users to sign up and log in to the app. It used AWS AppSync to read and write public user data about the user.

Now build and run. If you already completed the previous tutorial, confirm that you can still log in with the user account(s) you created before. If you’re starting here, sign up a new user.

App Analytics

Analyzing how people use your app in real life is an important part of the process of building a great product. AWS Pinpoint is a service providing analytics and marketing capabilities to your app. In this section, you’ll learn how to record user actions for future analysis.

To begin, open a terminal at the root of your project. Use the Amplify CLI to add analytics capability to your project:

amplify add analytics

When prompted, select Amazon Pinpoint and press Enter to accept the default resource name. Type Y to accept the recommended default for authorization.

Adding analytics via the CLI

Next, open your workspace in Xcode and open Podfile. Insert the following code before the line containing end:

pod 'AmplifyPlugins/AWSPinpointAnalyticsPlugin'

This adds the AWS Pinpoint plugin as a dependency of your app. Install the plugin by switching to your terminal and running the following:

pod install --repo-update 

Back in Xcode, open AppDelegate.swift and add the Pinpoint plugin to the Amplify configuration. In application(_:didFinishLaunchingWithOptions:), add the following line directly before the call to Amplify.configure():

try Amplify.add(plugin: AWSPinpointAnalyticsPlugin())

Your app is now configured to send analytics data to AWS Pinpoint.

Tracking Users and Sessions

Tracking user sessions with Amplify is simple. In fact, it couldn’t be any easier, as you don’t have to do anything! :] Just installing the plugin will automatically record when the app opens and closes.

But, to be really useful, you should add user identification to your analytics calls. In Xcode, open AuthenticationService.swift. At the very bottom of the file, add the following extension:

// MARK: AWS Pinpoint

extension AuthenticationService {
  // 1
  func identifyUserToAnalyticsService(_ user: AuthUser) {
    // 2
    let userProfile = AnalyticsUserProfile(
      name: user.username,
      email: nil,
      plan: nil,
      location: nil,
      properties: nil
    // 3
    Amplify.Analytics.identifyUser(user.userId, withProfile: userProfile)

In this code, you’re doing several things:

  1. First, you create a new method, identifyUserToAnalyticsService(_:), which takes an AuthUser object.
  2. Then, you create an analytics user profile for the user. For analytics, you only care about the user’s name, so you set the other optional fields to nil.
  3. You finish by calling identifyUser(_:withProfile:). You pass the user’s ID and the user profile you just created. This identifies the user in AWS Pinpoint.

Next, update the method signature for setUserSessionData(_:) to accept an optional AuthUser parameter:

private func setUserSessionData(_ user: User?, authUser: AuthUser? = nil) {

Add the following to the end of the DispatchQueue block in that method:

if let authUser = authUser {

Now, update the call to setUserSessionData(_:authUser:) in two places. Make the same change at the end of both signIn(as:identifiedBy:) and checkAuthSession():

setUserSessionData(user, authUser: authUser)

You’re now passing the authUser into setUserSessionData. This allows it to call into identifyUserToAnalyticsService(_:).

Build and run. Log in and out with your user several times so you’ll something to see in your Pinpoint analytics.

Next, open your terminal and type the following:

amplify console analytics

This will open a Pinpoint console in your browser showing the Analytics overview for your app’s back end.

The AWS Pinpoint Console

By default, Pinpoint displays aggregated data for the past 30 days. For now, this will almost certainly average to zero. Underneath the title, select Last 30 days. Then, in the pop-up, select today’s date for both the start of the period the end of the period. Click away from the pop-up to close it, and the stats will refresh with today’s data.

In the left-hand menu, select Usage. In the boxes showing active endpoints and active users, you should see some non-zero values. Don’t worry if the counts are still zero — it can take up to 15 minutes for the data to refresh. If this is the case, carry on with the tutorial and check again at the end. :]

AWS Pinpoint Console - User metrics

That’s enough analytics for now. It’s time to get back to building the chat functionality!