Tealium 分析

本文详细介绍了Tealium在Android上的实现,包括初始化流程、数据发送流程、Tealium的设计细节如Core、Visitor、Crash reporter等模块,以及网络设计、本地数据存储和发送策略。此外,还探讨了可借鉴的设计模式和代码实践,如重试机制、线程池和协程结合的使用。
摘要由CSDN通过智能技术生成


Tealium 分析, 以 1.2.4 版本为例

源码在: https://github.com/Tealium/tealium-kotlin

官方文档:https://docs.tealium.com/platforms/getting-started/

1.0 调用流程

1.1 初始化流程

在这里插入图片描述

说明:
① 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 所需要的资源。

1.2 发送数据流程

以 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 val payload = shallowCopy(dispatch.payload())

    override fun payload(): Map<String, Any> {
   
        return payload.toMap()
    }

    override fun addAll(data: Map<String, Any>) {
   
        payload.putAll(data)
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值