- admin:OrdersHub/UsersHub、Commerce/Ops/Enterprise Hub、MpAnalytics、Feishu/小程序配置面板、鉴权存储 - api:Analytics、DataMigration、FeishuLeadWebhook、mp 事件迁移 SQL - 微信/抖音小程序:analytics 上报与相关页面调整 - 开发文档与 scripts 补充 Made-with: Cursor
74 lines
1.5 KiB
JavaScript
74 lines
1.5 KiB
JavaScript
/**
|
|
* 抖音小程序埋点(同微信接口)
|
|
*/
|
|
const { request } = require('./request.js')
|
|
|
|
const MAX_BATCH = 30
|
|
const queue = []
|
|
let lastPageReport = { path: '', t: 0 }
|
|
|
|
function getAppSafe() {
|
|
try {
|
|
return getApp()
|
|
} catch (e) {
|
|
return null
|
|
}
|
|
}
|
|
|
|
function getCurrentRoute() {
|
|
const pages = getCurrentPages()
|
|
const p = pages[pages.length - 1]
|
|
return p && p.route ? p.route : ''
|
|
}
|
|
|
|
function track(eventName, props) {
|
|
if (!eventName || typeof eventName !== 'string') return
|
|
const app = getAppSafe()
|
|
const openId =
|
|
app && app.globalData
|
|
? (app.globalData.openId || (app.globalData.userInfo && (app.globalData.userInfo.openid || app.globalData.userInfo.openId)) || '')
|
|
: ''
|
|
queue.push({
|
|
event_name: eventName,
|
|
page_path: getCurrentRoute(),
|
|
props: props && typeof props === 'object' ? props : {},
|
|
client_ts: Date.now(),
|
|
openid: openId || undefined
|
|
})
|
|
if (queue.length >= MAX_BATCH) {
|
|
flush()
|
|
}
|
|
}
|
|
|
|
function reportPageView() {
|
|
const path = getCurrentRoute()
|
|
if (!path) return
|
|
const now = Date.now()
|
|
if (path === lastPageReport.path && now - lastPageReport.t < 2000) {
|
|
return
|
|
}
|
|
lastPageReport = { path, t: now }
|
|
track('page_view', { path })
|
|
}
|
|
|
|
function flush() {
|
|
if (queue.length === 0) return
|
|
const events = queue.splice(0, queue.length)
|
|
request({
|
|
url: '/api/analytics/events',
|
|
method: 'POST',
|
|
needAuth: true,
|
|
data: { events },
|
|
allow401: false,
|
|
success() {},
|
|
fail() {}
|
|
})
|
|
}
|
|
|
|
module.exports = {
|
|
track,
|
|
flush,
|
|
reportPageView,
|
|
getCurrentRoute
|
|
}
|