The minimum library to track screen UIViewController
.
Hook screen viewWillAppear
, viewWillDisappear
. And hook screen only ViewController in ScrollView.
If you use for Android, see Android-ScreenTracker.
Xcode10.2, Swift5.x
github "roana0229/iOS-ScreenTracker" >= 2.0
import UIKit
import ScreenTracker
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
ScreenTracker.initialize(trackStarted: { trackingMarker in
// tracked viewWillAppear
}, trackEnded: { trackingMarker, exposureTime in
// tracked viewWillDisappear
})
return true
}
...
}
use TrackingMarker
import ScreenTracker
class DetailViewController: UIViewController, TrackingMarker {
var id: Int?
func screenName() -> String {
return "detail"
}
func screenParameter() -> [String : Any] {
var params: [String: Any] = [:]
if let id = id {
params["id"] = id
}
return params
}
...
}
If you are tracking everything, you should write TrackingMarker
in BaseViewController
.
use PagingParentTrackingMarker
and PagingChildTrackingMarker
import UIKit
import ScreenTracker
class ViewController: UIViewController, PagingParentTrackingMarker {
...
override func viewDidLoad() {
super.viewDidLoad()
setTabScrollViewForTracking(scrollView)
let vc1 = ...
page1Container.addSubview(vc1.view)
addChildViewController(vc1)
vc1.didMove(toParentViewController: self)
let vc2 = ...
page2Container.addSubview(vc2.view)
addChildViewController(vc2)
vc2.didMove(toParentViewController: self)
let vc3 = ...
page3Container.addSubview(vc3.view)
addChildViewController(vc3)
vc3.didMove(toParentViewController: self)
}
...
}
call notifyTabChangedForTracking(index:)
extension ViewController: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let newPosition = Int(round(scrollView.contentOffset.x / scrollView.bounds.width))
if currentPosition != newPosition {
currentPosition = newPosition
notifyTabChangedForTracking(index: newPosition)
}
}
}
import UIKit
import ScreenTracker
class PagingChildViewController: UIViewController, PagingChildTrackingMarker {
var pageIndex: Int!
func screenName() -> String {
return "tab"
}
func screenParameter() -> [String : Any] {
var params: [String: Any] = [:]
if let index = pageIndex {
params["page_index"] = index
}
return params
}
....
}
see LICENSE
file.
Copyright (c) 2018 Kaoru Tsutsumishita roana.enter@gmail.com