xugenyuan

ref |> 完善埋点基础封装

Signed-off-by: xugenyuan <xugenyuan@wondertek.com.cn>
1 import { SpConstants } from 'wdConstant/Index'; 1 import { SpConstants } from 'wdConstant/Index';
2 -import { AccountManagerUtils, AppUtils, DeviceUtil, SPHelper, StringUtils } from 'wdKit/Index'; 2 +import { AccountManagerUtils, AppUtils, DeviceUtil, MpaasUtils, SPHelper, StringUtils } from 'wdKit/Index';
3 import { HostEnum, HostManager } from 'wdNetwork/Index'; 3 import { HostEnum, HostManager } from 'wdNetwork/Index';
4 4
5 export type ParamType = Record<string, string | number | boolean | Array<string>> 5 export type ParamType = Record<string, string | number | boolean | Array<string>>
@@ -7,7 +7,9 @@ export type ParamType = Record<string, string | number | boolean | Array<string> @@ -7,7 +7,9 @@ export type ParamType = Record<string, string | number | boolean | Array<string>
7 export class PublicParams { 7 export class PublicParams {
8 8
9 getPublicParams() : Promise<ParamType> { 9 getPublicParams() : Promise<ParamType> {
10 - return new Promise((resolve) => { 10 + return new Promise(async (resolve) => {
  11 +
  12 + let utdid = await MpaasUtils.mpaasUtdid();
11 13
12 let pub: ParamType = { 14 let pub: ParamType = {
13 "userName": SPHelper.default.getSync(SpConstants.USER_NAME, '') as string, 15 "userName": SPHelper.default.getSync(SpConstants.USER_NAME, '') as string,
@@ -24,7 +26,7 @@ export class PublicParams { @@ -24,7 +26,7 @@ export class PublicParams {
24 "version": AppUtils.getAppVersionName(), 26 "version": AppUtils.getAppVersionName(),
25 "deviceId": DeviceUtil.clientId(), 27 "deviceId": DeviceUtil.clientId(),
26 "model": DeviceUtil.getMarketName() || "未知", 28 "model": DeviceUtil.getMarketName() || "未知",
27 - "mpaasId": PublicParams.getMpaasId(), 29 + "mpaasId": utdid,
28 } 30 }
29 resolve(pub) 31 resolve(pub)
30 }) 32 })
@@ -52,9 +54,4 @@ export class PublicParams { @@ -52,9 +54,4 @@ export class PublicParams {
52 } 54 }
53 return "" 55 return ""
54 } 56 }
55 -  
56 - private static getMpaasId() {  
57 - // TODO: 新增mpaasId utdid  
58 - return 'alsjdflajxaljdlfjaldjfa';  
59 - }  
60 } 57 }
1 -import { ContentDetailDTO } from 'wdBean/Index'; 1 +import { CompInfoBean, ContentDetailDTO, ContentDTO, PageInfoDTO } from 'wdBean/Index';
2 import { ParamType } from './PublicParams'; 2 import { ParamType } from './PublicParams';
  3 +import { TrackingUtils } from './TrackingUtils';
3 4
4 export class TrackParamConvert { 5 export class TrackParamConvert {
5 6
6 - static convertContentDetail(detail: ContentDetailDTO) : ParamType { 7 + /// 页面pageInfo 转换 埋点参数
  8 + static page(pageInfo?: PageInfoDTO) : ParamType {
  9 + return TrackParamConvert.pageCompProgram(pageInfo, undefined, undefined)
  10 + }
  11 +
  12 + // 页面comp 转换 埋点参数
  13 + static pageAndComp(pageInfo?: PageInfoDTO, comp?: CompInfoBean) : ParamType {
  14 + return TrackParamConvert.pageCompProgram(pageInfo, comp, undefined)
  15 + }
  16 +
  17 + // 运营位数据 转换 埋点参数
  18 + static program(program?: ContentDTO) : ParamType {
  19 + return TrackParamConvert.pageCompProgram(undefined, undefined, program)
  20 + }
  21 +
  22 + static pageCompProgram(pageInfo?: PageInfoDTO, comp?: CompInfoBean, program?: ContentDTO) : ParamType {
  23 + let params = TrackingUtils.generateParams()
  24 +
  25 + //TODO: 转换参数
  26 +
  27 + return params
  28 + }
  29 +
  30 + static contentDetail(detail: ContentDetailDTO) : ParamType {
7 31
8 let param: ParamType = { 32 let param: ParamType = {
9 "contentType": detail.newsType.toFixed(0), 33 "contentType": detail.newsType.toFixed(0),
10 "contentId": detail.newsId, 34 "contentId": detail.newsId,
11 - "contentName": detail.newsTitle 35 + "contentName": detail.newsTitle,
  36 + "channelSourceId": detail.reLInfo?.channelId || "",
  37 + "rmhPlatform": detail.rmhPlatform,
  38 + }
  39 +
  40 + if (detail.rmhInfo && detail.rmhInfo.rmhId.length > 0) {
  41 + param["saAuthorId"] = detail.rmhInfo.rmhId
  42 + param["saAuthorName"] = detail.rmhInfo.rmhName || ""
  43 + }
  44 +
  45 + // 图文和音频
  46 + if (detail.newsType == 8 || detail.newsType == 13) {
  47 + param["readFlag"] = detail.readFlag ? detail.readFlag : 0
  48 + }
12 49
13 - //TODO: 添加更多参数 50 + // TODO: 频道处理
  51 + // if (detail.channelsList) {
  52 + // param["contentShowChannelId"] = X
  53 + // }
14 54
  55 + // TODO: 专题处理
  56 + // detail.topicInfo.
  57 + // param["summaryType"] = X
  58 + // param["summaryId"] = X
15 59
  60 + // TODO: 直播
  61 + if (detail.newsType == 2 && detail.liveInfo) {
  62 + // param["vliveId"] = X
  63 + // param["vliveName"] = X
  64 + // param["liveMode"] = X
  65 + // param["liveStreamType"] = X
  66 + // param["liveType"] = X
16 } 67 }
17 68
18 TrackParamConvert.appendRecommend(detail, param) 69 TrackParamConvert.appendRecommend(detail, param)
@@ -21,6 +72,8 @@ export class TrackParamConvert { @@ -21,6 +72,8 @@ export class TrackParamConvert {
21 72
22 private static appendRecommend(detail: ContentDetailDTO, to: ParamType) { 73 private static appendRecommend(detail: ContentDetailDTO, to: ParamType) {
23 74
  75 + //TODO: ContentDetailDTO 增加推荐字段
  76 +
24 to["sceneId"] = "9999" 77 to["sceneId"] = "9999"
25 to["subSceneId"] = "" 78 to["subSceneId"] = ""
26 to["cnsTraceId"] = "selfHold" 79 to["cnsTraceId"] = "selfHold"
  1 +import { ParamType } from './PublicParams';
  2 +
  3 +export class TrackingUtils {
  4 +
  5 + static generateParams(extParams?: ParamType) : ParamType {
  6 + let retParam: ParamType = {}
  7 + if (extParams) {
  8 + for (const obj of Object.entries(extParams)) {
  9 + // Logger.info(TAG, ` ${obj[0]} => ` + `${obj[1]}`);
  10 + retParam[obj[0]] = obj[1]
  11 + }
  12 + }
  13 + return retParam
  14 + }
  15 +
  16 + // wdTracking内部调用
  17 + static fillPositionWith(attr: ParamType) {
  18 + let pageName = attr["pageName"] as string
  19 + let action = attr["action"] as string
  20 + if (action.length > 0) {
  21 + attr["saPosition"] = pageName + "_" + action
  22 + return
  23 + }
  24 + attr["saPosition"] = pageName
  25 + }
  26 +
  27 +}
@@ -9,6 +9,7 @@ const TAG = "WDTracking" @@ -9,6 +9,7 @@ const TAG = "WDTracking"
9 9
10 export class Tracking { 10 export class Tracking {
11 11
  12 + // 通用埋点事件
12 static event(eventId: string, params?: ParamType) { 13 static event(eventId: string, params?: ParamType) {
13 14
14 if (!TrackingModule.getHasInit()) { 15 if (!TrackingModule.getHasInit()) {
1 import { ParamType } from '../common/PublicParams'; 1 import { ParamType } from '../common/PublicParams';
  2 +import { TrackingUtils } from '../common/TrackingUtils';
2 import { Tracking } from './Tracking'; 3 import { Tracking } from './Tracking';
3 4
4 export class TrackingButton { 5 export class TrackingButton {
5 6
  7 + // 普通按钮点击事件,例如登录按钮点击
  8 + // 对于其他按钮 各个业务 可以直接调用 Tracking.event,如果参数较多的话,可以在这里添加。
6 static click(buttonName: string, pageId: string, pageName: string, extParams?: ParamType) { 9 static click(buttonName: string, pageId: string, pageName: string, extParams?: ParamType) {
7 10
8 - if (!extParams) {  
9 - extParams = {}  
10 - }  
11 - extParams["pageName"] = pageName  
12 - extParams["pageId"] = pageId  
13 - extParams["clickButtonName"] = buttonName 11 + let params = TrackingUtils.generateParams(extParams)
  12 + params["pageName"] = pageName
  13 + params["pageId"] = pageId
  14 + params["clickButtonName"] = buttonName
14 15
15 - Tracking.event("name_click", extParams) 16 + Tracking.event("name_click", params)
16 } 17 }
  18 +
  19 +
  20 +
17 } 21 }
1 import { ParamType } from '../common/PublicParams' 1 import { ParamType } from '../common/PublicParams'
2 import { TrackConstants } from "../common/TrackConstants" 2 import { TrackConstants } from "../common/TrackConstants"
  3 +import { TrackingUtils } from '../common/TrackingUtils'
  4 +import { Tracking } from './Tracking'
  5 +import { PointerMatrix } from '@kit.TestKit'
3 6
4 export class TrackingContent { 7 export class TrackingContent {
5 8
@@ -8,38 +11,96 @@ export class TrackingContent { @@ -8,38 +11,96 @@ export class TrackingContent {
8 /// pageName 如果是统一业务值 见 TrackConstants 11 /// pageName 如果是统一业务值 见 TrackConstants
9 /// pageId 12 /// pageId
10 /// extParams 模型转换成字段参数传值 转换方法见 TrackParamConvert 13 /// extParams 模型转换成字段参数传值 转换方法见 TrackParamConvert
11 - static common(eventType: TrackConstants.EventType, pageId: string, pageName: string, extParams: ParamType) {  
12 - 14 + static common(eventType: TrackConstants.EventType, pageId: string, pageName: string, extParams?: ParamType) {
  15 + let params = TrackingUtils.generateParams(extParams)
  16 + if (pageId.length) {
  17 + params["pageId"] = pageId
  18 + }
  19 + if (pageName.length) {
  20 + params["pageName"] = pageName
  21 + }
  22 + if (eventType === TrackConstants.EventType.Click) {
  23 +
  24 + params["action"] = TrackConstants.ActionType.DetailPageShow
  25 + TrackingUtils.fillPositionWith(params)
  26 + Tracking.event("content_click", params)
  27 + } else if (eventType === TrackConstants.EventType.Show) {
  28 +
  29 + params["action"] = TrackConstants.ActionType.Show
  30 + TrackingUtils.fillPositionWith(params)
  31 + Tracking.event("content_exposure", params)
  32 + }
13 } 33 }
14 34
15 - static commonShare(shareType: number, pageId: string, pageName: string, extParams: ParamType) { 35 + static commonShare(shareType: number, pageId: string, pageName: string, extParams?: ParamType) {
16 36
17 } 37 }
18 38
19 - static like(like: boolean, pageId: string, pageName: string, extParams: ParamType) {  
20 - 39 + static like(like: boolean, pageId: string, pageName: string, extParams?: ParamType) {
  40 + let params = TrackingUtils.generateParams(extParams)
  41 + if (pageId.length) {
  42 + params["pageId"] = pageId
  43 + }
  44 + if (pageName.length) {
  45 + params["pageName"] = pageName
  46 + }
  47 + params["action"] = like ? TrackConstants.ActionType.Like : TrackConstants.ActionType.DisLike
  48 + TrackingUtils.fillPositionWith(params)
  49 + Tracking.event(like ? "content_like_click" : "content_cancel_like_click", params)
21 } 50 }
22 51
23 - static collect(collect: boolean, pageId: string, pageName: string, extParams: ParamType) {  
24 - 52 + static collect(collect: boolean, pageId: string, pageName: string, extParams?: ParamType) {
  53 + let params = TrackingUtils.generateParams(extParams)
  54 + if (pageId.length) {
  55 + params["pageId"] = pageId
  56 + }
  57 + if (pageName.length) {
  58 + params["pageName"] = pageName
  59 + }
  60 + params["action"] = collect ? TrackConstants.ActionType.Collect : TrackConstants.ActionType.UnCollect
  61 + TrackingUtils.fillPositionWith(params)
  62 + Tracking.event(collect ? "content_collection_click" : "content_cancel_collection_click", params)
25 } 63 }
26 64
27 - static follow(follow: boolean, followUserId: string, followUserName: string, pageId: string, pageName: string, extParams: ParamType) {  
28 - 65 + static follow(follow: boolean, followUserId: string, followUserName: string, pageId: string, pageName: string, extParams?: ParamType) {
  66 + let params = TrackingUtils.generateParams(extParams)
  67 + if (pageId.length) {
  68 + params["pageId"] = pageId
  69 + }
  70 + if (pageName.length) {
  71 + params["pageName"] = pageName
  72 + }
  73 + params[follow ? "followPDUserId" : "cancelFollowPDUseId"] = followUserId
  74 + params[follow ? "followUserName" : "cancelFollowUserName"] = followUserName
  75 +
  76 + params["action"] = follow ? TrackConstants.ActionType.Follow : TrackConstants.ActionType.UnFollow
  77 + TrackingUtils.fillPositionWith(params)
  78 +
  79 + /// 关注号主的,特殊处理下itemId字段,不用所在内容的推荐字段
  80 + params["itemId"] = followUserId + "_author"
  81 + Tracking.event(follow ? "follow_click" : "cancel_follow_click", params)
29 } 82 }
30 83
31 - static commentClick(pageId: string, pageName: string, extParams: ParamType) {  
32 - 84 + static commentClick(pageId: string, pageName: string, extParams?: ParamType) {
  85 + let params = TrackingUtils.generateParams(extParams)
  86 + if (pageId.length) {
  87 + params["pageId"] = pageId
  88 + }
  89 + if (pageName.length) {
  90 + params["pageName"] = pageName
  91 + }
  92 + params["action"] =TrackConstants.ActionType.Comment
  93 + TrackingUtils.fillPositionWith(params)
  94 + Tracking.event("content_comment_click", params)
33 } 95 }
34 96
35 - static commentShare(pageId: string, pageName: string, extParams: ParamType) { 97 + static commentShare(pageId: string, pageName: string, extParams?: ParamType) {
36 98
37 } 99 }
38 100
39 // 内容下载点击事件 1.图片 2.音频 3.视频 101 // 内容下载点击事件 1.图片 2.音频 3.视频
40 - static download(donwloadContentType: number, pageId: string, pageName: string, extParams: ParamType) { 102 + static download(donwloadContentType: number, pageId: string, pageName: string, extParams?: ParamType) {
41 103
42 } 104 }
43 105
44 -  
45 } 106 }
  1 +import { ParamType } from '../common/PublicParams'
  2 +import { TrackConstants } from '../common/TrackConstants'
  3 +import { TrackingUtils } from '../common/TrackingUtils'
  4 +import { Tracking } from './Tracking'
  5 +
1 export class TrackingPageBrowse { 6 export class TrackingPageBrowse {
2 7
  8 + /// 页面浏览事件
  9 + // duration 浏览时长,上层计算,单位毫秒
  10 + static trackCommonPageExposureEnd(pageId: string, pageName: string, duration: number ,extParams?: ParamType) {
  11 + let params = TrackingUtils.generateParams(extParams)
  12 + if (pageId.length) {
  13 + params["pageId"] = pageId
  14 + }
  15 + if (pageName.length) {
  16 + params["pageName"] = pageName
  17 + }
  18 +
  19 + params["duration"] = duration / 1000
  20 + params["action"] = TrackConstants.ActionType.Browse
  21 + TrackingUtils.fillPositionWith(params)
  22 + Tracking.event("channel_exposure", params)
  23 + }
  24 +
3 25
4 } 26 }
1 import { ParamType } from '../common/PublicParams'; 1 import { ParamType } from '../common/PublicParams';
  2 +import { TrackConstants } from '../common/TrackConstants';
  3 +import { TrackingUtils } from '../common/TrackingUtils';
  4 +import { Tracking } from './Tracking';
  5 +import { TrackingContent } from './TrackingContent';
2 6
3 /* 7 /*
4 * 处理音频、视频、直播 正片播放、播放结束、播放错误等 8 * 处理音频、视频、直播 正片播放、播放结束、播放错误等
5 * */ 9 * */
6 export class TrackingPlay { 10 export class TrackingPlay {
7 11
8 - // 视频 12 + // 视频 正片播放
9 static videoPositivePlay(prepareTime: number, pageId: string, pageName: string, extParams: ParamType) { 13 static videoPositivePlay(prepareTime: number, pageId: string, pageName: string, extParams: ParamType) {
10 - 14 + let params = TrackingUtils.generateParams(extParams)
  15 + if (pageId.length) {
  16 + params["pageId"] = pageId
  17 + }
  18 + if (pageName.length) {
  19 + params["pageName"] = pageName
  20 + }
  21 + params["timeConsuming"] = prepareTime
  22 +
  23 + params["action"] = TrackConstants.ActionType.DetailPageShow
  24 + TrackingUtils.fillPositionWith(params)
  25 + Tracking.event("video_positive_pybk", params)
11 } 26 }
12 27
  28 + // 视频 播放结束
13 static videoPlayEnd(currentPlayTime: number, totalTime: number, browseTime: number, pageId: string, pageName: string, extParams: ParamType) { 29 static videoPlayEnd(currentPlayTime: number, totalTime: number, browseTime: number, pageId: string, pageName: string, extParams: ParamType) {
14 - 30 + let params = TrackingUtils.generateParams(extParams)
  31 + if (pageId.length) {
  32 + params["pageId"] = pageId
  33 + }
  34 + if (pageName.length) {
  35 + params["pageName"] = pageName
  36 + }
  37 +
  38 + let playCompleted = currentPlayTime >= totalTime;
  39 + params["pybkDuration"] = currentPlayTime
  40 + params["completRate"] = playCompleted ? 1 : 0
  41 + params["duration"] = browseTime
  42 +
  43 + params["action"] = TrackConstants.ActionType.Browse
  44 + TrackingUtils.fillPositionWith(params)
  45 + Tracking.event("video_end_pybk", params)
15 } 46 }
16 47
  48 + // 视频 播放错误
17 static videoPlayError(errorInfo: string, pageId: string, pageName: string, extParams: ParamType) { 49 static videoPlayError(errorInfo: string, pageId: string, pageName: string, extParams: ParamType) {
18 - 50 + let params = TrackingUtils.generateParams(extParams)
  51 + if (pageId.length) {
  52 + params["pageId"] = pageId
  53 + }
  54 + if (pageName.length) {
  55 + params["pageName"] = pageName
  56 + }
  57 +
  58 + params["errorInformation"] = errorInfo
  59 + params["action"] = TrackConstants.ActionType.DetailPageShow
  60 + TrackingUtils.fillPositionWith(params)
  61 + Tracking.event("pybk_error", params)
19 } 62 }
20 63
21 64
22 - // 音频 65 + // 音频 正片播放
23 static audioPositivePlay(prepareTime: number, pageId: string, pageName: string, extParams: ParamType) { 66 static audioPositivePlay(prepareTime: number, pageId: string, pageName: string, extParams: ParamType) {
24 - 67 + let params = TrackingUtils.generateParams(extParams)
  68 + if (pageId.length) {
  69 + params["pageId"] = pageId
  70 + }
  71 + if (pageName.length) {
  72 + params["pageName"] = pageName
  73 + }
  74 + params["timeConsuming"] = prepareTime
  75 +
  76 + params["action"] = TrackConstants.ActionType.DetailPageShow
  77 + TrackingUtils.fillPositionWith(params)
  78 + Tracking.event("audio_positive_pybk", params)
25 } 79 }
26 80
  81 + // 音频 播放结束
27 static audioPlayEnd(currentPlayTime: number, totalTime: number, browseTime: number, pageId: string, pageName: string, extParams: ParamType) { 82 static audioPlayEnd(currentPlayTime: number, totalTime: number, browseTime: number, pageId: string, pageName: string, extParams: ParamType) {
28 - 83 + let params = TrackingUtils.generateParams(extParams)
  84 + if (pageId.length) {
  85 + params["pageId"] = pageId
  86 + }
  87 + if (pageName.length) {
  88 + params["pageName"] = pageName
  89 + }
  90 +
  91 + let playCompleted = currentPlayTime >= totalTime;
  92 + params["pybkDuration"] = currentPlayTime
  93 + params["completRate"] = playCompleted ? 1 : 0
  94 + params["duration"] = browseTime
  95 +
  96 + params["action"] = TrackConstants.ActionType.Browse
  97 + TrackingUtils.fillPositionWith(params)
  98 + Tracking.event("audio_end_pybk", params)
29 } 99 }
30 100
  101 + // 音频 播放错误
31 static audioPlayError(errorInfo: string, pageId: string, pageName: string, extParams: ParamType) { 102 static audioPlayError(errorInfo: string, pageId: string, pageName: string, extParams: ParamType) {
32 - 103 + let params = TrackingUtils.generateParams(extParams)
  104 + if (pageId.length) {
  105 + params["pageId"] = pageId
  106 + }
  107 + if (pageName.length) {
  108 + params["pageName"] = pageName
  109 + }
  110 +
  111 + params["errorInformation"] = errorInfo
  112 + params["action"] = TrackConstants.ActionType.DetailPageShow
  113 + TrackingUtils.fillPositionWith(params)
  114 + Tracking.event("audio_error", params)
33 } 115 }
34 116
35 117
36 - // 直播 118 + // 直播 正片播放
37 static liveVideoPositivePlay(prepareTime: number, pageId: string, pageName: string, extParams: ParamType) { 119 static liveVideoPositivePlay(prepareTime: number, pageId: string, pageName: string, extParams: ParamType) {
38 - 120 + let params = TrackingUtils.generateParams(extParams)
  121 + if (pageId.length) {
  122 + params["pageId"] = pageId
  123 + }
  124 + if (pageName.length) {
  125 + params["pageName"] = pageName
  126 + }
  127 + params["timeConsuming"] = prepareTime
  128 +
  129 + params["action"] = TrackConstants.ActionType.DetailPageShow
  130 + TrackingUtils.fillPositionWith(params)
  131 + Tracking.event("live_positive_pybk", params)
39 } 132 }
40 133
  134 + // 直播 播放结束
41 static liveVideoPlayEnd(currentPlayTime: number, totalTime: number, browseTime: number, pageId: string, pageName: string, extParams: ParamType) { 135 static liveVideoPlayEnd(currentPlayTime: number, totalTime: number, browseTime: number, pageId: string, pageName: string, extParams: ParamType) {
42 - 136 + let params = TrackingUtils.generateParams(extParams)
  137 + if (pageId.length) {
  138 + params["pageId"] = pageId
  139 + }
  140 + if (pageName.length) {
  141 + params["pageName"] = pageName
  142 + }
  143 +
  144 + let playCompleted = currentPlayTime >= totalTime;
  145 + params["pybkDuration"] = currentPlayTime
  146 + params["completRate"] = playCompleted ? 1 : 0
  147 + params["duration"] = browseTime
  148 +
  149 + params["action"] = TrackConstants.ActionType.Browse
  150 + TrackingUtils.fillPositionWith(params)
  151 + Tracking.event("quit_live_click", params)
43 } 152 }
44 -  
45 } 153 }