Latest 0.0.1
Homepage https://github.com/kuyazee/AnalyticsManagerService
License MIT
Platforms ios 8.0
Authors

Twitter
Github

A Protocol Oriented Analytics Wrapper. For more Information read my article on Medium

Installation

To install the AnalyticsManagerService via CocoaPods, simply use the add this in your podfile and then run pod install

pod 'AnalyticsManagerService'

Usage

Simply create a subclass of AnalyticsServiceProtocol and AnalyticsEventProtocol for example we’ll use Fabric for the example

// AnalyticsServiceProtocol
class FabricAnalyticsService: AnalyticsServiceProtocol {
    func initialize(application: UIApplication, launchOptions: [UIApplicationLaunchOptionsKey : Any]?) {
        Fabric.with([Answers.self])
    }

    func send(event: AnalyticsEventProtocol) {
        Answers.logCustomEvent(withName: event.name, customAttributes: event.parameters)
    }
}
// AnalyticsEventProtocol
enum AnalyticsEvent: AnalyticsEventProtocol {
    case userLoggedIn
    case userLoggedOut
    case productSelected(productId: Int)

    var name: String { 
        switch self {
        case .userLoggedIn:     return "User Logged In"
        case .userLoggedOut:    return "User Logged Out"
        case .productSelected:  return "Product Selected"
        }
    }

    var parameters: [String: Any]? {
        switch self {
        case .userLoggedIn:                 return nil
        case .userLoggedOut:                return nil
        case .productSelected(id: let id):  return ["product_id": productId]
        }
    }

    var isTrackedOnAnalyticsServices: [AnalyticsServiceProtocol.Type] { 
        switch self {
        /// will be sent on 2 services 
        case .userLoggedIn:     return [FacebookAnalyticsService.self, FabricAnalyticsService.self]

        /// will not be sent anywhere
        case .userLoggedOut:    return [] 

        /// will be sent on only one service
        case .productSelected:  return [FacebookAnalyticsService.self]
        }
    }
}

Then we add the service in our UIApplicationDelegate

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    ...

    // Add our services
    AnalyticsManagerService.shared.add(service: FacebookAnalyticsService())
    AnalyticsManagerService.shared.add(service: FabricAnalyticsService())

    // Initialize the analytics
    AnalyticsManagerService.shared.initialize(application: application: launchOptions: launchOptions)

    ...
}

Using an event would be as simple as accessing the singleton and using the send(event: _) function

AnalyticsManagerService.shared.send(event: AnalyticsEvent.userLoggedIn)
AnalyticsManagerService.shared.send(event: AnalyticsEvent.productSelected(productId: productModel.id))

Latest podspec

{
    "name": "AnalyticsManagerService",
    "version": "0.0.1",
    "summary": "A Protocol Oriented Analytics Wrapper",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Zonily Jame Pesquera": "[email protected]"
    },
    "social_media_url": "http://twitter.com/kuyazee",
    "platforms": {
        "ios": "8.0"
    },
    "source": {
        "git": "https://github.com/kuyazee/AnalyticsManagerService.git",
        "tag": "0.0.1"
    },
    "source_files": [
        "Classes",
        "AnalyticsServiceProtocol/**/*.{h,swift}"
    ],
    "description": "Reusable Analytics Wrapper",
    "homepage": "https://github.com/kuyazee/AnalyticsManagerService",
    "pushed_with_swift_version": "3.0"
}

Pin It on Pinterest

Share This