retention.vue 12 KB

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