const {
collect, // 缓存渠道数据
} = require('some_help_function')
App({
onShow(options) {
options.scene && collect('chan_wxapp_scene', options.scene)
options.referrerInfo.appId && collect('chan_refer_app_id', options.referrerInfo.appId)
}
})
渠道数据收集方法
本文主要描述小程序中渠道数据的系统化收集方法
渠道模型
渠道是用来追踪流量的数字化工具
渠道数据一般都会聚合在一个叫`chan`的结构里
在 App.onShow
里收集
-
总是在
App.onShow
里采集渠道数据,具体采集方法见下表 -
每次触发
App.onShow
时候,若新的渠道数据非空,则应该覆盖旧的渠道数据
渠道参数 | 类型 | 收集方法 |
---|---|---|
chan_id |
string |
自定义渠道的收集方法较复杂,详见下节 |
chan_wxapp_scene |
int |
就是小程序场景值,可从 |
chan_refer_app_id |
string |
来源公众号或小程序appID,可从 |
注意应该在
onShow
函数内收集渠道数据而不是onLaunch
因为在整个小程序周期内用户可能多次进出小程序从而多次触发onShow
,用户每次重新进入小程序都有可能改变当前会话的渠道信息。 而onLaunch
在整个小程序生命周期内只触发一次
参考代码
自定义渠道chan_id收集方法
chan_id
可在多种场景进行标记,下面介绍各个情况下的标记及收集方法
1. 通过接口A生成小程序码
使用接口A生成小程序码时需要将 chan_id=<渠道值>
作为URL查询参数填入 path
例如:为了统计 `chan_id=shop1357` 的门店的流量情况,且扫描小程序码时打开页面`/home/index` 调用接口A生成小程序码时,`path` 填入 `/home/index?chan_id=shop1356`
收集方法
App({
onShow({ query }) {
let chan_id = query.chan_id
console.log(`chan_id: ${chan_id}`)
}
})
2. 通过接口B生成小程序码
使用接口B生成小程序码时需要将 chan_id=<渠道值>
作为 scene
的一部分填入
例如:为了统计`chan_id=shop1357`的门店的流量情况,且扫描小程序码时打开页面`/home/index` 调用接口B生成小程序码时,`path` 填入 `/home/index`,`scene` 填入 `chan_id=shop1357`
收集方法
App({
onShow({ query }) {
const scene = decodeURIComponent(query.scene)
let match = scene.split('&').map(m => m.match(/^chan_id=(.*)$/)).find(m => !!m)
if (match) {
console.log(`chan_id=${match[1]}`)
} else {
console.log('do not find chan_id')
}
}
})
4. 通过wx.navigateToMiniProgram跳转小程序
当其他小程序通过wx.navigateToMiniProgram跳转过来时,chan_id
可能会以查询参数的形式附加在 path
参数上
收集方法同接口A一致,不再赘述
统一收集
用一段代码同时兼容以上4种情况:
App({
_getChanID(query) {
let chan_id = query.chan_id
if (chan_id) { return chan_id }
let sceneInQuery = decodeURIComponent(query.scene)
let match = sceneInQuery.split('&').map(m => m.match(/^chan_id=(.*)$/)).find(m => !!m)
if (match) { return match[1] }
},
onShow({ query }) {
let chan_id = this._getChanID(query)
console.log(`chan_id=${chan_id}`)
}
})
Copyright © 1998-2019 Tencent. All Rights Reserved.