retention.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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 p-1015">
  12. <view class="uni-stat--app-select">
  13. <uni-data-select collection="opendb-app-list" field="appid as value, name as text" orderby="text asc" :defItem="1" label="应用选择" @change="changeAppid" v-model="query.appid" :clear="false" />
  14. <uni-data-select collection="opendb-app-versions" :where="versionQuery" class="ml-m" field="_id as value, version as text, uni_platform as label, create_date as date" format="{label} - {text}" orderby="date desc" label="版本选择" v-model="query.version_id" />
  15. </view>
  16. </view>
  17. <view class="uni-stat--x flex">
  18. <uni-stat-tabs label="日期选择" :current="currentDateTab" mode="date" :yesterday="false" @change="changeTimeRange" />
  19. <uni-datetime-picker type="datetimerange" :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 class="uni-stat--x">
  25. <uni-stat-tabs label="平台选择" type="boldLine" mode="platform" v-model="query.platform_id" @change="changePlatform" />
  26. <uni-data-select ref="version-select" v-if="query.platform_id && query.platform_id.indexOf('==') === -1" collection="uni-stat-app-channels" :where="channelQuery" class="p-channel" field="_id as value, channel_name as text" orderby="text asc" label="渠道/场景值选择" v-model="query.channel_id" />
  27. </view>
  28. <view class="uni-stat--x mb-m" style="padding-top: 0;">
  29. <view class="mb-m line-bottom">
  30. <uni-stat-tabs type="boldLine" :tabs="fields" v-model="field" tooltip
  31. style="line-height: 40px; margin-bottom: -17px;" />
  32. </view>
  33. <uni-stat-tabs type="box" :tabs="keys" v-model="key" class="mb-l" />
  34. <view class="p-m">
  35. <view class="uni-charts-box">
  36. <qiun-data-charts type="area" :chartData="chartData" echartsH5 echartsApp :errorMessage="errorMessage"/>
  37. </view>
  38. </view>
  39. </view>
  40. <view class="uni-stat--x p-m">
  41. <view class="uni-tips mb-s flex">
  42. <uni-icons type="info"></uni-icons>
  43. 表格中显示为空,表示留存为 0 或无数据
  44. </view>
  45. <uni-table :loading="loading" stripe :emptyText="errorMessage || $t('common.empty')">
  46. <uni-tr style="background-color: #eee;">
  47. <block v-for="(mapper, index) in fieldsMap" :key="index">
  48. <uni-th v-if="mapper.title" :key="index" align="center">{{mapper.title}}</uni-th>
  49. </block>
  50. </uni-tr>
  51. <uni-tr v-for="(item ,i) in tableData" :key="i">
  52. <block v-for="(mapper, index) in fieldsMap" :key="index">
  53. <uni-td v-if="mapper.title" :key="index" align="center"
  54. :class="/[d|w|m]_\d/.test(mapper.field)&&[item[mapper.field] ? 'uni-stat-table-bg' : '']">
  55. {{item[mapper.field] ? item[mapper.field] : ''}}
  56. </uni-td>
  57. </block>
  58. </uni-tr>
  59. </uni-table>
  60. <view class="uni-pagination-box">
  61. <uni-pagination show-icon show-page-size :page-size="options.pageSize"
  62. :current="options.pageCurrent" :total="options.total" @change="changePageCurrent"
  63. @pageSizeChange="changePageSize" />
  64. </view>
  65. </view>
  66. </view>
  67. <!-- #ifndef H5 -->
  68. <fix-window />
  69. <!-- #endif -->
  70. </view>
  71. </template>
  72. <script>
  73. import {
  74. mapfields,
  75. stringifyQuery,
  76. stringifyField,
  77. stringifyGroupField,
  78. getTimeOfSomeDayAgo,
  79. division,
  80. format,
  81. formatDate,
  82. debounce
  83. } from '@/js_sdk/uni-stat/util.js'
  84. import fieldsFactory from './fieldsMap.js'
  85. export default {
  86. data() {
  87. return {
  88. query: {
  89. dimension: "day",
  90. appid: '',
  91. platform_id: '',
  92. uni_platform: '',
  93. version_id: '',
  94. channel_id: '',
  95. start_time: [],
  96. },
  97. options: {
  98. pageSize: 20,
  99. pageCurrent: 1, // 当前页
  100. total: 0, // 数据总量
  101. },
  102. loading: false,
  103. currentDateTab: 0,
  104. tableData: [],
  105. chartData: {},
  106. field: 'new_user',
  107. fields: [{
  108. _id: 'new_user',
  109. name: '新增留存',
  110. tooltip: '指定时间新增(即首次访问应用)用户,在之后的第N天,再次访问应用的用户数占比'
  111. }, {
  112. _id: 'active_user',
  113. name: '活跃留存',
  114. tooltip: '指定时间活跃(即访问应用)用户,在之后的第N天,再次访问应用的用户数占比'
  115. }],
  116. key: 1,
  117. channelData: [],
  118. errorMessage: "",
  119. }
  120. },
  121. computed: {
  122. fieldsMap() {
  123. const title = this.field === 'active_user' ? '活跃用户' : '新增用户'
  124. const maps = [{
  125. title,
  126. field: `${this.field}_count`,
  127. stat: 0
  128. }]
  129. return fieldsFactory(maps)
  130. },
  131. fieldName() {
  132. let name = ''
  133. this.fields.forEach(item => {
  134. if (item._id === this.field) {
  135. name = item.name
  136. }
  137. })
  138. return name
  139. },
  140. keyName() {
  141. return this.keys.forEach(item => {
  142. if (item._id === this.key) {
  143. return item.name
  144. }
  145. })
  146. },
  147. keys() {
  148. const values = [1, 2, 3, 4, 5, 6, 7, 14, 30]
  149. return values.map(val => {
  150. return {
  151. _id: val,
  152. name: `${val}天后`
  153. }
  154. })
  155. },
  156. channelQuery() {
  157. const platform_id = this.query.platform_id
  158. return stringifyQuery({
  159. platform_id
  160. })
  161. },
  162. versionQuery() {
  163. const {
  164. appid,
  165. uni_platform
  166. } = this.query
  167. const query = stringifyQuery({
  168. appid,
  169. uni_platform
  170. })
  171. return query
  172. }
  173. },
  174. created() {
  175. this.debounceGet = debounce(() => {
  176. this.getAllData(this.query);
  177. }, 300);
  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. if (!query.appid) {
  260. this.errorMessage = "请先选择应用";
  261. return; // 如果appid为空,则不进行查询
  262. }
  263. this.errorMessage = "";
  264. this.getChartData(query, this.key, this.keyName)
  265. this.getTabelData(query)
  266. },
  267. getChartData(query, key = this.key, name = '访问人数') {
  268. // this.chartData = {}
  269. const {
  270. pageCurrent
  271. } = this.options
  272. query = stringifyQuery(query, null, ['uni_platform'])
  273. const groupField = this.createStr("user_count", [key], [this.field])
  274. const db = uniCloud.database()
  275. db.collection('uni-stat-result')
  276. .where(query)
  277. .field(`${this.stringifyField(this.fieldsMap, `d_${key}`)}, start_time`)
  278. .groupBy('start_time')
  279. .groupField(stringifyGroupField(this.fieldsMap, `d_${key}`))
  280. .orderBy('start_time', 'asc')
  281. .get({
  282. getCount: true
  283. })
  284. .then(res => {
  285. let {
  286. count,
  287. data
  288. } = res.result
  289. const options = {
  290. categories: [],
  291. series: [{
  292. name: `${key}天后${this.fieldName}`,
  293. data: []
  294. }]
  295. }
  296. for (const item of data) {
  297. const x = formatDate(item.start_time, 'day')
  298. const y = item[`d_${key}`]
  299. options.series[0].data.push(y)
  300. options.categories.push(x)
  301. }
  302. this.chartData = options
  303. }).catch((err) => {
  304. console.error(err)
  305. // err.message 错误信息
  306. // err.code 错误码
  307. }).finally(() => {
  308. this.loading = false
  309. })
  310. },
  311. getTabelData(query) {
  312. const {
  313. pageCurrent
  314. } = this.options
  315. query = stringifyQuery(query, null, ['uni_platform'])
  316. const tail = this.field + "_count"
  317. const groupField = this.createStr('user_rate', '', [this.field], tail)
  318. this.loading = true
  319. const db = uniCloud.database()
  320. db.collection('uni-stat-result')
  321. .where(query)
  322. .field(this.stringifyField(this.fieldsMap))
  323. .groupBy('start_time')
  324. .groupField(stringifyGroupField(this.fieldsMap))
  325. .orderBy('start_time', 'desc')
  326. .skip((pageCurrent - 1) * this.options.pageSize)
  327. .limit(this.options.pageSize)
  328. .get({
  329. getCount: true
  330. })
  331. .then(res => {
  332. const {
  333. count,
  334. data
  335. } = res.result
  336. for (const item of data) {
  337. mapfields(this.fieldsMap, item, item)
  338. }
  339. this.options.total = count
  340. this.tableData = []
  341. this.tableData = data
  342. }).catch((err) => {
  343. console.error(err)
  344. // err.message 错误信息
  345. // err.code 错误码
  346. }).finally(() => {
  347. this.loading = false
  348. })
  349. },
  350. //获取渠道信息
  351. getChannelData(appid, platform_id) {
  352. this.query.channel_id = ''
  353. const db = uniCloud.database()
  354. const condition = {}
  355. //对应应用
  356. appid = appid ? appid : this.query.appid
  357. if (appid) {
  358. condition.appid = appid
  359. }
  360. //对应平台
  361. platform_id = platform_id ? platform_id : this.query.platform_id
  362. if (platform_id) {
  363. condition.platform_id = platform_id
  364. }
  365. let platformTemp = db.collection('uni-stat-app-platforms')
  366. .field('_id, name')
  367. .getTemp()
  368. let channelTemp = db.collection('uni-stat-app-channels')
  369. .where(condition)
  370. .field('_id, channel_name, create_time, platform_id')
  371. .getTemp()
  372. db.collection(channelTemp, platformTemp)
  373. .orderBy('platform_id', 'asc')
  374. .get()
  375. .then(res => {
  376. let data = res.result.data
  377. let channels = []
  378. if (data.length > 0) {
  379. let channelName
  380. for (let i in data) {
  381. channelName = data[i].channel_name ? data[i].channel_name : '默认'
  382. if (data[i].platform_id.length > 0) {
  383. channelName = data[i].platform_id[0].name + '-' + channelName
  384. }
  385. channels.push({
  386. value: data[i]._id,
  387. text: channelName
  388. })
  389. }
  390. }
  391. this.channelData = channels
  392. })
  393. .catch((err) => {
  394. console.error(err)
  395. // err.message 错误信息
  396. // err.code 错误码
  397. }).finally(() => {})
  398. }
  399. }
  400. }
  401. </script>
  402. <style lang="scss">
  403. .flex {
  404. display: flex;
  405. flex-wrap: wrap;
  406. align-items: center;
  407. }
  408. .label-text {
  409. font-size: 14px;
  410. color: #666;
  411. margin: auto 0;
  412. margin-right: 5px;
  413. }
  414. .line-bottom {
  415. border-bottom: 2px solid #eee;
  416. }
  417. .uni-stat-table-bg {
  418. background-color: #4e82d9;
  419. color: #fff;
  420. }
  421. </style>