From 9229352e9bcff7a81d932518c01acf47fd3df7e7 Mon Sep 17 00:00:00 2001 From: zhoulingpo Date: Thu, 25 May 2023 14:14:48 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E4=BF=AE=E6=94=B9=E3=80=91=E8=B0=83?= =?UTF-8?q?=E6=95=B4=E4=B8=8B=E8=BD=BD=E4=BC=A0=E5=8F=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/env.js | 8 +++ src/utils/quickTracking.js | 134 +++++++++++++++++++++++++++++++++++++ 2 files changed, 142 insertions(+) create mode 100644 src/utils/env.js create mode 100644 src/utils/quickTracking.js diff --git a/src/utils/env.js b/src/utils/env.js new file mode 100644 index 0000000..68b0040 --- /dev/null +++ b/src/utils/env.js @@ -0,0 +1,8 @@ +const config = { + webId: '', + // QuickTracking + appKey: '54x8kny36dp66gbiqngsdnmu', + trackHost: 'api_ubtas.faw-vw.com', + sdkUrl: 'https://o.alicdn.com/QTSDK/quicktracking-sdk/qt_web.umd.js' +} +export default config diff --git a/src/utils/quickTracking.js b/src/utils/quickTracking.js new file mode 100644 index 0000000..879ad80 --- /dev/null +++ b/src/utils/quickTracking.js @@ -0,0 +1,134 @@ +import config from '@/utils/env' +import store from '@/store' + +console.log(config) +const { + appKey, + trackHost, + sdkUrl +} = config + +export const EventType = { + Exp: 'EXP', + Click: 'CLK', + Other: 'OTHER' +} + +export function init() { + if (!window.aplus_queue) { + initQueue() + loadQuickTrackSDK() + .then(() => { + setUserInfo() + }) + } +} + +export function sendTrackEvent({ + eventName, + pageName = eventName, + eventType = EventType.Click, + params + }) { + console.log('qt event', eventName, pageName, eventType, params) + window.aplus_queue.push({ + action: 'aplus.record', + arguments: [eventName, eventType, { + ...params, + page_name: pageName + }] + }) +} + +export function sendTrack({ + eventName, + pageName = eventName, + params + }) { + console.log('qt tracking', eventName, pageName, params) + window.aplus_queue.push({ + action: 'aplus.sendPV', + arguments: [{ + is_auto: false + }, { + // ts: Date.now(), + // path: eventName, // 当前page的url + page_name: pageName, + ...params + }] + }) +} + +function initQueue() { + window.aplus_queue = [] + // 手动 PV + window.aplus_queue.push({ + action: 'aplus.setMetaInfo', + arguments: ['aplus-waiting', 'MAN'] + }) + // 集成应用的appKey + window.aplus_queue.push({ + action: 'aplus.setMetaInfo', + arguments: ['appKey', appKey] + }) + // 如果是私有云部署还需要在上面那段JS后面紧接着添加日志域名埋点 + if (trackHost) { + window.aplus_queue.push({ + action: 'aplus.setMetaInfo', + arguments: ['aplus-rhost-v', trackHost] + }) + } + // 关闭全埋点 + window.aplus_queue.push({ + action: 'aplus.setMetaInfo', + arguments: ['aplus-autotrack-enabled', false] + }) + window.aplus_queue.push({ + action: 'aplus.setMetaInfo', + arguments: ['_hold', 'BLOCK'] + }) +} + +async function loadQuickTrackSDK() { + // qt的库回覆盖window.history的函数 pushState, replaceState 函数并导致IE11报错 + // 这里覆盖回去 + const nativePushState = window.history.pushState + const nativeReplaceState = window.history.replaceState + return new Promise((resolve) => { + const $scriptInDOM = document.getElementsByTagName('script')[0] + const $script = document.createElement('script') + $script.async = true + $script.id = 'beacon-aplus' + $script.src = sdkUrl + $script.onload = () => { + // qt库内部在body渲染之后延时51,这里延时200应该足以覆盖 + sleep(200).then(() => { + window.history.pushState = nativePushState + window.history.replaceState = nativeReplaceState + resolve() + }) + } + $scriptInDOM.parentNode.insertBefore($script, $scriptInDOM) + }) +} + +async function setUserInfo() { + const userInfo = store.getters.userInfo + if (userInfo) { + window.aplus_queue.push({ + action: 'aplus.setMetaInfo', + arguments: ['_user_id', userInfo.usid] + }) + } + window.aplus_queue.push({ + action: 'aplus.setMetaInfo', + arguments: ['_hold', 'START'] + }) +} + +async function sleep(ms) { + return new Promise((resolve) => { + setTimeout(resolve, ms) + }) +} +