|
| 1 | +import { reportNavigationClick } from "../lib/navigation.js"; |
| 2 | +import reportSearch from "../lib/search.js"; |
| 3 | +import { reportSidebarClick } from "../lib/sidebar.js"; |
| 4 | +import { reportTagsClick } from "../lib/tags.js"; |
| 5 | +import { reportTopicClick, reportTopicLeave } from "../lib/topic.js"; |
| 6 | + |
| 7 | +function isCookieAgreed() { |
| 8 | + const regexp = /\bagreed-cookiepolicy=([^;])+/; |
| 9 | + const res = document.cookie.match(regexp)?.[1]; |
| 10 | + return res === "1"; |
| 11 | +} |
| 12 | + |
| 13 | +export default { |
| 14 | + name: "alert", |
| 15 | + initialize() { |
| 16 | + import( |
| 17 | + "https://unpkg.com/@opensig/[email protected]/dist/open-analytics.mjs" |
| 18 | + ).then(({ OpenAnalytics, getClientInfo, OpenEventKeys }) => { |
| 19 | + const oa = new OpenAnalytics({ |
| 20 | + appKey: "openEuler", |
| 21 | + request: (data) => { |
| 22 | + if (!isCookieAgreed()) { |
| 23 | + disableOA(); |
| 24 | + return; |
| 25 | + } |
| 26 | + fetch("https://dsapi.test.osinfra.cn/query/track/openeuler", { |
| 27 | + body: JSON.stringify(data), |
| 28 | + method: "POST", |
| 29 | + headers: { "Content-Type": "application/json" }, |
| 30 | + }); |
| 31 | + }, |
| 32 | + }); |
| 33 | + |
| 34 | + /** |
| 35 | + * 开启埋点上报功能 |
| 36 | + * |
| 37 | + * 设置上报内容的header信息为浏览器相关信息 |
| 38 | + */ |
| 39 | + const enableOA = () => { |
| 40 | + oa.setHeader(getClientInfo()); |
| 41 | + oa.enableReporting(true); |
| 42 | + }; |
| 43 | + |
| 44 | + /** |
| 45 | + * 关闭埋点上报功能,清除localStorage中关于埋点的条目 |
| 46 | + */ |
| 47 | + const disableOA = () => { |
| 48 | + oa.enableReporting(false); |
| 49 | + [ |
| 50 | + "oa-openEuler-client", |
| 51 | + "oa-openEuler-events", |
| 52 | + "oa-openEuler-session", |
| 53 | + ].forEach((key) => { |
| 54 | + localStorage.removeItem(key); |
| 55 | + }); |
| 56 | + }; |
| 57 | + |
| 58 | + function oaReport(event, eventData, $service = "forum", options) { |
| 59 | + return oa.report( |
| 60 | + event, |
| 61 | + async (...opt) => { |
| 62 | + return { |
| 63 | + $service, |
| 64 | + ...(typeof eventData === "function" |
| 65 | + ? await eventData(...opt) |
| 66 | + : eventData), |
| 67 | + }; |
| 68 | + }, |
| 69 | + options |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * 上报PageView事件 |
| 75 | + * @param $referrer 从哪一个页面跳转过来 |
| 76 | + */ |
| 77 | + const reportPV = ($referrer) => { |
| 78 | + oaReport(OpenEventKeys.PV, ($referrer && { $referrer }) || null); |
| 79 | + }; |
| 80 | + |
| 81 | + /** |
| 82 | + * 上报性能指标 |
| 83 | + */ |
| 84 | + const reportPerformance = () => { |
| 85 | + oaReport(OpenEventKeys.LCP); |
| 86 | + oaReport(OpenEventKeys.INP); |
| 87 | + oaReport(OpenEventKeys.PageBasePerformance); |
| 88 | + }; |
| 89 | + |
| 90 | + function listenCookieSet() { |
| 91 | + if (isCookieAgreed()) { |
| 92 | + enableOA(); |
| 93 | + } |
| 94 | + const desc = Object.getOwnPropertyDescriptor( |
| 95 | + Document.prototype, |
| 96 | + "cookie" |
| 97 | + ); |
| 98 | + Object.defineProperty(Document.prototype, "cookie", { |
| 99 | + ...desc, |
| 100 | + set(val) { |
| 101 | + desc.set.call(this, val); |
| 102 | + if (isCookieAgreed()) { |
| 103 | + enableOA(); |
| 104 | + } else { |
| 105 | + disableOA(); |
| 106 | + } |
| 107 | + }, |
| 108 | + }); |
| 109 | + } |
| 110 | + |
| 111 | + function listenHistoryChange() { |
| 112 | + let referrer; |
| 113 | + |
| 114 | + ["replaceState", "pushState"].forEach((method) => { |
| 115 | + const native = History.prototype[method]; |
| 116 | + History.prototype[method] = function (...args) { |
| 117 | + try { |
| 118 | + if (oa.enabled) { |
| 119 | + const beforePath = location.pathname; |
| 120 | + native.call(this, ...args); |
| 121 | + const afterPath = location.pathname; |
| 122 | + if ( |
| 123 | + beforePath.startsWith("/t/topic/") && |
| 124 | + afterPath.startsWith("/t/topic/") && |
| 125 | + beforePath.split("/")[3] === afterPath.split("/")[3] |
| 126 | + ) { |
| 127 | + return; |
| 128 | + } |
| 129 | + if (beforePath !== afterPath) { |
| 130 | + reportPV(referrer); |
| 131 | + window.dispatchEvent( |
| 132 | + new CustomEvent("afterRouteChange", { |
| 133 | + detail: { from: beforePath, to: afterPath }, |
| 134 | + }) |
| 135 | + ); |
| 136 | + } |
| 137 | + } else { |
| 138 | + native.call(this, ...args); |
| 139 | + } |
| 140 | + } catch { |
| 141 | + native.call(this, ...args); |
| 142 | + } finally { |
| 143 | + referrer = location.href; |
| 144 | + } |
| 145 | + }; |
| 146 | + }); |
| 147 | + |
| 148 | + window.addEventListener("popstate", () => { |
| 149 | + try { |
| 150 | + const beforePath = new URL(referrer).pathname; |
| 151 | + if (beforePath !== location.pathname) { |
| 152 | + setTimeout(() => reportPV(referrer)); |
| 153 | + window.dispatchEvent( |
| 154 | + new CustomEvent("afterRouteChange", { |
| 155 | + detail: { from: beforePath, to: location.pathname }, |
| 156 | + }) |
| 157 | + ); |
| 158 | + } |
| 159 | + } finally { |
| 160 | + referrer = location.href; |
| 161 | + } |
| 162 | + }); |
| 163 | + } |
| 164 | + |
| 165 | + listenCookieSet(); |
| 166 | + listenHistoryChange(); |
| 167 | + reportPV(); |
| 168 | + reportPerformance(); |
| 169 | + |
| 170 | + window._oaReport = oaReport; |
| 171 | + window._enableOA = enableOA; |
| 172 | + window._disableOA = disableOA; |
| 173 | + |
| 174 | + reportSidebarClick(); |
| 175 | + reportNavigationClick(); |
| 176 | + reportTopicClick(); |
| 177 | + reportTopicLeave(); |
| 178 | + reportTagsClick(); |
| 179 | + |
| 180 | + reportSearch(); |
| 181 | + }); |
| 182 | + }, |
| 183 | +}; |
0 commit comments