UNPKG

send-event-tracking-fun

Version:

埋点方法

142 lines (141 loc) 4.92 kB
var __defProp = Object.defineProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __publicField = (obj, key, value) => { __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); return value; }; let getRandomValues; const rnds8 = new Uint8Array(16); function rng() { if (!getRandomValues) { getRandomValues = typeof crypto !== "undefined" && crypto.getRandomValues && crypto.getRandomValues.bind(crypto); if (!getRandomValues) { throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported"); } } return getRandomValues(rnds8); } const byteToHex = []; for (let i = 0; i < 256; ++i) { byteToHex.push((i + 256).toString(16).slice(1)); } function unsafeStringify(arr, offset = 0) { return byteToHex[arr[offset + 0]] + byteToHex[arr[offset + 1]] + byteToHex[arr[offset + 2]] + byteToHex[arr[offset + 3]] + "-" + byteToHex[arr[offset + 4]] + byteToHex[arr[offset + 5]] + "-" + byteToHex[arr[offset + 6]] + byteToHex[arr[offset + 7]] + "-" + byteToHex[arr[offset + 8]] + byteToHex[arr[offset + 9]] + "-" + byteToHex[arr[offset + 10]] + byteToHex[arr[offset + 11]] + byteToHex[arr[offset + 12]] + byteToHex[arr[offset + 13]] + byteToHex[arr[offset + 14]] + byteToHex[arr[offset + 15]]; } let _nodeId; let _clockseq; let _lastMSecs = 0; let _lastNSecs = 0; function v1(options, buf, offset) { let i = buf && offset || 0; const b = buf || new Array(16); options = options || {}; let node = options.node || _nodeId; let clockseq = options.clockseq !== void 0 ? options.clockseq : _clockseq; if (node == null || clockseq == null) { const seedBytes = options.random || (options.rng || rng)(); if (node == null) { node = _nodeId = [seedBytes[0] | 1, seedBytes[1], seedBytes[2], seedBytes[3], seedBytes[4], seedBytes[5]]; } if (clockseq == null) { clockseq = _clockseq = (seedBytes[6] << 8 | seedBytes[7]) & 16383; } } let msecs = options.msecs !== void 0 ? options.msecs : Date.now(); let nsecs = options.nsecs !== void 0 ? options.nsecs : _lastNSecs + 1; const dt = msecs - _lastMSecs + (nsecs - _lastNSecs) / 1e4; if (dt < 0 && options.clockseq === void 0) { clockseq = clockseq + 1 & 16383; } if ((dt < 0 || msecs > _lastMSecs) && options.nsecs === void 0) { nsecs = 0; } if (nsecs >= 1e4) { throw new Error("uuid.v1(): Can't create more than 10M uuids/sec"); } _lastMSecs = msecs; _lastNSecs = nsecs; _clockseq = clockseq; msecs += 122192928e5; const tl = ((msecs & 268435455) * 1e4 + nsecs) % 4294967296; b[i++] = tl >>> 24 & 255; b[i++] = tl >>> 16 & 255; b[i++] = tl >>> 8 & 255; b[i++] = tl & 255; const tmh = msecs / 4294967296 * 1e4 & 268435455; b[i++] = tmh >>> 8 & 255; b[i++] = tmh & 255; b[i++] = tmh >>> 24 & 15 | 16; b[i++] = tmh >>> 16 & 255; b[i++] = clockseq >>> 8 | 128; b[i++] = clockseq & 255; for (let n = 0; n < 6; ++n) { b[i + n] = node[n]; } return buf || unsafeStringify(b); } const DomainMap = { "test": "mta-t1", "pre": "mta-p1", "pro": "mta", "dev": "mta-d1" }; const getUrl = (host) => { let result = ""; if (host) { result = DomainMap[host]; } return host && !result ? host : `https://${result || DomainMap.test}.dingdanll.com/ddll-haotian/mta/data/save`; }; class ReqTrackingParamsType { constructor() { __publicField(this, "eventId", ""); __publicField(this, "nodeId"); __publicField(this, "event", "web_click"); __publicField(this, "userId", localStorage.getItem("userId")); __publicField(this, "tel", localStorage.getItem("phone")); __publicField(this, "ntwId"); __publicField(this, "url"); __publicField(this, "platform", 4); } } const requestEventTracking = (host, params) => { const { eventId } = params; const defaultParams = new ReqTrackingParamsType(); Object.assign(defaultParams, params); const localUuid = sessionStorage.getItem("uuid"); if (!defaultParams.userId && !localUuid) { const newUuid = BigInt(`0x${v1().replace(/-/g, "")}`).toString().slice(0, 16); sessionStorage.setItem("uuid", newUuid); defaultParams.userId = newUuid; } if (!defaultParams.userId && localUuid) { defaultParams.userId = localUuid; } if (!eventId) { console.error("\u4E8B\u4EF6Id\u4E0D\u80FD\u4E3A\u7A7A\uFF01"); return; } fetch(getUrl(host), { method: "post", body: JSON.stringify(defaultParams), headers: { "Content-Type": "application/json" } }); }; const sendEventTracking = { click(host, params) { requestEventTracking(host, { ...params, event: "web_click" }); }, view(host, params) { requestEventTracking(host, { ...params, event: "page_view" }); } }; export { sendEventTracking };