ImpressionKit
这是一个用于分析 iOS 中UIView(例如UIView的暴露)印象事件的用户行为跟踪(UBT)工具。
工作原理:通过SwiftHook挂钩UIView的didMoveToWindow
方法,定期检查视图是否在屏幕上。
如何使用 ImpressionKit
主要 APIs
这相当简单。
// UIKit
UIView().detectImpression { (view, state) in
if state.isImpressed {
print("This view is impressed to users.")
}
}
// SwiftUI
Color.red.detectImpression { state in
if state.isImpressed {
print("This view is impressed to users.")
}
}
使用 ImpressionGroup
为 UICollectionView、UITableView、列表或其他可重用视图情况。
// UIKit
var group = ImpressionGroup.init {(_, index: IndexPath, view, state) in
if state.isImpressed {
print("impressed index: \(index.row)")
}
}
...
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! Cell
self.group.bind(view: cell, index: indexPath)
return cell
}
// SwiftUI
var group = ImpressionGroup.init { (_, index: Int, _, state) in
if state.isImpressed {
print("impressed index: \(index)")
}
}
var body: some View {
List(0 ..< 100) { index in
CellView(index: index)
.frame(height: 100)
.detectImpression(group: group, index: index)
}
}
更多 APIs
修改检测(扫描)间隔(以秒为单位)。detectionInterval
值越小代表精度越高,但CPU消耗也越高。
UIView.detectionInterval = 0.1 // apply to all views
UIView().detectionInterval = 0.1 // apply to the specific view. `UIView.detectionInterval` will be used if it's nil.
ImpressionGroup().detectionInterval = 0.1 // apply to the group. `UIView.detectionInterval` will be used if it's nil.
修改屏幕上画面持续时间的阈值(以秒为单位)。若画面在屏幕上的持续时间超过此阈值,可能会触发一次印象。
UIView.durationThreshold = 2 // apply to all views
UIView().durationThreshold = 2 // apply to the specific view. `UIView.durationThreshold` will be used if it's nil.
ImpressionGroup().durationThreshold = 2 // apply to the group. `UIView.durationThreshold` will be used if it's nil.
修改屏幕上画面面积比率的阈值(范围从0到1)。若画面区域的百分比超过此阈值,可能会触发一次印象。
UIView.areaRatioThreshold = 0.4 // apply to all views
UIView().areaRatioThreshold = 0.4 // apply to the specific view. `UIView.areaRatioThreshold` will be used if it's nil.
ImpressionGroup().areaRatioThreshold = 0.4 // apply to the group. `UIView.areaRatioThreshold` will be used if it's nil.
修改画面的透明度阈值(范围从0到1)。若画面的透明度超过此阈值,可能会触发一次印象。
UIView.alphaThreshold = 0.4 // apply to all views
UIView().alphaThreshold = 0.4 // apply to the specific view. `UIView.alphaThreshold` will be used if it's nil.
ImpressionGroup().alphaThreshold = 0.4 // apply to the group. `UIView.alphaThreshold` will be used if it's nil.
在某些情况下重新触发印象事件。
// Retrigger the impression event when a view left from the screen (The UIViewController (page) is still here, Just the view is out of the screen).
public static let leftScreen = Redetect(rawValue: 1 << 0)
// Retrigger the impression event when the UIViewController of the view disappear.
public static let viewControllerDidDisappear = Redetect(rawValue: 1 << 1)
// Retrigger the impression event when the App did enter background.
public static let didEnterBackground = Redetect(rawValue: 1 << 2)
// Retrigger the impression event when the App will resign active.
public static let willResignActive = Redetect(rawValue: 1 << 3)
UIView.redetectOptions = [.leftScreen, .viewControllerDidDisappear, .didEnterBackground, .willResignActive] // apply to all views
UIView().redetectOptions = [.leftScreen, .viewControllerDidDisappear, .didEnterBackground, .willResignActive] // apply to the specific view. `UIView.redetectOptions` will be used if it's nil.
ImpressionGroup().redetectOptions = [.leftScreen, .viewControllerDidDisappear, .didEnterBackground, .willResignActive] // apply to the group. `UIView.redetectOptions` will be used if it's nil.
更多信息请参考示例。
如何集成ImpressionKit
ImpressionKit可以通过cocoapods进行集成。
pod 'ImpressionKit'
或者使用Swift Package Manager。从3.1.0开始支持SPM。
要求
- iOS 11.0+(UIKit)
- iOS 13.0+(SwiftUI)
- Xcode 11+