Skip to content

Latest commit

 

History

History
126 lines (100 loc) · 2.68 KB

FormsAnalytics.md

File metadata and controls

126 lines (100 loc) · 2.68 KB

FormsAnalytics

FormsAnalytics collect user events and sends to multiple providers.

Import

import FormsAnalytics

Dependencies

FormsInjector.framework
FormsLogger.framework

External dependencies

FirebaseAnalytics.framework
FirebaseCore.framework
FirebaseCoreDiagnostics.framework
FirebaseInstallations.framework
GoogleAppMeasurement.framework
GoogleDataTransport.framework
GoogleDataTransportCCTSupport.framework
GoogleUtilities.framework
nanopb.framework
PromisesObjC.framework

Integration

Library uses Firebase service. You should create and configure project here. Then download GoogleService-Info.plist and add to your project. It's also important to add URL Type in your Target Info's - URL Schemas is your REVERSED_CLIENT_ID from GoogleService-Info.plist.

Usage

Configuration

class DemoAnalyticsFirebaseProvider: AnalyticsProvider {
    let name: String = "Firebase"
    
    func logEvent(_ event: AnalyticsEvent,
                  _ parameters: [String: Any],
                  _ userProperties: [String: String]) {
        for userProperty in userProperties {
            FirebaseAnalytics.Analytics.setUserProperty(
                userProperty.value,
                forName: userProperty.key)
        }
        FirebaseAnalytics.Analytics.logEvent(
            event.name,
            parameters: parameters)
    }
    
    func setUserId(_ userId: String?) {
        FirebaseAnalytics.Analytics.setUserID(userId)
    }
}
Analytics.register([
    DemoAnalyticsFirebaseProvider()
])

Event definition

enum DemoEvent: String, AnalyticsTag {
    case demoEvent = "demo_event"
}

Event parameters definition

enum DemoEvent: AnalyticsEvent {
    case demoEvent(value: Int?)
        
    var name: String {
        switch self {
        case .demoEvent: return "demo_event"
        }
    }
    var parameters: [String: Any?] {
        switch self {
        case .demoEvent(let value):
            return ["value": value]
        }
    }
    var userProperties: [String: String?] {
        switch self {
        case .demoEvent(let value):
            return ["value": value.description]
        }
    }
}

Sending event

Analytics.log(DemoEvent.demoEvent(value: 12))

Custom provider

class DemoAnalyticsCustomProvider: AnalyticsProvider {
    let name: String = "Custom"
    
    func logEvent(_ event: AnalyticsEvent,
                  _ parameters: [String: Any],
                  _ userProperties: [String: String]) {
        print("Some log")
    }
    
    func setUserId(_ userId: String?) {
        print("Some set")
    }
}