Swiftalytics 0.4

Swiftalytics 0.4

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Sep 2016
SPMSupports SPM

Maintained by Alek Åström.



Swiftalytics lets you use Aspect Oriented Programming to declare all the analytics view tracking for your app on neat one-liners in a single file!

  • Supports both static and dynamic view tracking
    • Assign your own closures with correct type inference
  • Flexible tracking (not forced to viewDidAppear)
  • Works great with ARAnalytics
func setupScreenTracking() {
    AuthorsViewController.self  >>   "Authors (start)"
    QuoteViewController.self    >> { "Quote: "+$0.author.name }
    NewQuoteViewController.self >>   .navigationTitle
    RandomQuoteViewController.computedPageName<<
}

extension UIViewController {

    // Swizzle viewDidAppear and report to your favorite analytics service
    func swizzled_viewDidAppear(animated: Bool) {
        if let name = Swiftalytics.trackingName(for: self) {
            ARAnalytics.pageView(name)
            println("Tracked view controller: "+name)
        }
    }
}

Swift versions

  • Swiftalytics 0.4 and above uses Swift 3.0
  • Swiftalytics 0.3 of the framework uses Swift 2.3
  • Swiftalytics 0.2 uses Swift 2.2

Installation

Embedded frameworks require a minimum deployment target of iOS 8 or OS X Mavericks.

To use Swiftalytics with a project targeting iOS 7, you must include the source files directly in your project.

Usage

Declaring tracking names

Use the setTrackingName(for:) function to declare a tracking name for a specific view controller type. The method has several different type signatures for static and dynamic typing.

// Static
Swiftalytics.setTrackingName(for: MasterViewController.self, name: "Start view")

// Dynamic with closure
Swiftalytics.setTrackingNameForViewController(for: DetailViewController.self) { "Detail: \($0.dataObject.name)" }

// Dynamic using navigation item title
Swiftalytics.setTrackingNameForViewController(for: SettingsViewController.self, trackingType:.navigationTitle)

// Dynamic with function
Swiftalytics.setTrackingNameForViewController(for: OtherViewController.functionProducingAString)

Retrieving tracking names

The trackingName(for:) function is used to retrieve the name that has been declared earlier for a specific view controller. When using dynamic tracking, the tracking name for that view controller is computed when calling this function.

Reporting view tracking to analytics providers

extension UIViewController {
    func viewDidAppear(animated: Bool) {
        if let name = Swiftalytics.trackingName(for: self) {
            // Report to your analytics service
            println("Tracked view controller: "+name)
        }
    }
}

By extending UIViewController, you can choose when in the view lifecycle to send analytics events to your provider's SDK or for internal logging. ARAnalytics is highly recommended to decouple your app from your chosen provider.

Recommended setup with custom operators

The recommended approach to use Swiftalytics is with custom operators, in order to achieve a clean DSL. Since operator overload polluting is a problem in Swift, no operators are declared in the framework. Instead it is recommended to create a Swift file for view tracking with private scoped operators and declare all page names there.

postfix operator <<
private postfix func <<<T: UIViewController>(trackClassFunction: @escaping ((T) -> () -> String)) {
    Swiftalytics.setTrackingName(for: trackClassFunction)
}

private func >> <T: UIViewController>(left: T.Type, right: @autoclosure () -> String) {
    Swiftalytics.setTrackingName(for: left, name: right)
}
private func >> <T: UIViewController>(left: T.Type, right: TrackingNameType) {
    Swiftalytics.setTrackingName(for: left, trackingType: right)
}
private func >> <T: UIViewController>(left: T.Type, right: @escaping ((T) -> String)) {
    Swiftalytics.setTrackingName(for: left, nameFunction: right)
}

See ScreenTracking.swift in the demo project for a proposed setup.

Demo

The included example project shows a recommended view tracking setup with both static and dynamic tracking.

License

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