growingio / growingio-sdk-harmonyos

GrowingAnalytics for HarmonyOS.
Apache License 2.0
0 stars 0 forks source link

feat: sub-tracker support #3

Closed YoloMao closed 5 months ago

YoloMao commented 7 months ago

多实例采集

初始化子实例

let config = new GrowingConfig(
  'SubTracker AccountId',
  'SubTracker DataSourceId',
  'SubTracker UrlScheme',
  'SubTracker DataCollectionServerHost<Optional>'
)
GrowingAnalytics.startSubTracker(trackerId, config)

初始化配置中,accountId/dataSourceId/dataCollectionServerHost 都可与主实例不同,具体如下表格:

配置项 子实例是否能单独配置
accountId
dataSourceId
urlScheme
dataCollectionServerHost
debugEnabled 否,以主实例为准
sessionInterval
dataUploadInterval
dataCollectionEnabled
idMappingEnabled

注意:初始化子实例前必须先初始化主实例

兼容 APIs

子实例可单独调用以下接口,其逻辑与其他实例相互隔离

export interface GrowingAnalyticsInterface {
   setDataCollectionEnabled(enabled: boolean)
   setLoginUserId(userId: string, userKey: string)
   cleanLoginUserId()

   setLoginUserAttributes(attributes: AttributesType)
   track(eventName: string, attributes: AttributesType, sendTo?: string[])
   trackTimerStart(eventName: string): Promise<string>
   trackTimerPause(timerId: string)
   trackTimerResume(timerId: string)
   trackTimerEnd(timerId: string, attributes: AttributesType)
   removeTimer(timerId: string)
   clearTrackTimer()
}

假设子实例的 trackerIdsubTrackerId_01,调用方式如下:

// 获取子实例
let subTracker = GrowingAnalytics.tracker('subTrackerId_01')
if (!subTracker) {
  return
}

// 数据采集开关
subTracker.setDataCollectionEnabled(true)

// 登录用户ID
subTracker.setLoginUserId('user')
subTracker.setLoginUserId('user', 'harmony')
subTracker.cleanLoginUserId()

// 设置埋点事件
subTracker.track('buyProduct1')
subTracker.track('buyProduct2', {
  'name': 'apple',
  'money': 1000,
  'num': 100,
  'from': ['sichuan', 'guizhou', 'hunan']
})

// 事件计时器
let timerId = await subTracker.trackTimerStart('eventName')
subTracker.trackTimerPause(timerId)
subTracker.trackTimerResume(timerId)
subTracker.trackTimerEnd(timerId)
subTracker.trackTimerEnd(timerId, {
  'property': 'value',
  'property2': 100
})
subTracker.removeTimer(timerId)
subTracker.clearTrackTimer()

// 设置登录用户属性
subTracker.setLoginUserAttributes({
  'name': 'ben',
  'age': 30
})

SendTo

可使用 sendTo 功能将主实例或子实例的自定义事件转发到其他子实例:

// 主实例track转发
GrowingAnalytics.track('buyProduct1', {}, ['subTrackerId_01', 'subTrackerId_02'])
GrowingAnalytics.track('buyProduct2', {
  'name': 'apple',
  'money': 1000,
  'num': 100,
  'from': ['sichuan', 'guizhou', 'hunan']
}, ['subTrackerId_01', 'subTrackerId_02'])

// 子实例track转发
let subTracker = GrowingAnalytics.tracker('subTrackerId_01')
if (!subTracker) {
  return
}
subTracker.track('buyProduct1', {}, ['subTrackerId_02'])
subTracker.track('buyProduct2', {
  'name': 'apple',
  'money': 1000,
  'num': 100,
  'from': ['sichuan', 'guizhou', 'hunan']
}, ['subTrackerId_02'])

当前仅 track 接口支持 sendTo 转发