源码在: https://github.com/Tealium/tealium-kotlin
官方文档:https://docs.tealium.com/platforms/getting-started/
说明:
① new 一个 TealiumConfig, 进行 Tealium 的一些配置
class TealiumConfig @JvmOverloads constructor(val application: Application,
val accountName: String,
val profileName: String,
val environment: Environment,
var dataSourceId: String? = null,
val collectors: MutableSet<CollectorFactory> = Collectors.core,
val dispatchers: MutableSet<DispatcherFactory> = mutableSetOf(),
val modules: MutableSet<ModuleFactory> = mutableSetOf()) {
/**
* A set of validators where any custom [DispatchValidator]s can be added. These will be merged
* in with the built in validators when initializing the library.
*/
val validators: MutableSet<DispatchValidator> = mutableSetOf()
// 文件的存储路径
private val pathName = "${
application.filesDir}${
File.separatorChar}tealium${
File.separatorChar}${
accountName}${
File.separatorChar}${
profileName}${
File.separatorChar}${
environment.environment}"
val tealiumDirectory: File = File(pathName)
/**
* 用于存储一些额外的信息
* Map of key-value pairs supporting override options that do not have a direct property on the
* [TealiumConfig] object.
*/
val options = mutableMapOf<String, Any>()
/**
* Gets and sets the initial LibrarySettings for the library. Useful defaults have already been
* set on the [LibrarySettings] default constructor, but the default settings used by the
* library can be set here.
*/
var overrideDefaultLibrarySettings: LibrarySettings? = null
/** 是否使用服务端的配置
* Sets whether or not to fetch publish settings from a remote host.
*/
var useRemoteLibrarySettings: Boolean = false
/**
*
* Sets the remote URL to use when requesting updated remote publish settings.
*/
var overrideLibrarySettingsUrl: String? = null
/**
* Set to false to disable deep link tracking.
*/
var deepLinkTrackingEnabled: Boolean = true
/**
* Set to false to disable the QR code trace feature.
*/
var qrTraceEnabled: Boolean = true
/**
* A list of EventTriggers for automatically starting and stopping TimedEvents.
*/
var timedEventTriggers: MutableList<EventTrigger> = mutableListOf()
init {
tealiumDirectory.mkdirs()
}
}
② 将一个 TealiumConfig 配置传入,创建一个 Tealium,可以有多个 Tealium, 内部使用 map 存储起来,同时还可以传入 onReady, 在 Tealium 初始化可以的时候进行回调
// Tealium.kt
fun create(name: String, config: TealiumConfig, onReady: (Tealium.() -> Unit)? = null): Tealium {
val instance = Tealium(name, config, onReady)
instances[name] = instance
return instance
}
③ Tealium#init 初始化 Tealium,初始化 Tealium 所需要的资源。
以 track TealiumEvent 主要流程分析
① GenericDispatch 也是继承 Dispatch,它的 payload 是从传入的 dispatch 进行复制
internal class GenericDispatch(dispatch: Dispatch) : Dispatch {
override val id: String = dispatch.id
override var timestamp: Long? = dispatch.timestamp ?: System.currentTimeMillis()
private