Swiftpack.co - Package - thejohnlima/BaseTracking

BaseTracking

GitHub release Build Status CocoaPods GitHub repo size License Platform

BaseTracking is the easiest way to setup your analytics in Swift.

❗️Requirements

  • iOS 9.3+
  • Swift 5.0+

βš’ Installation

Swift Package Manager

BaseTracking is available through SPM. To install it, follow the steps:

Open Xcode project > File > Swift Packages > Add Package Dependecy

After that, put the url in the field: https://github.com/thejohnlima/BaseTracking.git

CocoaPods

BaseTracking is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'BaseTracking'

and run pod install

πŸŽ“ How to use

Example using Firebase Analytics:

Import library in your swift file

import BaseTracking

In your tracking manager file create an extension for BaseTrackingProtocol:

extension TrackingManager: BaseTrackingProtocol {
  func configure() {
    FirebaseApp.configure()
  }

  func track(event log: BaseTrackingEventLogData) {
    Analytics.logEvent(log.name, parameters: log.parameters)
  }

  func track(view data: BaseTrackingViewData) {
    Analytics.setScreenName(data.name, screenClass: data.className)
  }
}

And create another one to prepare protocol methods

extension BaseTrackingEventLog {
  static func trackView(name: ScreenName, className: ClassName? = nil) {
    let data = BaseTrackingViewData(
      name: name.rawValue as? String ?? "",
      className: className?.rawValue as? String
    )
    TrackingManager.shared.track(view: data)
  }

  static func trackEvent(name: EventName, parameters: EventParameters? = nil) {
    let data = BaseTrackingEventLogData(
      name: name.rawValue as? String ?? "",
      parameters: parameters?.rawValue as? [String: Any]
    )
    TrackingManager.shared.track(event: data)
  }
}

Now, create a tracking file for some area of the app.
Example:

struct HomeTracking: BaseTrackingEventLog {
  enum ScreenName: String {
    case home
  }

  enum EventName: String {
    case showDetails = "show_home_details"
  }

  typealias ClassName = CustomRawRepresentable
  typealias EventParameters = CustomRawRepresentable
}

extension HomeTracking {
  static func getSelectButtonParameter(_ value: Any) -> EventParameters? {
    let item: [String: Any] = ["button_name": value]
    return EventParameters(rawValue: item)
  }
}

Than, let's get tracking some data:

class HomeViewController: UIViewController {

  override func viewDidLoad() {
    super.viewDidLoad()
  }

  override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    HomeTracking.trackView(name: .home)
  }

  @IBAction private func showDetails(sender: Any?) {
    let buttonName = (sender as? UIBarButtonItem)?.title ?? ""
    let parameters = HomeTracking.getSelectButtonParameter(buttonName)
    HomeTracking.trackEvent(name: .showDetails, parameters: parameters)
  }
}

If you need examples unsing GA, take a look at demo project.

πŸ™‹πŸ»β€ Communication

  • If you found a bug, open an issue.
  • If you have a feature request, open an issue.
  • If you want to contribute, submit a pull request. πŸ‘¨πŸ»β€πŸ’»

πŸ“œ License

BaseTracking is under MIT license. See the LICENSE file for more info.

Github

link
Stars: 1
Help us keep the lights on

Dependencies

Used By

Total: 0

Releases

1.0.2 - Aug 25, 2019

  • Add support for SPM;

1.0.1 - Mar 14, 2019

β€’ Adjust demo project; β€’ Adjust extension access modifier;

1.0.0 - Mar 14, 2019

First release version