提交 93203dfd 编写于 作者: M mehaotian

fix(stat): uni-stat build

上级 3c7db84f
......@@ -15,16 +15,16 @@ const OPERATING_TIME = 10; // 数据上报时间 单位s
const DIFF_TIME = 60 * 1000 * 60 * 24;
let statConfig = {
appid: ''
appid: process.env.UNI_APP_ID,
};
let titleJsons = {};
let debug = !!process.env.UNI_STAT_DEBUG || false;
// #ifdef VUE3
statConfig.appid = process.env.UNI_APP_ID;
titleJsons = process.env.UNI_STAT_TITLE_JSON;
// #endif
// #ifndef VUE3
statConfig = require('uni-stat-config').default || require('uni-stat-config');
// eslint-disable-next-line no-restricted-globals
const pagesTitle = require('uni-pages?{"type":"style"}').default;
let pagesData = pagesTitle.pages;
for (let i in pagesData) {
......@@ -33,7 +33,7 @@ for (let i in pagesData) {
// MP
style.navigationBarTitleText ||
// ali
style.defaultTitle||
style.defaultTitle ||
// H5 || App
style.navigationBar?.titleText ||
'';
......@@ -43,47 +43,46 @@ for (let i in pagesData) {
}
// #endif
// TODO 在云函数中获取,暂时注释
// const UUID_KEY = '__DC_STAT_UUID'
// const UUID_VALUE = '__DC_UUID_VALUE'
// function getUuid() {
// let uuid = ''
// if (get_platform_name() === 'n') {
// try {
// uuid = plus.runtime.getDCloudId()
// } catch (e) {
// uuid = ''
// }
// return uuid
// }
// try {
// uuid = uni.getStorageSync(UUID_KEY)
// } catch (e) {
// uuid = UUID_VALUE
// }
// if (!uuid) {
// uuid = Date.now() + '' + Math.floor(Math.random() * 1e7)
// try {
// uni.setStorageSync(UUID_KEY, uuid)
// } catch (e) {
// uni.setStorageSync(UUID_KEY, UUID_VALUE)
// }
// }
// return uuid
// }
// export const get_uuid = (statData) => {
// // 有可能不存在 deviceId(一般不存在就是出bug了),就自己生成一个
// return sys.deviceId || getUuid()
// }
const UUID_KEY = '__DC_STAT_UUID';
const UUID_VALUE = '__DC_UUID_VALUE';
function getUuid() {
let uuid = '';
if (get_platform_name() === 'n') {
try {
uuid = plus.runtime.getDCloudId();
} catch (e) {
uuid = '';
}
return uuid
}
try {
uuid = uni.getStorageSync(UUID_KEY);
} catch (e) {
uuid = UUID_VALUE;
}
if (!uuid) {
uuid = Date.now() + '' + Math.floor(Math.random() * 1e7);
try {
uni.setStorageSync(UUID_KEY, uuid);
} catch (e) {
uni.setStorageSync(UUID_KEY, UUID_VALUE);
}
}
return uuid
}
/**
* 获取配置信息 如 appid
*/
const stat_config = statConfig;
const get_uuid = (statData) => {
// 有可能不存在 deviceId(一般不存在就是出bug了),就自己生成一个
return sys.deviceId || getUuid()
};
const get_sgin = (statData) => {
let arr = Object.keys(statData);
let sortArr = arr.sort();
......@@ -127,7 +126,7 @@ const get_platform_name = () => {
// 苹果审核代码中禁止出现 alipay 字样 ,需要特殊处理一下
const aliArr = ['y', 'a', 'p', 'mp-ali'];
const platformList = {
'app': 'n',
app: 'n',
'app-plus': 'n',
h5: 'h5',
'mp-weixin': 'wx',
......@@ -138,13 +137,13 @@ const get_platform_name = () => {
'quickapp-native': 'qn',
'mp-kuaishou': 'ks',
'mp-lark': 'lark',
'quickapp-webview':'qw'
'quickapp-webview': 'qw',
};
if(platformList[process.env.VUE_APP_PLATFORM] === 'ali'){
if(my&&my.env){
if (platformList[process.env.VUE_APP_PLATFORM] === 'ali') {
if (my && my.env) {
const clientName = my.env.clientName;
if(clientName === 'ap') return 'ali'
if(clientName === 'dingtalk') return 'dt'
if (clientName === 'ap') return 'ali'
if (clientName === 'dingtalk') return 'dt'
// TODO 缺少 ali 下的其他平台
}
}
......@@ -222,7 +221,11 @@ const get_route = (pageVm) => {
let scope_route = _self.$scope && _self.$scope.is;
return mp_route || scope_route || ''
} else {
return _self.route || (_self.$scope && _self.$scope.route) || (_self.$mp && _self.$mp.page.route)
return (
_self.route ||
(_self.$scope && _self.$scope.route) ||
(_self.$mp && _self.$mp.page.route)
)
}
};
......@@ -233,11 +236,11 @@ const get_page_route = (pageVm) => {
// 从 app 进入应用 ,没有 $page ,获取不到路由 ,需要获取页面 尝试从 getCurrentPages 获取也页面实例
// FIXME 尽量不使用 getCurrentPages ,大部分获取路由是从 onHide 获取 ,这时可以获取到,如果是 onload ,则可能获取不到,比如 百度
let page = pageVm.$page || (pageVm.$scope && pageVm.$scope.$page);
let page = pageVm && (pageVm.$page || (pageVm.$scope && pageVm.$scope.$page));
let lastPageRoute = uni.getStorageSync('_STAT_LAST_PAGE_ROUTE');
if (!page) return lastPageRoute || ''
// 如果找不到 fullPath 就取 route 的值
return page.fullPath === '/' ? page.route : (page.fullPath||page.route)
return page.fullPath === '/' ? page.route : page.fullPath || page.route
};
/**
......@@ -255,15 +258,23 @@ const get_page_vm = () => {
*/
const get_page_types = (self) => {
// XXX 百度有问题 ,获取的都是 componet ,等待修复
if (self.mpType === 'page' || self.$mpType === 'page' || (self.$mp && self.$mp.mpType === 'page') || self
.$options.mpType === 'page') {
return 'page';
}
if (self.mpType === 'app' || self.$mpType === 'app' || (self.$mp && self.$mp.mpType === 'app') || self.$options
.mpType === 'app') {
if (
self.mpType === 'page' ||
self.$mpType === 'page' ||
(self.$mp && self.$mp.mpType === 'page') ||
self.$options.mpType === 'page'
) {
return 'page'
}
if (
self.mpType === 'app' ||
self.$mpType === 'app' ||
(self.$mp && self.$mp.mpType === 'app') ||
self.$options.mpType === 'app'
) {
return 'app'
}
return null;
return null
};
/**
......@@ -293,7 +304,6 @@ const handle_data = (statData) => {
return JSON.stringify(firstArr)
};
/**
* 自定义事件参数校验
*/
......@@ -304,21 +314,29 @@ const calibration = (eventName, options) => {
return true
}
if (typeof eventName !== 'string') {
console.error(`uni.report [eventName] Parameter type error, it can only be of type String`);
console.error(
`uni.report [eventName] Parameter type error, it can only be of type String`
);
return true
}
if (eventName.length > 255) {
console.error(`uni.report [eventName] Parameter length cannot be greater than 255`);
console.error(
`uni.report [eventName] Parameter length cannot be greater than 255`
);
return true
}
if (typeof options !== 'string' && typeof options !== 'object') {
console.error('uni.report [options] Parameter type error, Only supports String or Object type');
console.error(
'uni.report [options] Parameter type error, Only supports String or Object type'
);
return true
}
if (typeof options === 'string' && options.length > 255) {
console.error(`uni.report [options] Parameter length cannot be greater than 255`);
console.error(
`uni.report [options] Parameter length cannot be greater than 255`
);
return true
}
......@@ -418,6 +436,42 @@ const requestData = (done) => {
});
};
/**
* 是否开启 debug 模式
*/
const is_debug = debug;
/**
* 日志输出
* @param {*} data
*/
const log = (data) => {
let msg_type = '';
switch (data.lt) {
case '1':
msg_type = '应用启动';
break
case '3':
msg_type = '应用进入后台';
break
case '11':
msg_type = '页面切换';
break
case '21':
msg_type = '事件触发';
break
case '31':
msg_type = '应用错误';
break
}
if (msg_type) {
console.log(`=== 统计数据采集:${msg_type} ===`);
console.log(data);
console.log(`=== 采集结束 ===`);
}
};
let data = uni.getStorageSync('$$STAT__DBDATA') || {};
const dbSet = (name, value) => {
if (!data) {
......@@ -587,10 +641,11 @@ const get_residence_time = (type) => {
// 统计数据默认值
let statData = {
uuid: get_uuid(), // 设备标识
// uuid: get_uuid(), // 设备标识
// ak: stat_config.appid, // uni-app 应用 Appid
// p: sys.platform === 'android' ? 'a' : 'i', // 手机系统
ut: get_platform_name(), // 平台类型
mpn: get_pack_name(), // 原生平台包名、小程序 appid
ak: stat_config.appid, // uni-app 应用 Appid
usv: STAT_VERSION, // 统计 sdk 版本
v: get_version(), // 应用版本,仅app
ch: get_channel(), // 渠道信息
......@@ -599,7 +654,6 @@ let statData = {
ct: '', // 城市
t: get_time(), // 上报数据时的时间戳
tt: '',
p: sys.platform === 'android' ? 'a' : 'i', // 手机系统
brand: sys.brand || '', // 手机品牌
md: sys.model, // 手机型号
sv: sys.system.replace(/(Android|iOS)\s/, ''), // 手机系统版本
......@@ -692,7 +746,8 @@ class Report {
}
_login() {
this.sendEventRequest({
this.sendEventRequest(
{
key: 'login',
},
0
......@@ -700,14 +755,20 @@ class Report {
}
_share() {
this.sendEventRequest({
this.sendEventRequest(
{
key: 'share',
}, 0);
},
0
);
}
_payment(key) {
this.sendEventRequest({
this.sendEventRequest(
{
key,
}, 0);
},
0
);
}
/**
......@@ -738,7 +799,7 @@ class Report {
* @param {Object} type
*/
applicationHide(self, type) {
if(!self){
if (!self) {
// 表示应用切换到后台 ,此时需要从页面栈获取页面实例
self = get_page_vm();
}
......@@ -748,7 +809,8 @@ class Report {
const time = get_residence_time();
const route = get_page_route(self);
uni.setStorageSync('_STAT_LAST_PAGE_ROUTE', route);
this.sendHideRequest({
this.sendHideRequest(
{
urlref: route,
urlref_ts: time.residenceTime,
},
......@@ -821,7 +883,6 @@ class Report {
}
}
/**
* 发送请求,应用维度上报
* @param {Object} options 页面信息
......@@ -833,12 +894,12 @@ class Report {
let query = is_opt ? '?' + JSON.stringify(options.query) : '';
Object.assign(this.statData, {
lt: '1',
url: (options.path + query) || '',
url: options.path + query || '',
t: get_time(),
sc: get_scene(options.scene),
fvts: get_first_visit_time(),
lvts: get_last_visit_time(),
tvc: get_total_visit_count()
tvc: get_total_visit_count(),
});
if (get_platform_name() === 'n') {
this.getProperty();
......@@ -852,15 +913,12 @@ class Report {
* @param {Object} opt
*/
sendPageRequest(opt) {
let {
url,
urlref,
urlref_ts
} = opt;
let { url, urlref, urlref_ts } = opt;
this._navigationBarTitle.lt = '11';
let options = {
ak: this.statData.ak,
uuid: this.statData.uuid,
// ak: this.statData.ak,
// uuid: this.statData.uuid,
// p: this.statData.p,
lt: '11',
ut: this.statData.ut,
url,
......@@ -870,7 +928,6 @@ class Report {
ch: this.statData.ch,
usv: this.statData.usv,
t: get_time(),
p: this.statData.p,
};
this.request(options);
}
......@@ -881,13 +938,11 @@ class Report {
* @param {Object} type
*/
sendHideRequest(opt, type) {
let {
urlref,
urlref_ts
} = opt;
let { urlref, urlref_ts } = opt;
let options = {
ak: this.statData.ak,
uuid: this.statData.uuid,
// ak: this.statData.ak,
// uuid: this.statData.uuid,
// p: this.statData.p,
lt: '3',
ut: this.statData.ut,
urlref,
......@@ -895,7 +950,6 @@ class Report {
ch: this.statData.ch,
usv: this.statData.usv,
t: get_time(),
p: this.statData.p,
};
this.request(options, type);
}
......@@ -903,17 +957,15 @@ class Report {
/**
* 自定义事件上报
*/
sendEventRequest({
key = '',
value = ''
} = {}) {
sendEventRequest({ key = '', value = '' } = {}) {
// const route = this._lastPageRoute
const routepath = get_route();
this._navigationBarTitle.config = get_page_name(routepath);
this._navigationBarTitle.lt = '21';
let options = {
ak: this.statData.ak,
uuid: this.statData.uuid,
// ak: this.statData.ak,
// uuid: this.statData.uuid,
// p: this.statData.p,
lt: '21',
ut: this.statData.ut,
url: routepath,
......@@ -922,7 +974,6 @@ class Report {
e_v: typeof value === 'object' ? JSON.stringify(value) : value.toString(),
usv: this.statData.usv,
t: get_time(),
p: this.statData.p,
};
this.request(options);
}
......@@ -987,7 +1038,7 @@ class Report {
Object.assign(data, {
ttn: title.page,
ttpj: title.config,
ttc: title.report
ttc: title.report,
});
let uniStatData = dbGet('__UNI__STAT__DATA') || {};
......@@ -999,6 +1050,10 @@ class Report {
dbSet('__UNI__STAT__DATA', uniStatData);
let page_residence_time = get_page_residence_time();
// debug 打印打点信息
if (is_debug) {
log(data);
}
// 判断时候到达上报时间 ,默认 10 秒上报
if (page_residence_time < OPERATING_TIME && !type) return
......@@ -1014,7 +1069,6 @@ class Report {
// 重置队列
dbRemove('__UNI__STAT__DATA');
{
if (data.ut === 'h5') {
this.imageRequest(optionsData);
......@@ -1033,7 +1087,7 @@ class Report {
this.sendRequest(optionsData);
}
getIsReportData(){
getIsReportData() {
return is_report_data()
}
......@@ -1043,16 +1097,25 @@ class Report {
*/
sendRequest(optionsData) {
{
this.getIsReportData().then(() => {
uni.request({
url: STAT_URL,
method: 'POST',
data: optionsData,
success: () => {},
success: () => {
if (is_debug) {
console.log(`=== 统计队列数据上报 ===`);
console.log(optionsData);
console.log(`=== 上报结束 ===`);
}
},
fail: (e) => {
if (++this._retry < 3) {
if (is_debug) {
console.warn('=== 统计上报错误,尝试重新上报!');
console.error(e);
}
setTimeout(() => {
this.sendRequest(optionsData);
}, 1000);
......@@ -1071,6 +1134,11 @@ class Report {
let image = new Image();
let options = get_sgin(get_encodeURIComponent_options(data)).options;
image.src = STAT_H5_URL + '?' + options;
if (is_debug) {
console.log(`=== 统计队列数据上报 ===`);
console.log(data);
console.log(`=== 上报结束 ===`);
}
});
}
......@@ -1082,7 +1150,8 @@ class Report {
this._navigationBarTitle.report = value;
return
}
this.sendEventRequest({
this.sendEventRequest(
{
key,
value: typeof value === 'object' ? JSON.stringify(value) : value,
},
......@@ -1096,6 +1165,7 @@ class Stat extends Report {
if (!uni.__stat_instance) {
uni.__stat_instance = new Stat();
}
return uni.__stat_instance
}
constructor() {
......@@ -1118,11 +1188,11 @@ class Stat extends Report {
this._query = options;
}
appHide(self){
appHide(self) {
this.applicationHide(self, true);
}
appShow(self){
appShow(self) {
this.applicationShow(self);
}
......@@ -1145,7 +1215,6 @@ class Stat extends Report {
this.appShow();
}
// #endif
}
hide(self) {
......@@ -1167,7 +1236,6 @@ class Stat extends Report {
this.appHide();
}
// #endif
}
error(em) {
......@@ -1175,7 +1243,7 @@ class Stat extends Report {
if (this._platform === 'devtools') {
if (process.env.NODE_ENV === 'development') {
console.info('当前运行环境为开发者工具,不上报数据。');
return;
return
}
}
let emVal = '';
......@@ -1185,8 +1253,9 @@ class Stat extends Report {
emVal = em.stack;
}
let options = {
ak: this.statData.ak,
uuid: this.statData.uuid,
// ak: this.statData.ak,
// uuid: this.statData.uuid,
// p: this.statData.p,
lt: '31',
ut: this.statData.ut,
ch: this.statData.ch,
......@@ -1196,7 +1265,6 @@ class Stat extends Report {
em: emVal,
usv: this.statData.usv,
t: parseInt(new Date().getTime() / 1000),
p: this.statData.p,
};
this.request(options);
}
......@@ -1218,7 +1286,7 @@ const lifecycle = {
// 重写分享,获取分享上报事件
if (this.$scope && this.$scope.onShareAppMessage) {
let oldShareAppMessage = this.$scope.onShareAppMessage;
this.$scope.onShareAppMessage = function(options) {
this.$scope.onShareAppMessage = function (options) {
stat.interceptShare(false);
return oldShareAppMessage.call(this, options)
};
......@@ -1241,21 +1309,15 @@ const lifecycle = {
},
onError(e) {
stat.error(e);
}
},
};
function main() {
{
console.log('uni统计开启,version:1');
}
if (process.env.NODE_ENV === 'development') {
uni.report = function(type, options) {};
} else {
// 加载统计代码
function load_stat() {
// #ifdef VUE3
uni.onCreateVueApp((app) => {
app.mixin(lifecycle);
uni.report = function(type, options) {
uni.report = function (type, options) {
stat.sendEvent(type, options);
};
});
......@@ -1271,12 +1333,29 @@ function main() {
// #endif
// #ifndef VUE3
const Vue = require('vue');
(Vue.default || Vue).mixin(lifecycle);
uni.report = function(type, options) {
// eslint-disable-next-line no-restricted-globals
const Vue = require('vue')
;(Vue.default || Vue).mixin(lifecycle);
uni.report = function (type, options) {
stat.sendEvent(type, options);
};
// #endif
}
function main() {
if (is_debug) {
{
// #ifndef APP-NVUE
console.log('=== uni统计开启,version:1.0');
// #endif
}
load_stat();
} else {
if (process.env.NODE_ENV === 'development') {
uni.report = function (type, options) {};
} else {
load_stat();
}
}
}
......
......@@ -13,16 +13,16 @@ const OPERATING_TIME = 10; // 数据上报时间 单位s
const DIFF_TIME = 60 * 1000 * 60 * 24;
let statConfig = {
appid: ''
appid: process.env.UNI_APP_ID,
};
let titleJsons = {};
let debug = !!process.env.UNI_STAT_DEBUG || false;
// #ifdef VUE3
statConfig.appid = process.env.UNI_APP_ID;
titleJsons = process.env.UNI_STAT_TITLE_JSON;
// #endif
// #ifndef VUE3
statConfig = require('uni-stat-config').default || require('uni-stat-config');
// eslint-disable-next-line no-restricted-globals
const pagesTitle = require('uni-pages?{"type":"style"}').default;
let pagesData = pagesTitle.pages;
for (let i in pagesData) {
......@@ -31,7 +31,7 @@ for (let i in pagesData) {
// MP
style.navigationBarTitleText ||
// ali
style.defaultTitle||
style.defaultTitle ||
// H5 || App
style.navigationBar?.titleText ||
'';
......@@ -41,47 +41,46 @@ for (let i in pagesData) {
}
// #endif
// TODO 在云函数中获取,暂时注释
// const UUID_KEY = '__DC_STAT_UUID'
// const UUID_VALUE = '__DC_UUID_VALUE'
// function getUuid() {
// let uuid = ''
// if (get_platform_name() === 'n') {
// try {
// uuid = plus.runtime.getDCloudId()
// } catch (e) {
// uuid = ''
// }
// return uuid
// }
// try {
// uuid = uni.getStorageSync(UUID_KEY)
// } catch (e) {
// uuid = UUID_VALUE
// }
// if (!uuid) {
// uuid = Date.now() + '' + Math.floor(Math.random() * 1e7)
// try {
// uni.setStorageSync(UUID_KEY, uuid)
// } catch (e) {
// uni.setStorageSync(UUID_KEY, UUID_VALUE)
// }
// }
// return uuid
// }
// export const get_uuid = (statData) => {
// // 有可能不存在 deviceId(一般不存在就是出bug了),就自己生成一个
// return sys.deviceId || getUuid()
// }
const UUID_KEY = '__DC_STAT_UUID';
const UUID_VALUE = '__DC_UUID_VALUE';
function getUuid() {
let uuid = '';
if (get_platform_name() === 'n') {
try {
uuid = plus.runtime.getDCloudId();
} catch (e) {
uuid = '';
}
return uuid
}
try {
uuid = uni.getStorageSync(UUID_KEY);
} catch (e) {
uuid = UUID_VALUE;
}
if (!uuid) {
uuid = Date.now() + '' + Math.floor(Math.random() * 1e7);
try {
uni.setStorageSync(UUID_KEY, uuid);
} catch (e) {
uni.setStorageSync(UUID_KEY, UUID_VALUE);
}
}
return uuid
}
/**
* 获取配置信息 如 appid
*/
const stat_config = statConfig;
const get_uuid = (statData) => {
// 有可能不存在 deviceId(一般不存在就是出bug了),就自己生成一个
return sys.deviceId || getUuid()
};
const get_sgin = (statData) => {
let arr = Object.keys(statData);
let sortArr = arr.sort();
......@@ -125,7 +124,7 @@ const get_platform_name = () => {
// 苹果审核代码中禁止出现 alipay 字样 ,需要特殊处理一下
const aliArr = ['y', 'a', 'p', 'mp-ali'];
const platformList = {
'app': 'n',
app: 'n',
'app-plus': 'n',
h5: 'h5',
'mp-weixin': 'wx',
......@@ -136,13 +135,13 @@ const get_platform_name = () => {
'quickapp-native': 'qn',
'mp-kuaishou': 'ks',
'mp-lark': 'lark',
'quickapp-webview':'qw'
'quickapp-webview': 'qw',
};
if(platformList[process.env.VUE_APP_PLATFORM] === 'ali'){
if(my&&my.env){
if (platformList[process.env.VUE_APP_PLATFORM] === 'ali') {
if (my && my.env) {
const clientName = my.env.clientName;
if(clientName === 'ap') return 'ali'
if(clientName === 'dingtalk') return 'dt'
if (clientName === 'ap') return 'ali'
if (clientName === 'dingtalk') return 'dt'
// TODO 缺少 ali 下的其他平台
}
}
......@@ -220,7 +219,11 @@ const get_route = (pageVm) => {
let scope_route = _self.$scope && _self.$scope.is;
return mp_route || scope_route || ''
} else {
return _self.route || (_self.$scope && _self.$scope.route) || (_self.$mp && _self.$mp.page.route)
return (
_self.route ||
(_self.$scope && _self.$scope.route) ||
(_self.$mp && _self.$mp.page.route)
)
}
};
......@@ -231,11 +234,11 @@ const get_page_route = (pageVm) => {
// 从 app 进入应用 ,没有 $page ,获取不到路由 ,需要获取页面 尝试从 getCurrentPages 获取也页面实例
// FIXME 尽量不使用 getCurrentPages ,大部分获取路由是从 onHide 获取 ,这时可以获取到,如果是 onload ,则可能获取不到,比如 百度
let page = pageVm.$page || (pageVm.$scope && pageVm.$scope.$page);
let page = pageVm && (pageVm.$page || (pageVm.$scope && pageVm.$scope.$page));
let lastPageRoute = uni.getStorageSync('_STAT_LAST_PAGE_ROUTE');
if (!page) return lastPageRoute || ''
// 如果找不到 fullPath 就取 route 的值
return page.fullPath === '/' ? page.route : (page.fullPath||page.route)
return page.fullPath === '/' ? page.route : page.fullPath || page.route
};
/**
......@@ -253,15 +256,23 @@ const get_page_vm = () => {
*/
const get_page_types = (self) => {
// XXX 百度有问题 ,获取的都是 componet ,等待修复
if (self.mpType === 'page' || self.$mpType === 'page' || (self.$mp && self.$mp.mpType === 'page') || self
.$options.mpType === 'page') {
return 'page';
}
if (self.mpType === 'app' || self.$mpType === 'app' || (self.$mp && self.$mp.mpType === 'app') || self.$options
.mpType === 'app') {
if (
self.mpType === 'page' ||
self.$mpType === 'page' ||
(self.$mp && self.$mp.mpType === 'page') ||
self.$options.mpType === 'page'
) {
return 'page'
}
if (
self.mpType === 'app' ||
self.$mpType === 'app' ||
(self.$mp && self.$mp.mpType === 'app') ||
self.$options.mpType === 'app'
) {
return 'app'
}
return null;
return null
};
/**
......@@ -291,7 +302,6 @@ const handle_data = (statData) => {
return JSON.stringify(firstArr)
};
/**
* 自定义事件参数校验
*/
......@@ -302,21 +312,29 @@ const calibration = (eventName, options) => {
return true
}
if (typeof eventName !== 'string') {
console.error(`uni.report [eventName] Parameter type error, it can only be of type String`);
console.error(
`uni.report [eventName] Parameter type error, it can only be of type String`
);
return true
}
if (eventName.length > 255) {
console.error(`uni.report [eventName] Parameter length cannot be greater than 255`);
console.error(
`uni.report [eventName] Parameter length cannot be greater than 255`
);
return true
}
if (typeof options !== 'string' && typeof options !== 'object') {
console.error('uni.report [options] Parameter type error, Only supports String or Object type');
console.error(
'uni.report [options] Parameter type error, Only supports String or Object type'
);
return true
}
if (typeof options === 'string' && options.length > 255) {
console.error(`uni.report [options] Parameter length cannot be greater than 255`);
console.error(
`uni.report [options] Parameter length cannot be greater than 255`
);
return true
}
......@@ -416,6 +434,42 @@ const requestData = (done) => {
});
};
/**
* 是否开启 debug 模式
*/
const is_debug = debug;
/**
* 日志输出
* @param {*} data
*/
const log = (data) => {
let msg_type = '';
switch (data.lt) {
case '1':
msg_type = '应用启动';
break
case '3':
msg_type = '应用进入后台';
break
case '11':
msg_type = '页面切换';
break
case '21':
msg_type = '事件触发';
break
case '31':
msg_type = '应用错误';
break
}
if (msg_type) {
console.log(`=== 统计数据采集:${msg_type} ===`);
console.log(data);
console.log(`=== 采集结束 ===`);
}
};
let data = uni.getStorageSync('$$STAT__DBDATA') || {};
const dbSet = (name, value) => {
if (!data) {
......@@ -585,10 +639,11 @@ const get_residence_time = (type) => {
// 统计数据默认值
let statData = {
uuid: get_uuid(), // 设备标识
// uuid: get_uuid(), // 设备标识
// ak: stat_config.appid, // uni-app 应用 Appid
// p: sys.platform === 'android' ? 'a' : 'i', // 手机系统
ut: get_platform_name(), // 平台类型
mpn: get_pack_name(), // 原生平台包名、小程序 appid
ak: stat_config.appid, // uni-app 应用 Appid
usv: STAT_VERSION, // 统计 sdk 版本
v: get_version(), // 应用版本,仅app
ch: get_channel(), // 渠道信息
......@@ -597,7 +652,6 @@ let statData = {
ct: '', // 城市
t: get_time(), // 上报数据时的时间戳
tt: '',
p: sys.platform === 'android' ? 'a' : 'i', // 手机系统
brand: sys.brand || '', // 手机品牌
md: sys.model, // 手机型号
sv: sys.system.replace(/(Android|iOS)\s/, ''), // 手机系统版本
......@@ -690,7 +744,8 @@ class Report {
}
_login() {
this.sendEventRequest({
this.sendEventRequest(
{
key: 'login',
},
0
......@@ -698,14 +753,20 @@ class Report {
}
_share() {
this.sendEventRequest({
this.sendEventRequest(
{
key: 'share',
}, 0);
},
0
);
}
_payment(key) {
this.sendEventRequest({
this.sendEventRequest(
{
key,
}, 0);
},
0
);
}
/**
......@@ -736,7 +797,7 @@ class Report {
* @param {Object} type
*/
applicationHide(self, type) {
if(!self){
if (!self) {
// 表示应用切换到后台 ,此时需要从页面栈获取页面实例
self = get_page_vm();
}
......@@ -746,7 +807,8 @@ class Report {
const time = get_residence_time();
const route = get_page_route(self);
uni.setStorageSync('_STAT_LAST_PAGE_ROUTE', route);
this.sendHideRequest({
this.sendHideRequest(
{
urlref: route,
urlref_ts: time.residenceTime,
},
......@@ -819,7 +881,6 @@ class Report {
}
}
/**
* 发送请求,应用维度上报
* @param {Object} options 页面信息
......@@ -831,12 +892,12 @@ class Report {
let query = is_opt ? '?' + JSON.stringify(options.query) : '';
Object.assign(this.statData, {
lt: '1',
url: (options.path + query) || '',
url: options.path + query || '',
t: get_time(),
sc: get_scene(options.scene),
fvts: get_first_visit_time(),
lvts: get_last_visit_time(),
tvc: get_total_visit_count()
tvc: get_total_visit_count(),
});
if (get_platform_name() === 'n') {
this.getProperty();
......@@ -850,15 +911,12 @@ class Report {
* @param {Object} opt
*/
sendPageRequest(opt) {
let {
url,
urlref,
urlref_ts
} = opt;
let { url, urlref, urlref_ts } = opt;
this._navigationBarTitle.lt = '11';
let options = {
ak: this.statData.ak,
uuid: this.statData.uuid,
// ak: this.statData.ak,
// uuid: this.statData.uuid,
// p: this.statData.p,
lt: '11',
ut: this.statData.ut,
url,
......@@ -868,7 +926,6 @@ class Report {
ch: this.statData.ch,
usv: this.statData.usv,
t: get_time(),
p: this.statData.p,
};
this.request(options);
}
......@@ -879,13 +936,11 @@ class Report {
* @param {Object} type
*/
sendHideRequest(opt, type) {
let {
urlref,
urlref_ts
} = opt;
let { urlref, urlref_ts } = opt;
let options = {
ak: this.statData.ak,
uuid: this.statData.uuid,
// ak: this.statData.ak,
// uuid: this.statData.uuid,
// p: this.statData.p,
lt: '3',
ut: this.statData.ut,
urlref,
......@@ -893,7 +948,6 @@ class Report {
ch: this.statData.ch,
usv: this.statData.usv,
t: get_time(),
p: this.statData.p,
};
this.request(options, type);
}
......@@ -901,17 +955,15 @@ class Report {
/**
* 自定义事件上报
*/
sendEventRequest({
key = '',
value = ''
} = {}) {
sendEventRequest({ key = '', value = '' } = {}) {
// const route = this._lastPageRoute
const routepath = get_route();
this._navigationBarTitle.config = get_page_name(routepath);
this._navigationBarTitle.lt = '21';
let options = {
ak: this.statData.ak,
uuid: this.statData.uuid,
// ak: this.statData.ak,
// uuid: this.statData.uuid,
// p: this.statData.p,
lt: '21',
ut: this.statData.ut,
url: routepath,
......@@ -920,7 +972,6 @@ class Report {
e_v: typeof value === 'object' ? JSON.stringify(value) : value.toString(),
usv: this.statData.usv,
t: get_time(),
p: this.statData.p,
};
this.request(options);
}
......@@ -985,7 +1036,7 @@ class Report {
Object.assign(data, {
ttn: title.page,
ttpj: title.config,
ttc: title.report
ttc: title.report,
});
let uniStatData = dbGet('__UNI__STAT__DATA') || {};
......@@ -997,6 +1048,10 @@ class Report {
dbSet('__UNI__STAT__DATA', uniStatData);
let page_residence_time = get_page_residence_time();
// debug 打印打点信息
if (is_debug) {
log(data);
}
// 判断时候到达上报时间 ,默认 10 秒上报
if (page_residence_time < OPERATING_TIME && !type) return
......@@ -1012,7 +1067,6 @@ class Report {
// 重置队列
dbRemove('__UNI__STAT__DATA');
{
if (data.ut === 'h5') {
this.imageRequest(optionsData);
......@@ -1031,7 +1085,7 @@ class Report {
this.sendRequest(optionsData);
}
getIsReportData(){
getIsReportData() {
return is_report_data()
}
......@@ -1041,16 +1095,25 @@ class Report {
*/
sendRequest(optionsData) {
{
this.getIsReportData().then(() => {
uni.request({
url: STAT_URL,
method: 'POST',
data: optionsData,
success: () => {},
success: () => {
if (is_debug) {
console.log(`=== 统计队列数据上报 ===`);
console.log(optionsData);
console.log(`=== 上报结束 ===`);
}
},
fail: (e) => {
if (++this._retry < 3) {
if (is_debug) {
console.warn('=== 统计上报错误,尝试重新上报!');
console.error(e);
}
setTimeout(() => {
this.sendRequest(optionsData);
}, 1000);
......@@ -1069,6 +1132,11 @@ class Report {
let image = new Image();
let options = get_sgin(get_encodeURIComponent_options(data)).options;
image.src = STAT_H5_URL + '?' + options;
if (is_debug) {
console.log(`=== 统计队列数据上报 ===`);
console.log(data);
console.log(`=== 上报结束 ===`);
}
});
}
......@@ -1080,7 +1148,8 @@ class Report {
this._navigationBarTitle.report = value;
return
}
this.sendEventRequest({
this.sendEventRequest(
{
key,
value: typeof value === 'object' ? JSON.stringify(value) : value,
},
......@@ -1094,6 +1163,7 @@ class Stat extends Report {
if (!uni.__stat_instance) {
uni.__stat_instance = new Stat();
}
return uni.__stat_instance
}
constructor() {
......@@ -1116,11 +1186,11 @@ class Stat extends Report {
this._query = options;
}
appHide(self){
appHide(self) {
this.applicationHide(self, true);
}
appShow(self){
appShow(self) {
this.applicationShow(self);
}
......@@ -1143,7 +1213,6 @@ class Stat extends Report {
this.appShow();
}
// #endif
}
hide(self) {
......@@ -1165,7 +1234,6 @@ class Stat extends Report {
this.appHide();
}
// #endif
}
error(em) {
......@@ -1173,7 +1241,7 @@ class Stat extends Report {
if (this._platform === 'devtools') {
if (process.env.NODE_ENV === 'development') {
console.info('当前运行环境为开发者工具,不上报数据。');
return;
return
}
}
let emVal = '';
......@@ -1183,8 +1251,9 @@ class Stat extends Report {
emVal = em.stack;
}
let options = {
ak: this.statData.ak,
uuid: this.statData.uuid,
// ak: this.statData.ak,
// uuid: this.statData.uuid,
// p: this.statData.p,
lt: '31',
ut: this.statData.ut,
ch: this.statData.ch,
......@@ -1194,7 +1263,6 @@ class Stat extends Report {
em: emVal,
usv: this.statData.usv,
t: parseInt(new Date().getTime() / 1000),
p: this.statData.p,
};
this.request(options);
}
......@@ -1216,7 +1284,7 @@ const lifecycle = {
// 重写分享,获取分享上报事件
if (this.$scope && this.$scope.onShareAppMessage) {
let oldShareAppMessage = this.$scope.onShareAppMessage;
this.$scope.onShareAppMessage = function(options) {
this.$scope.onShareAppMessage = function (options) {
stat.interceptShare(false);
return oldShareAppMessage.call(this, options)
};
......@@ -1239,21 +1307,15 @@ const lifecycle = {
},
onError(e) {
stat.error(e);
}
},
};
function main() {
{
console.log('uni统计开启,version:1');
}
if (process.env.NODE_ENV === 'development') {
uni.report = function(type, options) {};
} else {
// 加载统计代码
function load_stat() {
// #ifdef VUE3
uni.onCreateVueApp((app) => {
app.mixin(lifecycle);
uni.report = function(type, options) {
uni.report = function (type, options) {
stat.sendEvent(type, options);
};
});
......@@ -1269,12 +1331,29 @@ function main() {
// #endif
// #ifndef VUE3
const Vue = require('vue');
(Vue.default || Vue).mixin(lifecycle);
uni.report = function(type, options) {
// eslint-disable-next-line no-restricted-globals
const Vue = require('vue')
;(Vue.default || Vue).mixin(lifecycle);
uni.report = function (type, options) {
stat.sendEvent(type, options);
};
// #endif
}
function main() {
if (is_debug) {
{
// #ifndef APP-NVUE
console.log('=== uni统计开启,version:1.0');
// #endif
}
load_stat();
} else {
if (process.env.NODE_ENV === 'development') {
uni.report = function (type, options) {};
} else {
load_stat();
}
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册