/iOS-ScreenTracker

Screen Tracking on iOS.

Primary LanguageSwiftApache License 2.0Apache-2.0

iOS-ScreenTracker

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.

demo

Usage

1. Import this library by Carthage

Xcode10.2, Swift5.x

github "roana0229/iOS-ScreenTracker" >= 2.0

Look at the sample code

2. Initialize ScreenTracker

Track UIViewController
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
    }

    ...
}

Look at the sample code

3. Implement Protocol TrackingMarker

Track UIViewController

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
    }

    ...
}

Look at the sample code

If you are tracking everything, you should write TrackingMarker in BaseViewController.

Track PagingViewController(UIViewController in UIScrollView)

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)
    }

    ...
}

Look at the sample code

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)
        }
    }

}

Look at the sample code

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
    }

    ....
}

Look at the sample code

Sample Tracking Logger Utility

Look at the sample code

LICENSE

see LICENSE file.

Copyright (c) 2018 Kaoru Tsutsumishita roana.enter@gmail.com