Latest 0.4
Homepage https://github.com/MrAlek/Swiftalytics
License MIT
Platforms ios 8.0, requires ARC
Frameworks UIKit
Authors

Version
License
Platform
Carthage compatible

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.

CocoaPods

To integrate Swiftalytics into your Xcode project using CocoaPods, specify it in your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'

pod 'Swiftalytics', '~> 0.4'

Then, run the following command:

$ pod install

Carthage

Carthage is a decentralized dependency manager that automates the process of adding frameworks to your Cocoa application.

You can install Carthage with Homebrew using the following command:

$ brew update
$ brew install carthage

To integrate Swiftalytics into your Xcode project using Carthage, specify it in your Cartfile:

github "MrAlek/Swiftalytics" >= 0.4

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.

Latest podspec

{
    "name": "Swiftalytics",
    "version": "0.4",
    "summary": "A declarative Swift DSL for your view tracking needs",
    "description": "Swiftalytics lets you use Aspect Oriented Programming tondeclare all the analytics view tracking for your app onnneat one-liners in a single file!nn* Supports both static and dynamic view trackingn  * Assign your own closures with correct type inferencen* Flexible tracking (not forced to viewDidAppear)n* Works great with [ARAnalytics](https://github.com/orta/ARAnalytics)n* Built for Swift 3.0",
    "homepage": "https://github.com/MrAlek/Swiftalytics",
    "license": "MIT",
    "authors": {
        "Alek Au030astrou0308m": "[email protected]"
    },
    "source": {
        "git": "https://github.com/MrAlek/Swiftalytics.git",
        "tag": "0.4"
    },
    "social_media_url": "https://twitter.com/MisterAlek",
    "platforms": {
        "ios": "8.0"
    },
    "requires_arc": true,
    "source_files": "Source/*",
    "frameworks": "UIKit"
}

Pin It on Pinterest

Share This