feat:sfg
This commit is contained in:
118
utils/track.js
118
utils/track.js
@@ -1,23 +1,23 @@
|
||||
import { request } from "@/utils/request"
|
||||
import { request } from "@/utils/request";
|
||||
|
||||
// 生成并缓存 device_id
|
||||
function getOrCreateDeviceId() {
|
||||
let deviceId = uni.getStorageSync('device_id')
|
||||
let deviceId = uni.getStorageSync("device_id");
|
||||
if (!deviceId) {
|
||||
deviceId = Date.now() + '_' + Math.random().toString(36).substr(2, 9)
|
||||
uni.setStorageSync('device_id', deviceId)
|
||||
deviceId = Date.now() + "_" + Math.random().toString(36).substr(2, 9);
|
||||
uni.setStorageSync("device_id", deviceId);
|
||||
}
|
||||
return deviceId
|
||||
return deviceId;
|
||||
}
|
||||
|
||||
// 获取网络类型
|
||||
function getNetworkType() {
|
||||
return new Promise((resolve) => {
|
||||
uni.getNetworkType({
|
||||
success: (res) => resolve(res.networkType || 'unknown'),
|
||||
fail: () => resolve('unknown')
|
||||
})
|
||||
})
|
||||
success: (res) => resolve(res.networkType || "unknown"),
|
||||
fail: () => resolve("unknown"),
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// 获取系统平台
|
||||
@@ -25,67 +25,71 @@ function getSystemInfo() {
|
||||
return new Promise((resolve) => {
|
||||
uni.getSystemInfo({
|
||||
success: (res) => resolve(res),
|
||||
fail: () => resolve({})
|
||||
})
|
||||
})
|
||||
fail: () => resolve({}),
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// 获取当前页面路径
|
||||
function getCurrentPagePath() {
|
||||
const pages = getCurrentPages()
|
||||
const currentPage = pages[pages.length - 1]
|
||||
return currentPage?.route || ''
|
||||
const pages = getCurrentPages();
|
||||
const currentPage = pages[pages.length - 1];
|
||||
return currentPage?.route || "";
|
||||
}
|
||||
|
||||
function getMiniProgramVersion() {
|
||||
const platform = process.env.UNI_PLATFORM
|
||||
let version = ''
|
||||
let envVersion = ''
|
||||
const platform = process.env.UNI_PLATFORM;
|
||||
let version = "";
|
||||
let envVersion = "";
|
||||
|
||||
try {
|
||||
if (platform === 'mp-weixin') {
|
||||
if (platform === "mp-weixin") {
|
||||
// 微信小程序
|
||||
const info = wx.getAccountInfoSync()
|
||||
version = info?.miniProgram?.version || ''
|
||||
envVersion = info?.miniProgram?.envVersion || ''
|
||||
} else if (platform === 'mp-alipay') {
|
||||
const info = wx.getAccountInfoSync();
|
||||
version = info?.miniProgram?.version || "";
|
||||
envVersion = info?.miniProgram?.envVersion || "";
|
||||
} else if (platform === "mp-alipay") {
|
||||
// 支付宝小程序
|
||||
const info = my.getAppInfoSync()
|
||||
version = info?.version || ''
|
||||
} else if (platform === 'mp-toutiao' || platform === 'mp-jd') {
|
||||
const info = my.getAppInfoSync();
|
||||
version = info?.version || "";
|
||||
} else if (platform === "mp-toutiao" || platform === "mp-jd") {
|
||||
// 抖音/头条/京东小程序
|
||||
const info = tt.getAccountInfoSync?.()
|
||||
version = info?.miniProgram?.version || ''
|
||||
envVersion = info?.miniProgram?.envVersion || ''
|
||||
} else if (platform === 'mp-baidu') {
|
||||
const info = tt.getAccountInfoSync?.();
|
||||
version = info?.miniProgram?.version || "";
|
||||
envVersion = info?.miniProgram?.envVersion || "";
|
||||
} else if (platform === "mp-baidu") {
|
||||
// 百度小程序(无标准方法获取版本号)
|
||||
version = '' // 百度不支持获取版本号
|
||||
version = ""; // 百度不支持获取版本号
|
||||
} else {
|
||||
version = ''
|
||||
version = "";
|
||||
}
|
||||
} catch (e) {
|
||||
version = ''
|
||||
envVersion = ''
|
||||
version = "";
|
||||
envVersion = "";
|
||||
}
|
||||
|
||||
return {
|
||||
platform, // 当前小程序平台
|
||||
version, // 小程序版本号
|
||||
envVersion // develop / trial / release(微信等支持)
|
||||
}
|
||||
platform, // 当前小程序平台
|
||||
version, // 小程序版本号
|
||||
envVersion, // develop / trial / release(微信等支持)
|
||||
};
|
||||
}
|
||||
|
||||
// 构造埋点对象
|
||||
async function buildEventData(eventName, eventType = 'click', customParams = {}) {
|
||||
const deviceId = getOrCreateDeviceId()
|
||||
const systemInfo = await getSystemInfo()
|
||||
const networkType = await getNetworkType()
|
||||
async function buildEventData(
|
||||
eventName,
|
||||
eventType = "click",
|
||||
customParams = {}
|
||||
) {
|
||||
const deviceId = getOrCreateDeviceId();
|
||||
const systemInfo = await getSystemInfo();
|
||||
const networkType = await getNetworkType();
|
||||
// const location = await getLocation()
|
||||
const appVersion = (typeof plus !== 'undefined') ? plus?.runtime?.version : ''
|
||||
const { envVersion, platform, version} = getMiniProgramVersion()
|
||||
const appVersion = typeof plus !== "undefined" ? plus?.runtime?.version : "";
|
||||
const { envVersion, platform, version } = getMiniProgramVersion();
|
||||
|
||||
return {
|
||||
userId: uni.getStorageSync('user_id') || null,
|
||||
userId: uni.getStorageSync("user_id") || null,
|
||||
deviceId: deviceId,
|
||||
eventName: eventName,
|
||||
eventType: eventType,
|
||||
@@ -95,23 +99,23 @@ async function buildEventData(eventName, eventType = 'click', customParams = {})
|
||||
elementContent: customParams.element_content || null,
|
||||
customParams: customParams,
|
||||
networkType: networkType,
|
||||
os: systemInfo.platform || 'unknown',
|
||||
appVersion: version || 'unknown',
|
||||
envVersion: envVersion ?? '',
|
||||
platform: platform ?? ''
|
||||
}
|
||||
os: systemInfo.platform || "unknown",
|
||||
appVersion: version || "unknown",
|
||||
envVersion: envVersion || "",
|
||||
platform: platform || "",
|
||||
};
|
||||
}
|
||||
|
||||
// 发送埋点数据到服务器
|
||||
async function sendTrack(eventName, eventType = 'click', customParams = {}) {
|
||||
async function sendTrack(eventName, eventType = "click", customParams = {}) {
|
||||
const eventData = await buildEventData(eventName, eventType, customParams);
|
||||
request({
|
||||
url: '/api/common/tracking/create',
|
||||
method: 'POST',
|
||||
data: eventData
|
||||
})
|
||||
url: "/api/common/tracking/create",
|
||||
method: "POST",
|
||||
data: eventData,
|
||||
});
|
||||
}
|
||||
|
||||
export default {
|
||||
sendTrack
|
||||
}
|
||||
sendTrack,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user