README
package-mp-track-demo
A Vue.js project
Build Setup
# install dependencies
npm install
# serve with hot reload at localhost:8080
npm run dev
# build for production with minification
npm run build
# build for production and view the bundle analyzer report
npm run build --report
For detailed explanation on how things work, checkout the guide and docs for vue-loader.
sd-mp-track-demo
SD 小程序 统计封装
安装使用
npm i sd-mp-track-service --save
集成
初始化
在App.vue中?
import trackPlugin from 'sd-mp-track-service'
let trackConfig = {
showLog: false, // 是否打印统计成功 log
/**
用于从Storage中获取account Object的对应key
account Object 应该包含 account = {cryptoUserId, sdToken}
*/
accountKey: 'account',
commonParams: () => {
return {
biz: 'DEMO_TRACK', // 根据业务自行修改 后面可能会删掉
appType: -1, // 根据业务自行修改, 后面可能会删掉
appId: 'sd123456' // 根据业务自行修改
}
},
commonControlArg: {
host: 'shuidichou.com', // 默认为水滴筹
onHandleTrack: (trackData, controlArg) => { // 用于打印发出的统计log 可注释掉
const {
showInfo = true
} = controlArg
if (showInfo) console.info(trackData)
}
}
}
Vue.use(trackerPlugin, trackConfig)
位置统计(非必须)
App.vue
this.$tracker.loadLocation()
集成分享
somePage.vue中
onShareAppMessage (options) {
// return this.$tracker.onShareAppMessage({}, options)
// 分享的配置 eg: {path: '', success :() => console.warn('shareSuccess'), fail: () => {}}
// path默认为当前页面
let shareConfig = {}
// 分享url会拼上对象里面的参数
let shareParams = {}
// 分享统计打点附带的统计参数对象 eg: {amount: 30, title: 'haha', extInfo: {id: 29, msg: 'extMsg'}}
let trackParams = {}
let data = this.$tracker.onShareAppMessage(
shareConfig,
options,
{
shareParams,
trackParams
}
)
return data
}
其他业务统计 调用
this.$tracker.track({
op: 'login',
mobile: 'xxxxx'
})
注意
v0.6.5开始
- commonParams 里面不要放selfTag 库本身已经接管selfTag 若想获取可以调用
tracker.getSelfTag()
- commonParams 里面不要放authorizationV2 库本身从'account'里面取了 如需自定义可以覆盖或者 config中加入
getToken
func
// 默认获取account的方法
const defaultGetAccount = () => {
let key = tracker.config.accountKey || 'account'
let account
try {
account = wx.getStorageSync(key) || {}
} catch (e) {
console.warn(e, TAG)
}
return account
}
// 默认获取token的方法
const defaultGetToken = () => {
let tokenKey = tracker.config.tokenKey || 'sdToken'
let account = tracker.config.getAccount()
return account[tokenKey]
}
tracker.data = {
...tracker.data,
shareSourceId,
userSourceId,
selfTagSourceId
}
更多详细api见demo 和源码
说明
扩展
微信小程序统计
默认注入公共参数
不需要再传了,其他参数见wiki
分类 | 名称 | 类型 | 描述 | 备注 |
---|---|---|---|---|
version | string | 日志版本号 | 1.0 | |
opTime | long | 事件触发时间点(毫秒) | ||
channel | int | scene | onLaunch => option.scene 场景值 | |
platform | string | 平台,如ios、android、wx、mp? | 小程序是mp | |
actionType | string | 小程序值固定为: actionType: 'miniapp', | ||
latitude | double | scene | ||
longitude | double | scene | ||
visitId | string | 当前会话唯一随机字符串(session id) | ||
visitDv | string | 当前会话唯一随机字符串(session id) | ||
pageName | string | |||
fromPath | string | |||
shareSourceId | string | |||
userSourceId | string | 点击别人分享进入小程序的那人用户id | ||
shareSourceId | string | 点击别人分享进入小程序的那条分享id | hash.default | |
selfTagSourceId | string | 点击别人分享进入小程序的那人的selfTag | hash.default | |
shareTo | int | |||
msgChannel | string | |||
authorizationV2 | string | |||
selfTag | string |
extInfo
分类 | 名称 | 类型 | 描述 | 备注 |
---|---|---|---|---|
shareAction | int | [1(页面按钮), 2(右上角菜单)] | ||
id | string | 当前分享id |
分享url参数注入
分类 | 名称 | 类型 | 描述 | 备注 |
---|---|---|---|---|
userSourceId | string | 当前用户id | 默认取 wx.getStorageSync('userId') | |
shareSourceId | string | 当前分享的id | hash.default | |
selfTagSourceId | string | 当前用户selfTag | hash.default |
更新日志
v0.7.7
fix bug ▶ babel libsrc -d lib
v0.7.6
fix bug
v0.7.5
修复 pageEnter | if (discardSamePageTrack && currentShowPage === tracker.data.currentPage) { // 若相等则 页面并没有切换
v0.7.4
新增 tracker.onAppHide() 切到后台 视为 跳转到了 名字为 fakePageBackground 的新页面 用于精细化统计
v0.7.3
fix msgChannel没记上
v0.7.2
fix fromPage 丢失 bug版本[0.6.5 - 0.7.1]
commonControlArg中增加host参数可配 host: 'shuidichou.com'
v0.7.1
修改默认 log api 为 url: 'http://log.shuidichou.com/log'
v0.7.0
增加公共字段actionType: miniapp
v0.6.5
修改使用cryptoUserId 替换userId config 增加 accountKey字段 用于获取 account: {cryptoUserId}
- commonParams 里面不要放selfTag 库本身已经接管selfTag 若想获取可以调用tracker.getSelfTag() 或tracker.config.getSelfTag()
- commonParams 里面不要放authorizationV2 库本身从account里面取了sdToken 如需自定义可以覆盖或者 config中加入getToken func
- config中原有的 getUserId 可以删除 默认取的account 里面的 cryptoUserId
v0.6.4
onPageShow 增加一个参数 onPageShow({op: 'overrideOp', extInfo: {word: 'fakeWord'}}) 用于增加pageEnter统计数据
v0.6.3
分享时自动拼上selfTagSourceId,并统计点击分享进入的selfTagSourceId
v0.6.2
app.wpy => onLaunch 自动调用 tracker.track({op: 'start'})
v0.6.1
onShareAppMessage 增加第三个参数 trackOptions
v0.6.0
增加 api
onShareAppMessage
config 增加 getUserId
v0.5.1
rename tracker.wraper 2 tracker.wrapper
v0.5.0
修正readme
v0.4.3
增加shareSourceId, userSourceId, msgChannel, shareTo 字段 api 增加tracker.onAppLaunch, tracker.onAppShow, tracker.onPageShow, tracker.promoteShare
v0.4.2
修改readme格式
v0.4.1
同步部分readme注释
v0.4.0
增加onShow方法 用来统计页面 附带增加公共 参数 [pageName, fromPath, visitDv] 重写readme
v0.3.1
修复import error
v0.3.0
更新一波功能
v0.2.3
fix bug
Possible Unhandled Promise Rejection: TypeError: Cannot read property 'then' of undefined
v0.2.0
增加 commonControlArg 来自定义url
v0.1.9
更新track-core version 0.0.5
v0.1.8
修改body为数组
v0.1.7
补充opTime
v0.1.5
fix import err
v0.1.4
抽离track-core
v0.1.3
showLog 控制请求返回log是否显示