Buried point function description

The scheme is applied to uniAPP small program mall project 1. Page browsing event is automatically processed by calling encapsulated jump method 2. Page parameters are automatically added (page URL, page parameters, trigger time, etc.) and platform public parameters are automatically added (user information, platform identity, etc.) 3. Click events and interface request events need to be added in your codeCopy the code

Route jump encapsulation method eventRoute

import eventAnalyse from "@/common/eventAnalyse.js";
const navigateTo = (url) => {
    const link = getCurrentPages()[getCurrentPages().length - 1].route;
    eventAnalyse.analyse('hide', {url: link})
    let _url = typeof url == 'object' ? url.url : url
    uni.navigateTo({
        url: _url,
        animationType: 'pop-in',
        animationDuration: 300,
        success: function(res) {
            eventAnalyse.analyse('show', {url: _url})
        }
    });
}
const redirectTo = (url) => {
    const link = getCurrentPages()[getCurrentPages().length - 1].route;
    eventAnalyse.analyse('hide', {url: link})
    let _url = typeof url == 'object' ? url.url : url
    uni.redirectTo({
        url: _url,
        animationType: 'pop-in',
        animationDuration: 300,
        success: function(res) {
            eventAnalyse.analyse('show', {url: _url})
        }
    });
}
const reLaunch = (url) => {
    const link = getCurrentPages()[getCurrentPages().length - 1].route;
    eventAnalyse.analyse('hide', {url: link})
    let _url = typeof url == 'object' ? url.url : url
    uni.reLaunch({
        url: _url,
        success: function(res) {
            eventAnalyse.analyse('show', {url: _url})
        }
    });
}
const switchTab = (url) => {
    const link = getCurrentPages()[getCurrentPages().length - 1].route;
    uni.switchTab({
        url: _url,
        success: function(res) {
        }
    });
}
const navigateBack = (num) => {
    const link = getCurrentPages()[getCurrentPages().length - 1].route;
    eventAnalyse.analyse('hide', {url: link})
    uni.navigateBack({
        delta: num
    });
}

export default { 
    navigateTo,
    redirectTo,
    reLaunch,
    switchTab,
    navigateBack
}
Copy the code

Event processing encapsulation method eventAnalyse

Import * as common from './common.js' // import common import * as db from './db.js' // import db import API from '@/apiConfig/api.js' import {pageCode} from './ pagecode.js' /** * @description route jump behavior analysis * @param {*} TriggerType Event type, Will pass the 'show' | | 'hide' | | 'quit' | | 'custom events' * @ param params event parameters will not pass * / const analyse = (triggerType = "", Params = {}) => {let startTime, leaveTime leaveTime, // leaveTime leaveTime stayTime = null, CityObj = db.get('cityObj'), userInfo = db.get('userInfo'); if (! triggerType) return switch (triggerType){ case 'show': const _url = params.url startTime = new Date().getTime() leaveTime = '' db.set('startTime',startTime) page_params = params.url ? getArgs(params.url) : {} url = getUrl() toUrl = _url.indexOf('? ')! = 1? _url.split('? ')[0] : _url break case 'hide': startTime = db.get('startTime') leaveTime = new Date().getTime() stayTime = leaveTime-startTime if (stayTime > 5*1000*60) { isQuit = true } url = getUrl() break case 'tabBarShow': startTime = new Date().getTime() leaveTime = '' db.set('startTime_tabBar',startTime) url = getUrl() break case 'tabBarHide': startTime = db.get('startTime_tabBar') leaveTime = new Date().getTime() stayTime = leaveTime-startTime if (stayTime > 5*1000*60) { isQuit = true } url = getUrl() break case 'appLaunch': startTime = new Date().getTime() leaveTime = '' db.set('startTime',startTime) url = getUrl() break case 'appShow': startTime = new Date().getTime() leaveTime = '' db.set('startTime',startTime) url = getUrl() break case 'appHide': startTime = db.get('startTime') leaveTime = new Date().getTime() stayTime = leaveTime-startTime if (stayTime > 5*1000*60) { isQuit = true } url = getUrl() break case 'quit': StartTime = db.get('startTime') leaveTime = new Date().getTime() URL = getUrl() // Indicates that the user has exited IsQuit = true // console.log(' jump out ') break case 'click': startTime = new Date().getTime() url = getUrl() break case 'response': startTime = new Date().getTime() url = getUrl() break } const tabBar = [ 'pages/index/index', 'pages/class/index', 'pages/welfare/index', 'pages/goods-car/goods-car', 'pages/ucenter/index] the if (triggerType = =' show ')} {return false report / / trigger tabBar page twice to prevent the if (triggerType = = 'hide' | | triggerType == 'appShow' || triggerType == 'appHide') { if (tabBar.indexOf(url) ! = 1) {return false}} / / client types let _client = the constParams. Platform the if (db) constParams) platform = = 'web' | | Db.constparams. platform == 'wxWeb ') {_client = 'h5'} const logData = {logVersion: "0.1.0", joinEvents: triggerType, client: _client, userId: userInfo.uuid ? userInfo.uuid : '', sessionId: db.get('sessionId'), deviceId: db.get('deviceId'), city: cityObj.cityCode, area: cityObj.towncode, startTime: common.timeToDate(startTime), stayTimeStr: stayTime ? common.msToDate(stayTime) : 0, stayTime: stayTime ? parseInt(stayTime/1000) : 0, endTime: leaveTime ? common.timeToDate(leaveTime) : common.timeToDate(startTime), isQuit: isQuit, pageUrl: url, pageId: pageCode[url], pageParams: page_params, ... params } let userLog = db.get('userLog') ? db.get('userLog') : [] userLog.push(logData) // if (logData.stayTime < 5*60) { // userLog.push(logData) // } db.set('userLog',userLog) if 5 * 60 (logData. StayTime > | | userLog. Length > 100) {the console. The log (' buried point data submission) API. AnalysisData (userLog). Then (() = > { db.del('userLog') }) } } function getArgs(urlArgs) { let params,url,result,reqDataString if (urlArgs.indexOf("?") ! = -1) { url = urlArgs.split("?" )[1] result = url.replace(/&/g, '","').replace(/=/g, '":"') reqDataString = '{"' + result + '"}' params = JSON.parse(reqDataString) return params } else { return params = {}  } } function getUrl() { const pages = getCurrentPages() const currentPage = pages[pages.length - 1] const url = currentPage ? currentPage.route : '' return url } export default { analyse }Copy the code

Global registration method

import eventRoute from "@/common/eventRoute.js";
import eventAnalyse from "@/common/eventAnalyse.js"
Vue.prototype.$eventRoute = eventRoute;
Vue.prototype.$eventAnalyse = eventAnalyse;
Copy the code

Page browsing events

this.$eventRoute.navigateTo ({
  url: '/pages/index/index'
});
Copy the code

Click events

$eventAnalyse. Analyse ('click', {page_id:'cart_btn',paramsA:'',paramsB:'});Copy the code

Interface request event

$eventAnalyse. Analyse (' Response ',{page_id:'cart_add',paramsA:',paramsB:');Copy the code

App. Vue listener background switch

onShow: function() {
    this.$eventAnalyse.analyse('appShow')
},
onHide: function() {
    this.$eventAnalyse.analyse('appHide')
}
Copy the code