retention.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. <template>
  2. <!-- 对应页面:注册用户统计-留存 -->
  3. <view class="fix-top-window">
  4. <view class="uni-header">
  5. <uni-stat-breadcrumb class="uni-stat-breadcrumb-on-phone" />
  6. <view class="uni-group">
  7. <view class="uni-sub-title hide-on-phone">用户留存趋势分析</view>
  8. </view>
  9. </view>
  10. <view class="uni-container">
  11. <view class="uni-stat--x flex">
  12. <uni-data-select collection="opendb-app-list" field="appid as value, name as text" orderby="text asc"
  13. :defItem="1" label="应用选择" @change="changeAppid" v-model="query.appid" :clear="false" />
  14. <uni-data-select collection="opendb-app-versions" :where="versionQuery"
  15. field="_id as value, version as text" orderby="text asc" label="版本选择" v-model="query.version_id" />
  16. <view class="flex">
  17. <uni-stat-tabs label="日期选择" :current="currentDateTab" mode="date" :yesterday="false"
  18. @change="changeTimeRange" />
  19. <uni-datetime-picker type="daterange" :end="new Date().getTime()" v-model="query.start_time"
  20. returnType="timestamp" :clearIcon="false" class="uni-stat-datetime-picker"
  21. :class="{'uni-stat__actived': currentDateTab < 0 && !!query.start_time.length}"
  22. @change="useDatetimePicker" />
  23. </view>
  24. </view>
  25. <view class="uni-stat--x">
  26. <uni-stat-tabs label="平台选择" type="boldLine" mode="platform" v-model="query.platform_id"
  27. @change="changePlatform" />
  28. <uni-data-select v-if="query.platform_id && query.platform_id.indexOf('==') === -1"
  29. :localdata="channelData" label="渠道选择" v-model="query.channel_id"></uni-data-select>
  30. </view>
  31. <view class="uni-stat--x mb-m" style="padding-top: 0;">
  32. <view class="mb-m line-bottom">
  33. <uni-stat-tabs type="boldLine" :tabs="fields" v-model="field" tooltip
  34. style="line-height: 40px; margin-bottom: -17px;" />
  35. </view>
  36. <uni-stat-tabs type="box" :tabs="keys" v-model="key" class="mb-l" />
  37. <view class="p-m">
  38. <view class="uni-charts-box">
  39. <qiun-data-charts type="area" :chartData="chartData" echartsH5 echartsApp />
  40. </view>
  41. </view>
  42. </view>
  43. <view class="uni-stat--x p-m">
  44. <view class="uni-tips mb-s flex">
  45. <uni-icons type="info"></uni-icons>
  46. 表格中显示为空,表示留存为 0 或无数据
  47. </view>
  48. <uni-table :loading="loading" stripe :emptyText="$t('common.empty')">
  49. <uni-tr style="background-color: #eee;">
  50. <template v-for="(mapper, index) in fieldsMap">
  51. <uni-th v-if="mapper.title" :key="index" align="center">{{mapper.title}}</uni-th>
  52. </template>
  53. </uni-tr>
  54. <uni-tr v-for="(item ,i) in tableData" :key="i">
  55. <template v-for="(mapper, index) in fieldsMap">
  56. <uni-td v-if="mapper.title" :key="index" align="center"
  57. :class="/[d|w|m]_\d/.test(mapper.field)&&[item[mapper.field] ? 'uni-stat-table-bg' : '']">
  58. {{item[mapper.field] ? item[mapper.field] : ''}}
  59. </uni-td>
  60. </template>
  61. </uni-tr>
  62. </uni-table>
  63. <view class="uni-pagination-box">
  64. <uni-pagination show-icon show-page-size :page-size="options.pageSize"
  65. :current="options.pageCurrent" :total="options.total" @change="changePageCurrent"
  66. @pageSizeChange="changePageSize" />
  67. </view>
  68. </view>
  69. </view>
  70. <!-- #ifndef H5 -->
  71. <fix-window />
  72. <!-- #endif -->
  73. </view>
  74. </template>
  75. <script>
  76. import {
  77. mapfields,
  78. stringifyQuery,
  79. stringifyField,
  80. stringifyGroupField,
  81. getTimeOfSomeDayAgo,
  82. division,
  83. format,
  84. formatDate,
  85. debounce
  86. } from '@/js_sdk/uni-stat/util.js'
  87. import fieldsFactory from './fieldsMap.js'
  88. export default {
  89. data() {
  90. return {
  91. query: {
  92. dimension: "day",
  93. appid: '',
  94. platform_id: '',
  95. uni_platform: '',
  96. version_id: '',
  97. channel_id: '',
  98. start_time: [],
  99. },
  100. options: {
  101. pageSize: 20,
  102. pageCurrent: 1, // 当前页
  103. total: 0, // 数据总量
  104. },
  105. loading: false,
  106. currentDateTab: 0,
  107. tableData: [],
  108. chartData: {},
  109. field: 'new_user',
  110. fields: [{
  111. _id: 'new_user',
  112. name: '新增留存',
  113. tooltip: '指定时间新增(即首次访问应用)用户,在之后的第N天,再次访问应用的用户数占比'
  114. }, {
  115. _id: 'active_user',
  116. name: '活跃留存',
  117. tooltip: '指定时间活跃(即访问应用)用户,在之后的第N天,再次访问应用的用户数占比'
  118. }],
  119. key: 1,
  120. channelData: []
  121. }
  122. },
  123. computed: {
  124. fieldsMap() {
  125. const title = this.field === 'active_user' ? '活跃用户' : '新增用户'
  126. const maps = [{
  127. title,
  128. field: `${this.field}_count`,
  129. stat: 0
  130. }]
  131. return fieldsFactory(maps)
  132. },
  133. fieldName() {
  134. let name = ''
  135. this.fields.forEach(item => {
  136. if (item._id === this.field) {
  137. name = item.name
  138. }
  139. })
  140. return name
  141. },
  142. keyName() {
  143. return this.keys.forEach(item => {
  144. if (item._id === this.key) {
  145. return item.name
  146. }
  147. })
  148. },
  149. keys() {
  150. const values = [1, 2, 3, 4, 5, 6, 7, 14, 30]
  151. return values.map(val => {
  152. return {
  153. _id: val,
  154. name: `${val}天后`
  155. }
  156. })
  157. },
  158. channelQuery() {
  159. const platform_id = this.query.platform_id
  160. return stringifyQuery({
  161. platform_id
  162. })
  163. },
  164. versionQuery() {
  165. const {
  166. appid,
  167. uni_platform
  168. } = this.query
  169. const query = stringifyQuery({
  170. appid,
  171. uni_platform
  172. })
  173. return query
  174. }
  175. },
  176. created() {
  177. this.debounceGet = debounce(() => this.getAllData(this.query))
  178. this.getChannelData()
  179. },
  180. watch: {
  181. query: {
  182. deep: true,
  183. handler(val) {
  184. this.options.pageCurrent = 1 // 重置分页
  185. this.debounceGet()
  186. }
  187. },
  188. key() {
  189. this.debounceGet()
  190. },
  191. field() {
  192. this.debounceGet()
  193. }
  194. },
  195. methods: {
  196. useDatetimePicker() {
  197. this.currentDateTab = -1
  198. },
  199. changeAppid(id) {
  200. this.getChannelData(id, false)
  201. },
  202. changePlatform(id, index, name, item) {
  203. this.getChannelData(null, id)
  204. this.query.version_id = 0
  205. this.query.uni_platform = item.code
  206. },
  207. changeTimeRange(id, index) {
  208. this.currentDateTab = index
  209. const start = getTimeOfSomeDayAgo(id),
  210. end = getTimeOfSomeDayAgo(0) - 1
  211. this.query.start_time = [start, end]
  212. },
  213. changePageCurrent(e) {
  214. this.options.pageCurrent = e.current
  215. this.getTabelData(this.query)
  216. },
  217. changePageSize(pageSize) {
  218. this.options.pageSize = pageSize
  219. this.options.pageCurrent = 1 // 重置分页
  220. this.getTabelData(this.query)
  221. },
  222. // 此处 util 中的 stringifyField 不满足需求,特殊处理 stringifyField
  223. stringifyField(mapping, goal, prop) {
  224. if (goal) {
  225. mapping = mapping.filter(f => f.field === goal)
  226. }
  227. if (prop) {
  228. mapping = mapping.filter(f => f.field && f.hasOwnProperty(prop))
  229. }
  230. const fields = mapping.map(f => {
  231. if (f.stat === -1) {
  232. return f.field
  233. } else if (f.stat === 0) {
  234. return `${f.field} as ${ 'temp_' + f.field}`
  235. } else {
  236. return `retention.${this.field}.${f.field}.user_count as ${ 'temp_' + f.field}`
  237. }
  238. }).join()
  239. return fields
  240. },
  241. // 此处 util 中的 groupField 不满足需求,特殊处理 groupField
  242. createStr(type = "user_count", vals, fields, tail) {
  243. const value = vals || [1, 2, 3, 4, 5, 6, 7, 14, 30]
  244. const p = 'd'
  245. const f = this.fields.map(item => item._id)
  246. fields = fields || f
  247. const strArr = value.map(item => {
  248. return fields.map(field => {
  249. return `retention.${field}.${p + '_' + item}.${type} as ${p + '_' + item}`
  250. })
  251. })
  252. if (tail) {
  253. strArr.push(tail)
  254. }
  255. const str = strArr.join()
  256. return str
  257. },
  258. getAllData(query) {
  259. this.getChartData(query, this.key, this.keyName)
  260. this.getTabelData(query)
  261. },
  262. getChartData(query, key = this.key, name = '访问人数') {
  263. // this.chartData = {}
  264. const {
  265. pageCurrent
  266. } = this.options
  267. query = stringifyQuery(query, null, ['uni_platform'])
  268. const groupField = this.createStr("user_count", [key], [this.field])
  269. const db = uniCloud.database()
  270. db.collection('uni-stat-result')
  271. .where(query)
  272. .field(`${this.stringifyField(this.fieldsMap, `d_${key}`)}, start_time`)
  273. .groupBy('start_time')
  274. .groupField(stringifyGroupField(this.fieldsMap, `d_${key}`))
  275. .orderBy('start_time', 'asc')
  276. .get({
  277. getCount: true
  278. })
  279. .then(res => {
  280. let {
  281. count,
  282. data
  283. } = res.result
  284. const options = {
  285. categories: [],
  286. series: [{
  287. name: `${key}天后${this.fieldName}`,
  288. data: []
  289. }]
  290. }
  291. for (const item of data) {
  292. const x = formatDate(item.start_time, 'day')
  293. const y = item[`d_${key}`]
  294. options.series[0].data.push(y)
  295. options.categories.push(x)
  296. }
  297. this.chartData = options
  298. }).catch((err) => {
  299. console.error(err)
  300. // err.message 错误信息
  301. // err.code 错误码
  302. }).finally(() => {
  303. this.loading = false
  304. })
  305. },
  306. getTabelData(query) {
  307. const {
  308. pageCurrent
  309. } = this.options
  310. query = stringifyQuery(query, null, ['uni_platform'])
  311. const tail = this.field + "_count"
  312. const groupField = this.createStr('user_rate', '', [this.field], tail)
  313. this.loading = true
  314. const db = uniCloud.database()
  315. db.collection('uni-stat-result')
  316. .where(query)
  317. .field(this.stringifyField(this.fieldsMap))
  318. .groupBy('start_time')
  319. .groupField(stringifyGroupField(this.fieldsMap))
  320. .orderBy('start_time', 'desc')
  321. .skip((pageCurrent - 1) * this.options.pageSize)
  322. .limit(this.options.pageSize)
  323. .get({
  324. getCount: true
  325. })
  326. .then(res => {
  327. const {
  328. count,
  329. data
  330. } = res.result
  331. for (const item of data) {
  332. mapfields(this.fieldsMap, item, item)
  333. }
  334. this.options.total = count
  335. this.tableData = []
  336. this.tableData = data
  337. }).catch((err) => {
  338. console.error(err)
  339. // err.message 错误信息
  340. // err.code 错误码
  341. }).finally(() => {
  342. this.loading = false
  343. })
  344. },
  345. //获取渠道信息
  346. getChannelData(appid, platform_id) {
  347. this.query.channel_id = ''
  348. const db = uniCloud.database()
  349. const condition = {}
  350. //对应应用
  351. appid = appid ? appid : this.query.appid
  352. if (appid) {
  353. condition.appid = appid
  354. }
  355. //对应平台
  356. platform_id = platform_id ? platform_id : this.query.platform_id
  357. if (platform_id) {
  358. condition.platform_id = platform_id
  359. }
  360. let platformTemp = db.collection('uni-stat-app-platforms')
  361. .field('_id, name')
  362. .getTemp()
  363. let channelTemp = db.collection('uni-stat-app-channels')
  364. .where(condition)
  365. .field('_id, channel_name, create_time, platform_id')
  366. .getTemp()
  367. db.collection(channelTemp, platformTemp)
  368. .orderBy('platform_id', 'asc')
  369. .get()
  370. .then(res => {
  371. let data = res.result.data
  372. let channels = []
  373. if (data.length > 0) {
  374. let channelName
  375. for (let i in data) {
  376. channelName = data[i].channel_name ? data[i].channel_name : '默认'
  377. if (data[i].platform_id.length > 0) {
  378. channelName = data[i].platform_id[0].name + '-' + channelName
  379. }
  380. channels.push({
  381. value: data[i]._id,
  382. text: channelName
  383. })
  384. }
  385. }
  386. this.channelData = channels
  387. })
  388. .catch((err) => {
  389. console.error(err)
  390. // err.message 错误信息
  391. // err.code 错误码
  392. }).finally(() => {})
  393. }
  394. }
  395. }
  396. </script>
  397. <style lang="scss">
  398. .flex {
  399. display: flex;
  400. flex-wrap: wrap;
  401. align-items: center;
  402. }
  403. .label-text {
  404. font-size: 14px;
  405. color: #666;
  406. margin: auto 0;
  407. margin-right: 5px;
  408. }
  409. .line-bottom {
  410. border-bottom: 2px solid #eee;
  411. }
  412. .uni-stat-table-bg {
  413. background-color: #4e82d9;
  414. color: #fff;
  415. }
  416. </style>