Trikalabs
  • Home
  • Best online TDD videos
  • Book Suggestions
  • About Me
  • Contact
Trikalabs
No Result
View All Result

AWS: Use Amazon PinPoint to collect analytics

by fragi
July 25, 2019
in AWS
Share on FacebookShare on Twitter

In this article, we will add analytics to an iOS app by using Amplify and PinPoint.

Create a project

Firstly let’s create our iOS project. Open Xcode and create a project. Let’s name it PinPoint.

Install & configure Amplify

  1. Install the Node.js and npm. (Skip if it is already installed)
  2. On the command line execute:npm install -g @aws-amplify/cli
  3. In the command line, navigate to the root folder of the Xcode project.
  4. On the command line execute: amplify configure. You will be prompted to sign in to the AWS administrator account: https://console.aws.amazon.com/. Tap “Enter” on the command line when you sign in.
  5. Now you have to specify the AWS Region. Choose the one that is nearest to your users’ location. For example, most of my users are in London so I will select the region eu-west-2
  6.  Specify a name for the new IAM user.  Identity and Access Management allow us to access our AWS services and resources securely. Choose a name or tap enter to use the suggested. You will have to tap on the provided link and go to the AWS console to finish the creation of the user. Follow the steps by using the default settings but take note of the accessKeyID and secretAccessKey before you finish the creation. (* The default settings give administrator access to the IAM user. A good practice is to give only the minimum required privileges).
  7. Enter the accessKeyID and secretAccessKey on the command line
  8. Now choose a profile. For this example select the default.
  9. Now, execute: amplify init
  10. Enter a project name, an environment name, a default editor and choose iOS
  11. Choose the default profile.

Cocoapods

  1. Install cocoapods if it is not installed.
  2. In the command line, navigate to the root folder of the Xcode project.
  3. execute pod init
  4. Open the podfile and add the following pods:
Swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Uncomment the next line to define a global platform for your project
platform :ios, '10.0'
 
target 'AWSAuthentication' do
  # Comment the next line if you're not using Swift and don't want to use dynamic frameworks
  use_frameworks!
 
  # Pods for AWSAuthentication
  pod 'AWSMobileClient', '~> 2.9.0'    
  pod 'AWSPinpoint', '~> 2.9.0'
  
  target 'AWSAuthenticationTests' do
    inherit! :search_paths
    # Pods for testing
  end
 
end

Update amplify & cocoapods

Now, let’s push our changes. In command line execute: amplify push. You should see a result like:

Now, let’s update our pods in the project. In command line execute: pod install –repo-update

Open the .xcworkspace file, and let’s drag and drop to our app the awsconfiguration.json file.

Add analytics

In teh command line execute: amplify add analytics

Then provide a resource and allow guests to send analytics.

Then execute: amplify push

Add the AnalyticsManager

Let’ s create the AnalyticsManager:

Swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// AuthenticationAnalytics.swift
protocol AuthenticationAnalytics {
    func didTapSignIn()
}
 
// AnalyticsManager.swift
import AWSPinpoint
 
class AnalyticsManager {
    
    private var pinpoint: AWSPinpoint?
    init(launchOptions: [UIApplication.LaunchOptionsKey: Any]?) {
        let pinpointConfiguration = AWSPinpointConfiguration.defaultPinpointConfiguration(launchOptions: launchOptions)
        pinpoint = AWSPinpoint(configuration: pinpointConfiguration)
    }
    
    func logEvent(name: String, attributes: [String : String], metrics: [String : NSNumber]) {
        if let analyticsClient = pinpoint?.analyticsClient {
            let event = analyticsClient.createEvent(withEventType: name)
            
            for attribute in attributes {
                event.addAttribute(attribute.value, forKey: attribute.key)
            }
            
            for metric in metrics {
                event.addMetric(metric.value, forKey: metric.key)
            }
            
            analyticsClient.record(event)
            analyticsClient.submitEvents()
        }
    }
}
 
extension AnalyticsManager: AuthenticationAnalytics {
    func didTapSignIn() {
        logEvent(name: "SignInButton", attributes: [:], metrics: [:])
    }
}

Now let’s use the AnalyticsManager. 

First let’s add a sign in UIButton in the ViewController. Also add a didTapSignIn IBAction from the button to the ViewController.

Inside the ViewController all we have to do is:

Swift
1
2
3
4
5
6
7
8
9
   var analytics: AuthenticationAnalytics?
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    @IBAction func didTapSignIn(_ sender: Any) {
        analytics?.didTapSignIn()
    }

Lastly, let’s create AnalyticsManager in the AppDelegate and pass it to the ViewController. 

Swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import UIKit
import AWSPinpoint
 
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
 
    var window: UIWindow?
    private var analyticsManager: AnalyticsManager?
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        
        guard let vc = self.window?.rootViewController as? ViewController else {
            return true
        }
        
        analyticsManager = AnalyticsManager(launchOptions: launchOptions)
        vc.analytics = analyticsManager
        
        return true
    }
 
}
 

Let’s run the app and tap the sign in button.

Now if we open the PinPoint from the AWS console, we can see our events:

Resources:

https://aws-amplify.github.io/docs/ios/analytics

Tags: AmplifyPinPoint
fragi

fragi

Related Posts

AWS: AppSync – Real-time data with subscriptions.
AWS

AWS: AppSync – Real-time data with subscriptions.

July 25, 2019

In the previous article (http://trikalabs.com/aws-appsync-swift/) we set up AppSync into our app. In this article, we will see how we...

AWS: Build data-driven apps with real-time and offline capabilities by using AppSync and Swift.
AWS

AWS: Build data-driven apps with real-time and offline capabilities by using AppSync and Swift.

July 25, 2019

In this article, we will use the AppSync framework to fetch and create data for our iOS app. By using...

AWS: Integrate User Pools with Clean Architecture
AWS

AWS: Integrate User Pools with Clean Architecture

July 25, 2019

In the previous article(http://trikalabs.com/aws-integrating-user-pools-for-ios-apps/) we integrated User Pools into an iOS project. Also in the article http://trikalabs.com/clean-architecture/,we learned how to use...

AWS: Integrate User Pools on iOS Apps
AWS

AWS: Integrate User Pools on iOS Apps

July 25, 2019

Authenticate and manage users is a common task/need for many apps. In this article, we will set up an Amazon Cognito...

Next Post
AWS: Build data-driven apps with real-time and offline capabilities by using AppSync and Swift.

AWS: Build data-driven apps with real-time and offline capabilities by using AppSync and Swift.

AWS: AppSync – Real-time data with subscriptions.

AWS: AppSync - Real-time data with subscriptions.

Add SwiftLint to your iOS app

Add SwiftLint to your iOS app

  • Advertise
  • Privacy & Policy
  • Contact

© 2019 Trikalabs Ltd. All rights reserved.

No Result
View All Result
  • Home
  • About Me
  • A curated list with the best free online TDD videos
  • Book Suggestions
  • Pinner Code Club

© 2019 Trikalabs Ltd. All rights reserved.