98 lines
2.8 KiB
Swift
98 lines
2.8 KiB
Swift
//
|
|
// MabAnalyticsEvents.swift
|
|
// Pods
|
|
//
|
|
// Created by 250102 on 2025/5/10.
|
|
//
|
|
|
|
// 单例实现
|
|
internal class MabAnalyticEvents {
|
|
// 单例访问点
|
|
static let shared = MabAnalyticEvents()
|
|
|
|
// 私有初始化方法确保单例模式
|
|
private init() {}
|
|
|
|
static func faLoad(
|
|
faUnitId: String,
|
|
adType: AdType,
|
|
aggregatorId: String,
|
|
adUnitId: String,
|
|
requestReason: RequestReason?
|
|
) {
|
|
var params: [String: Any] = [:]
|
|
params["fa_unitid"] = faUnitId
|
|
params["aggregator_id"] = aggregatorId
|
|
if let requestReason = requestReason {
|
|
params["req_type"] = requestReason.statsName
|
|
}
|
|
params["ad_format"] = adType.label
|
|
params["ad_unit_name"] = shrink(adUnitId)
|
|
|
|
AnalyticsEvents.shared.onEvent(name: "fa_load", param: params)
|
|
}
|
|
|
|
static func faLoaded(
|
|
faUnitId: String,
|
|
adType: AdType,
|
|
adSource: String?,
|
|
aggregatorId: String,
|
|
adUnitId: String,
|
|
durationInMillis: Int64?
|
|
) {
|
|
guard let durationInMillis = durationInMillis else {
|
|
return
|
|
}
|
|
|
|
AnalyticsEvents.shared.onEvent(
|
|
name: "fa_loaded",
|
|
pairs:
|
|
("fa_unitid", faUnitId),
|
|
("aggregator_id", aggregatorId),
|
|
("ad_format", adType.label),
|
|
("duration", durationInMillis),
|
|
("ad_unit_name", shrink(adUnitId)),
|
|
("ad_source", (adSource ?? "unknown"))
|
|
)
|
|
}
|
|
|
|
static func faFailed(
|
|
faUnitId: String,
|
|
adType: AdType,
|
|
aggregatorId: String,
|
|
adUnitId: String,
|
|
durationInMillis: Int64?,
|
|
errorCode: Int
|
|
) {
|
|
guard let durationInMillis = durationInMillis else {
|
|
return
|
|
}
|
|
|
|
AnalyticsEvents.shared.onEvent(
|
|
name: "fa_failed",
|
|
pairs:
|
|
("fa_unitid", faUnitId),
|
|
("aggregator_id", aggregatorId),
|
|
("ad_format", adType.label),
|
|
("duration", durationInMillis),
|
|
("ad_unit_name", shrink(adUnitId)),
|
|
("error_code", errorCode)
|
|
)
|
|
}
|
|
|
|
/**
|
|
* 精简adUnitId以便于不过多占用打点数据包
|
|
*/
|
|
private static func shrink(_ adUnitId: String) -> String {
|
|
// shrink a adUnitId from AdMob to a shorter form,
|
|
// for example, ca-app-pub-2436733915645843/9966308712
|
|
// shrink("ca-app-pub-2436733915645843/9966308712") == "9966308712"
|
|
if adUnitId.hasPrefix("ca-app-pub-") && adUnitId.count == 38 && adUnitId[adUnitId.index(adUnitId.startIndex, offsetBy: 27)] == "/" {
|
|
return String(adUnitId.suffix(10))
|
|
}
|
|
|
|
// no need to shrink
|
|
return adUnitId
|
|
}
|
|
}
|