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 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)
}
}
}
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 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 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
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)
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.
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.
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.
The included example project shows a recommended view tracking setup with both static and dynamic tracking.
Swiftalytics is available under the MIT license. See the LICENSE file for more info.