AWS Mobile
Developer Guide

Add Analytics to the Notes App

In the previous section of this tutorial, we installed Xcode, downloaded a sample note-taking app from GitHub, then compiled and ran it in the iOS Simulator. This tutorial assumes you have completed the those steps. In this section, we will extend the notes app to include application analytics. Application analytics allow us to gather demographic information about the application usage.

You should be able to complete this section in 10-15 minutes.

Set Up Your Back End

To start, set up the mobile backend resources in AWS:

  1. Open the AWS Mobile Hub console.

  2. Choose Create on the upper left, and the type ios-notes-app for the name of the Mobile Hub project.

  3. Choose Next, choose iOS, and then choose Add.

  4. Choose Download Cloud Config, and save awsconfiguration.json. This file the configuration to connect your app to your backend.

  5. Choose Next and then choose Done to create the project.

Used in this section

AWS Mobile Hub: Configure your mobile app's AWS backend in minutes, and then to manage those resources as your app evolves.

Connect to Your Backend

  1. Drag awsconfiguration.json from the download location into the folder in the XCode Project Navigator that contains Info.plist. Select Copy items if needed and Create groups in the options dialog.

  2. Choose Finish.

You have now created the AWS resources you need and connected them to your app.

Add Analytics the Dependencies

  1. To create a Podfile for your project, run:

    cd YOUR-APP-ROOT-FOLDER pod init
  2. Open Podfile and replace the placeholder code with the following. If the file is not visible your Xcode Project Navigator, right-click the project root and choose Show in finder.

    platform :ios, '9.0' target :'MyNotes' do use_frameworks! # Analytics dependency pod 'AWSPinpoint', '~> 2.6.5' # other pods end
  3. Close your Xcode project and then run:

    pod install --repo-update

    If you encounter an error message that begins "[!] Failed to connect to GitHub to update the CocoaPods/Specs . . .", and your internet connectivity is working, you may need to update openssl and Ruby.


    From this point forward, open your project using the .xcworkspace file generated by cocoapods for all further development.

  4. Rebuild your app after reopening it in the workspace to resolve APIs from new libraries called in your code. This is a good practice any time you add import statements.

Initialize Amazon Pinpoint to Enable Analytics

You have just installed the AWS Mobile dependencies for your app.

To turn your analytics on, open your project using MyNotes.xcworkspace insert the following code into the didFinishLaunchwithOptions method of your app's AppDelegate.swift.

//. . . // Analytics imports import AWSCore import AWSPinpoint //. . . class AppDelegate: UIResponder, UIApplicationDelegate { //. . . // Add the pinpoint variable var pinpoint: AWSPinpoint? //. . . func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { //. . . // Initialize Pinpoint to enable session analytics pinpoint = AWSPinpoint(configuration: AWSPinpointConfiguration.defaultPinpointConfiguration(launchOptions: launchOptions)) return true } //. . . }

Now your app is setup to provide session analytics you can view in the Amazon Pinpoint console.

Run the App and Validate Results

Re-build the application and run the application in the Simulator. It should work as before. Add and delete some notes to generate analytics traffic that can be shown in the Pinpoint console.

To view the demographics and custom events:

  1. Choose Analytics on the top right to open your project in the Amazon Pinpoint console.

  2. Choose the Analytics icon on the left. You should see an up-tick in several graphs (it may take a few minutes for the data to show):

                  Image of the Amazon Pinpoint console.
  3. Choose Demographics to view the demographics information.

                  Image of the Amazon Pinpoint console Demographics tab.

If you see data within each page, you have successfully added analytics to your app. Should you release your app, you can come back here to see more details about your users.

Add Custom Analytics

Amazon Pinpoint also allows you to capture custom analytics data for events that show usage of your app's features. For this tutorial we'll send analytics each time a note is added or deleted.

To add analytics events, open ./Data/NotesContentProvider.swift where both add and delete operations happen.

Start by adding the following imports.

import AWSCore import AWSPinpoint

Add the following function and enum to the NotesContentProvider class to send AddNote and DeleteNote event analytics.

public class NotesContentProvider { // . . . // Send analytics AddNote and DeleteNote events func sendNoteEvent(noteId: String, eventType: String) { let pinpointClient = AWSPinpoint(configuration: AWSPinpointConfiguration.defaultPinpointConfiguration(launchOptions: nil)) let pinpointAnalyticsClient = pinpointClient.analyticsClient let event = pinpointAnalyticsClient.createEvent(withEventType: eventType) event.addAttribute("NoteId", forKey: noteId) pinpointAnalyticsClient.record(event) pinpointAnalyticsClient.submitEvents() } enum noteEventType: String { case AddNote = "AddNote" case DeleteNote = "DeleteNote" } }

To capture note additions, place the following sendNoteEvent function call within the insert function of that class.

/** * Insert a new record into the database using NSManagedObjectContext * * @param noteTitle the note title to be inserted * @param noteContent the note content to be inserted * @return noteId the unique Note Id */ func insert(noteTitle: String, noteContent: String) -> String { // . . . print("New Note Saved : \(newNoteId)") //Send AddNote analytics event sendNoteEvent(noteId: newNoteId, eventType: noteEventType.AddNote.rawValue) return newNoteId }

To capture note deletions, place the following sendNoteEvent function call within the delete function of that class.

/** * Delete note using NSManagedObjectContext and NSManagedObject * @param managedObjectContext the managed context for the note to be deleted * @param managedObj the core data managed object for note to be deleted * @param noteId the noteId to be delete */ public func delete(managedObjectContext: NSManagedObjectContext, managedObj: NSManagedObject, noteId: String!) { let context = managedObjectContext context.delete(managedObj) do { // . . . // Send DeletNote analytics event sendNoteEvent(noteId: noteId, eventType: noteEventType.DeleteNote.rawValue) // . . . } catch { // . . . } }

View Your Custom Analytics

To view the AddNote and DeleteNote custom analytics events, rebuild and run your app in the Simulator, add and delete notes, then return to the Amazon Pinpoint console for your project.

  1. Choose Events.

  2. Use the Event drop down to filter the event type (event types may take several minutes to appear).

                  Image of the Add note event in the Amazon Pinpoint.

Next steps