Giter VIP home page Giter VIP logo

swiftalytics's Introduction

Swiftalytics

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
  • Built for Swift 1.2
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.trackingNameForViewController(self) {
            println("Tracked view controller: "+name)
        }
    }
}

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 Swiftalytics.swift source file directly in your project.

Cocoapods

CocoaPods 0.36 adds supports for Swift and embedded frameworks. You can install it with the following command:

$ gem install cocoapods --pre

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.1'

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.1

Usage

Declaring tracking names

Use the setTrackingNameForViewController 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.setTrackingNameForViewController(MasterViewController.self, name: "Start view")

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

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

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

Retrieving tracking names

The trackingNameForViewController 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.trackingNameForViewController(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: (T -> () -> String)) {
    Swiftalytics.setTrackingNameForViewController(trackClassFunction)
}

private func >> <T: UIViewController>(left: T.Type, @autoclosure right: () -> String) {
    Swiftalytics.setTrackingNameForViewController(left, name: right)
}
private func >> <T: UIViewController>(left: T.Type, right: TrackingNameType) {
    Swiftalytics.setTrackingNameForViewController(left, trackingType: right)
}
private func >> <T: UIViewController>(left: T.Type, right: (T -> String)) {
    Swiftalytics.setTrackingNameForViewController(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.

swiftalytics's People

Contributors

mralek avatar

Watchers

James Cloos avatar Gunter Hager avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.