nw-log-distribution

分流打点组件,根据配置实现将某个点打到哈勃或部门点接收平台

Usage no npm install needed!

<script type="module">
  import nwLogDistribution from 'https://cdn.skypack.dev/nw-log-distribution';
</script>

README

nw-log-distribution

分流打点组件

重要更新日志

  • 2020-08-05 本SDK改为全部向分流服务器打点,不再发送到哈勃,所以不再需要传入pointConfig
  • 2021-08-18 2.14.0版本SDK实例化增加一个参数activityId,用于上报打点的attributes中附加指定activityId,使得活动数据报表系统能够实现自动收集。
  • 2021-10-21 2.16.0版本SDK支持在打点时指定activityId,用于在某些特定的打点上附加指定activityId,用法见下面示例。
  • 2021-12-17 2.18.0版本SDK支持在打点时指定useAppTrack,用于在某些特定的打点选择是否通过app上传,用法见下面示例。

安装

$ yarn add nw-log-distribution

example

import {ENV,APP_KEY,HubLog as Log} from 'nw-log-distribution';

const log = new Log({
    userId: '',
    key: APP_KEY.PC | APP_KEY.H5, // 根据当前开发的环境选择正确的appkey
    hbInitOptions: {
        truncateLength: 255,
        persistence: "localStorage",
        cross_subdomain_cookie: true
    },
    k: 'lofter',
    v: '6.15.0',
    env: ENV.ST,
    activityId: 'ac-log' // 非必须,SDK 初始化传入activityId,后续所有打点都会带上activityId
})

log.capture('h5-4-1', {
  category: 'h5-4',
  activityId: 'ac-log', // 非必须,只有本次打点带上activityId,同时会覆盖SDK初始化的activityId
  useAppTrack: false    // 非必须,针对某一打点设置是否通过app上传,不传默认为false,既默认使用js ajax上传,true则通过app上传
})

let deviceId = '';
async function getDeviceId() {
    deviceId = await log.getDeviceId();
}
getDeviceId();

ST平台验证注意事项

userAgent

本地开发验证打点时,k需要指定为lofter,同时浏览器的userAgent需要设置为符合对应的平台特征,否则在对应的平台会找不到打点记录,因为st平台会根据打点请求的userAgent来判断打点请求所对应的平台是WebH5还是其他。

版本号

本地开发验证打点时,v注意指定准确的统计点所属版本号,如果不指定统计点版本号,st平台会认为统计点属于最新版的版本号,可能会出现版本不匹配的问题。

API

Classes

HubLog

HubLog

Functions

createMaxWaitingCallback(options)

createMaxWaitingCallback

Typedefs

LogOptions : Object

打点的额外参数

HubLog

HubLog

Kind: global class

new HubLog(props)

Param Type Description
props Object
props.userId string 用户id
props.key string hubble产品id
props.hbInitOptions object hubble init方法需要的options参数,如不传默认值{truncateLength: 255,persistence: "localStorage",cross_subdomain_cookie: true}
props.k string ST平台产品key
props.env string 数据上报平台
props.activityId string 活动ID

hubLog.logToHub(eventId, attributes, [options])

打点到哈勃 eg: logToHub('event-Idxxxx', { attr1: 'attr1value', attr2: "Laptop Computer", attr3: "MacBook Pro", attr4: 123.45 });

Kind: instance method of HubLog

Param Type Description
eventId string
attributes object
[options] LogOptions 打点额外参数

hubLog.logToSt(eventId, attributes, [options])

打点到打点验证平台 eg:logToSt('event-Idxxxx', { attr1: 'attr1 value', attr2: "Laptop Computer", attr3: "MacBook Pro", attr4: 123.45 });

Kind: instance method of HubLog

Param Type Description
eventId string 打点事件id
attributes object 上报属性对象
[options] LogOptions 打点额外参数

hubLog.login(userId)

标识真实用户,建议用户登录完成时调用

Kind: instance method of HubLog

Param
userId

hubLog.capture(eventId, attributes, [options])

根据对象初始化配置env属性打点到不同平台 eg:capture('event-Idxxxx', { attr1: 'attr1 value', attr2: "Laptop Computer", attr3: "MacBook Pro", attr4: 123.45 });

Kind: instance method of HubLog

Param Type Description
eventId string 打点事件id
attributes object 上报属性对象
[options] LogOptions 打点额外参数

hubLog.getDeviceId() ⇒

通过哈勃sdk获取设备ID,如果在端外或者获取失败,会生成随机的uuid返回

Kind: instance method of HubLog
Returns: Promise

ENV : enum

Enum env values.

Kind: global enum
Read only: true
Properties

Name Type Default
ST string "ST"
HB string "HB"

APP_KEY : enum

Enum env values.

Kind: global enum
Read only: true
Properties

Name Type Default
PC string "MA-BFD7-963BF6846668"
H5 string "MA-B4E8-3BEB9540671E"

createMaxWaitingCallback(options)

createMaxWaitingCallback

Kind: global function

Param Type
options LogOptions

LogOptions : Object

打点的额外参数

Kind: global typedef
Properties

Name Type Default Description
[LogOptions.callback] function 上报完成后的回调
[LogOptions.maxWaitingTime] number 200 回调最多等待时间,单位毫秒,默认200ms
[LogOptions.reqHb] boolean 使用设置是否调用 哈勃