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.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Using AWS as a Back End: The Data Store API
40 mins
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.
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:
push=true modelgen=true profile=default envName=dev
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.
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.
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:
- First, you create a new method,
identifyUserToAnalyticsService(_:)
, which takes anAuthUser
object. - 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
. - 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 {
identifyUserToAnalyticsService(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.
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. :]
That’s enough analytics for now. It’s time to get back to building the chat functionality!