Feat(页面):未登录时页面埋点数据写入缓存以便下次登录时获取上报

This commit is contained in:
lifizer
2024-06-16 16:33:41 +08:00
parent 4e6d5598b7
commit 19914b5b0d
4 changed files with 44 additions and 7 deletions
+34 -1
View File
@@ -1,5 +1,6 @@
import Vue from 'vue'
import { pushSystemStatic } from '@/api/public'
import { pageViewDataCacheKey } from '@/config/index'
// 日期格式化
export function formatterDate(date, flag = '-', mode = 'date') {
@@ -54,6 +55,24 @@ export function pageToWatchShowTimer(key, data) {
if (Vue.prototype.$pageToWatchData[key]) {
Vue.prototype.$pageToWatchData[key].push(data)
}
const userInfo = uni.getStorageSync('userInfo')
if (!userInfo) {
uni.setStorageSync(pageViewDataCacheKey, Vue.prototype.$pageToWatchData)
}
}
/**
* desc: 获取缓存页面埋点数据
* auth: lifizer
* date: 2024-06-16
* @returns {data}
*/
export function getPageViewDataToLocalCache() {
let data = {}
if (uni.setStorageSync(pageViewDataCacheKey)) {
data = uni.setStorageSync(pageViewDataCacheKey)
}
return data
}
/**
@@ -130,6 +149,10 @@ function pushSysBySecondsHandle() {
statsData = statsData.concat(Vue.prototype.$pageToWatchData[key])
}
}
if (statsData.length < 1) {
console.log('*************没有页面埋点数据则不用上报,节约网络请求')
return
}
// 设备类型phone、pad、pc、unknow
const deviceType = uni.getSystemInfoSync().deviceType
// 系统名称ios、android
@@ -142,7 +165,17 @@ function pushSysBySecondsHandle() {
statsData
}
console.log('开始上报数据-------------------')
pushSystemStatic(postData).catch(err => {
pushSystemStatic(postData).then(res => {
const { success } = res
if (success) {
// 上报成功之后清空上一次的页面埋点数据
for (const key in Vue.prototype.$pageToWatchData) {
Vue.prototype.$pageToWatchData[key] = []
}
console.log('*************清除上次未登录缓存的页面埋点数据')
uni.removeStorageSync(pageViewDataCacheKey)
}
}).catch(err => {
console.log(err)
})
}, 30 * 1000)