Habit Analytics
  • What is the Habit Analytics Platform?
  • How to integrate devices with Habit Analytics
  • How to integrate an application with Habit Analytics
  • How to create a custom mobile app with Habit Analytics
  • Selfcare
    • What is Selfcare?
    • Projects
    • Integrations
  • SDK
    • Getting Started
    • iOS Quick Start
      • Installation
      • Xcode Project Configuration
      • Usage
        • SDK Initialization
        • Configuration
        • Tracking UX Events
        • Status Codes
Powered by GitBook
On this page
  • Importing the SDK
  • Initialize SDK
  • Configuration
  • Set External ID (Optional)
  • HabitAnalyticsDelegate
  • Background fetch
  • Logout

Was this helpful?

  1. SDK
  2. iOS Quick Start
  3. Usage

SDK Initialization

PreviousUsageNextConfiguration

Last updated 4 years ago

Was this helpful?

Importing the SDK

First start by importing the HabitAnalytics module in the classes in which you're going to use it.

import HabitAnalytics

Initialize SDK

In the didFinishLaunchingWithOptions method of your AppDelegate, call the initialize function of the SDK. The analyticsID and analyticsAPIKey parameters are mandatory and must be valid in order for the SDK to start. If you don't have credentials yet, check for more info.

func initialize(analyticsID: String, analyticsAPIKey: String, configuration : Configuration?, externalID: String? = nil, completion: @escaping (_ status : HabitStatusCode) -> Void)

Please note that the analyticsID and analyticsAPIKey should not be hard-coded, unless for testing purposes. It is not secure and can lead to leaked credentials. In your app, ideally, load these credentials from a secure source such as a remote server, and store them securely on the device.

Configuration

The configuration object sets the capabilities that are to be enabled in the SDK, as well as if the permissions for the capabilities have been granted to the App. By default all these values are false so make sure to enable the required ones.

In the example below, the developer wishes to use the location capability but hasn't requested the user for permission yet. However, the permission can be updated at a later time after the permission has been requested. .

let config = Configuration()
        
config.capabilities.bluetooth = true
config.capabilities.location = true
config.capabilities.motion = true
config.capabilities.ux_events = true

config.permissions.location =  false
config.permissions.bluetooth =  true
config.permissions.motion = true

config.uxEventsConfig.token = "<valid UX token>"

Set External ID (Optional)

While user analytics information is anonymous, an external ID can be set so that the data can be correlated with a particular user.

If an external ID is set, it should follow the best practices of user data consent.

To set an External user ID, it should either be provided upon initialization of the SDK or later by calling the appropriate function after a user performs a login in the app.

Set External ID during initialization:

HabitAnalytics.shared.initialize(analyticsID: String, analyticsAPIKey: String, configuration: Configuration, externalID: String) { (statusCode) in
    debugPrint("\(statusCode) : \(HabitStatusCodes.getDescription(code: statusCode)")
}

Set External ID after initialization:

HabitAnalytics.shared.setExternalID(identifier: String) { (statusCode) in
    debugPrint("\(statusCode) : \(HabitStatusCodes.getDescription(code: statusCode)")
}

HabitAnalyticsDelegate

You can listen to the status changes of the SDK. In order to do that, implement the delegate HabitAnalyticsDelegate and the following method:

func HabitAnalyticsStatusChange(statusCode: HabitStatusCode) {
    print("\(statusCode) : \(HabitStatusCodes.getDescription(code: statusCode))")
}

Background fetch

Using the background app refresh mechanism, the OS allows some minimal work to be done from time to time. Ensure you have this code on didFinishLaunchingWithOptions to set the minimum background fetch interval:

UIApplication.shared.setMinimumBackgroundFetchInterval(TimeInterval(1800))

Also add the following method that is executed when background fetch is triggered:

func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

    HabitAnalytics.shared.handleBGFetch { (result) in
        completionHandler(result)
    }
}

Logout

To logout a user, just call the corresponding method.

Make sure to call the logout method when changing between users.

HabitAnalytics.shared.logout(completion: { (statusCode) in
    debugPrint("\(statusCode) : \(HabitStatusCodes.getDescription(code: statusCode)")
})

For more details on the configuration parameter please check the section of this guide.

Configuration
here
See more info