Search code examples
iosswift3amazon-mobile-analytics

AWS analytics - swift 3 - iOS?


Im trying to use mobile Analytics with Swift 3 and currently have AWSCore 2.5.9 and AWSMobileAnalytics 2.5.9 via CocoaPods but I'm unable to record any event. My partner has successfully logged events using Android with the identical CognitoIdentityPoolId that I'm using and I also confirmed that it has AmazonMobileAnalyticsFullAccess policy attached.

I have the following code inside my AppDelegate's didFinishLaunchingWithOptions

  let credentialsProvider = AWSCognitoCredentialsProvider(regionType:.USWest2,
                  identityPoolId:"us-west-2:theRestOfMyID")

      let configuration = AWSServiceConfiguration(region:.USWest2,
                                                  credentialsProvider:credentialsProvider)

      AWSServiceManager.default().defaultServiceConfiguration = configuration

      let analyticsConf = AWSMobileAnalyticsConfiguration.init()

      analyticsConf.serviceConfiguration = AWSServiceManager.default().defaultServiceConfiguration

      _ = AWSMobileAnalytics.init(forAppId: "MyMobileAnalyticsAppId", configuration: analyticsConf)

Which results in a console output of:

2017-07-20 16:39:53:875 test2[7288:125553] Mobile Analytics SDK(2.0-alpha) Initialization successfully completed.
2017-07-20 16:39:53:875 test2[7288:125635] 
==========Batch Object==========
{"attributes":{"_session.id":"82e879d9-C9E6D9C7-20170720-233953870","ver":"v2.0","_session.startTime":"2017-07-20T23:39:53.872Z"},"event_type":"_session.start","timestamp":"2017-07-20T23:39:53.873Z"}
2017-07-20 16:39:53:876 test2[7288:125635] Event: '_sess...' recorded to local filestore

I have a button where I record an event and then try to submit events:

 let eventClient = AWSMobileAnalytics(forAppId: "MyMobileAnalyticsAppId").eventClient

      guard let client = eventClient else {
         print("Error creating AMA event client")
         return
      }
      guard let event = client.createEvent(withEventType: "test_50_logIn") else {
         print("Error creating AMA event")
         return
      }
      event.addAttribute("username", forKey: "sample")
      event.addAttribute("device", forKey: "ios")
      client.record(event)

      client.submitEvents()

However, submitEvents always triggers the following error which for two days i have been unable to resolve. As a result I have been unable to record a single iOS event.

2017-07-20 16:46:01:736 test2[7566:129412] Unable to successfully deliver events to server. Error Message:Error Domain=NSURLErrorDomain Code=-1003 "A server with the specified hostname could not be found." UserInfo={NSUnderlyingError=0x6080002438a0 {Error Domain=kCFErrorDomainCFNetwork Code=-1003 "(null)" UserInfo={_kCFStreamErrorCodeKey=8, _kCFStreamErrorDomainKey=12}}, NSErrorFailingURLStringKey=https://mobileanalytics.us-west-2.amazonaws.com/2014-06-05/events, NSErrorFailingURLKey=https://mobileanalytics.us-west-2.amazonaws.com/2014-06-05/events, _kCFStreamErrorDomainKey=12, _kCFStreamErrorCodeKey=8, NSLocalizedDescription=A server with the specified hostname could not be found.}

I see no additional steps under the integration guide, does anyone know what the problem may be if the IDs work perfectly fine in the android integration? Thank you!


Solution

  • Mobile Analytics isn't available in US-West (Docs).

    Q: What AWS Regions is the Amazon Mobile Analytics service available in?

    Currently, Amazon Mobile Analytics is available in the AWS US East (N. Virginia) Region.

    You just need to change the region in your AWSServiceConfiguration:

    let serviceConfiguration = AWSServiceConfiguration(region: .USEast1, credentialsProvider: credentialsProvider)
    let analyticsConfiguration = AWSMobileAnalyticsConfiguration()
    analyticsConfiguration.serviceConfiguration = serviceConfiguration
    _ = AWSMobileAnalytics(forAppId: "MyMobileAnalyticsAppId", configuration: analyticsConfiguration)
    

    Endpoints:
    https://mobileanalytics.us-west-2.amazonaws.com => Doesn't work
    https://mobileanalytics.us-east-1.amazonaws.com/ => Does