Analytics

public class Analytics

Records analytics data and sends it to the Mobile Analytics service.

To subscribe to automated analytics recording, pass the DeviceEvent options that you want recorded in initialize(appName:apiKey:hasUserContext:deviceEvents:).

Custom events can be recorded with log(metadata:). You can also define the current application user with userIdentity using, for example, the user’s login information.

When you are ready to send all of the recorded data to the Mobile Analytics service, use send(completionHandler:).

  • Determines whether analytics data will be recorded.

    Declaration

    Swift

    public static var isEnabled: Bool
  • Identifies the current application user. To reset the userIdentity, set the value to nil.

    Declaration

    Swift

    public static var userIdentity: String? { get set }
  • Record analytics data.

    Analytics logs are added to the log file until the file size is greater than Logger.maxLogStoreSize. At this point, the first half of the stored logs will be deleted to make room for new log data.

    When ready, use send(completionHandler:) to send the recorded data to the Mobile Analytics service.

    Note

    Do not supply values for the file, function, or line parameters. These parameters take default values to automatically record the file, function, and line in which this method was called.

    Declaration

    Swift

    public static func log(metadata: [String : Any], file: String = #file, function: String = #function, line: Int = #line)

    Parameters

    metadata

    The analytics data

  • The required initializer for the Analytics class when communicating with a Mobile Analytics service.

    Here you can choose what data you want to have monitored by Analytics, including user identities, user location, app lifecycle events, and network requests.

    Note

    This method must be called after initializing BMSClient with BMSClient.sharedInstance.initialize(bluemixRegion:) from the BMSCore framework, and before calling send(completionHandler:) or Logger.send(completionHandler:).

    Declaration

    Swift

    public static func initialize(appName: String?, apiKey: String?, hasUserContext: Bool = false, collectLocation: Bool = false, deviceEvents: DeviceEvent...)

    Parameters

    appName

    The application name. Should be consistent across platforms (i.e. Android and iOS).

    apiKey

    A unique ID used to authenticate with the Mobile Analytics service.

    hasUserContext

    If false, user identities will be automatically recorded using the device on which the app is installed. If you want to define user identities yourself using Analytics.userIdentity, set this parameter to true.

    collectLocation

    Determines whether Analytics should automatically monitor the user’s current location. Before location can be retrieved, you must first request permission from the user with CLLocationManager requestWhenInUseAuthorization().

    deviceEvents

    Device events that will be recorded automatically by the Analytics class.

  • Log the user’s current location once.

    Important

    Before calling this method, make sure that you have requested permission to use location services from the user (using CLLocationManager requestWhenInUseAuthorization()), and set the collectLocation parameter to true in the Analytics.initialize(appName:apiKey:hasUserContext:collectLocation:deviceEvents:) method.

    Declaration

    Swift

    public static func logLocation()
  • Send the accumulated analytics logs to the Mobile Analytics service.

    Analytics logs can only be sent if the BMSClient was initialized with BMSClient.sharedInstance.initialize(bluemixRegion:) from the BMSCore framework.

    Declaration

    Swift

    public static func send(completionHandler userCallback: BMSCompletionHandler? = nil)

    Parameters

    completionHandler

    Optional callback containing the results of the send request.

  • Trigger feedback Mode

    Note: Feedback Mode can only be triggered if the BMSClient was initialized with BMSClient.sharedInstance.initialize(bluemixRegion:) from the BMSCore framework.

    Declaration

    Swift

    public static func triggerFeedbackMode()