polyv-rum-sdk 0.1.13 → 0.1.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -34,28 +34,44 @@ __export(src_exports, {
34
34
  RUMPluginVue2: () => RUMPluginVue2,
35
35
  RUM_ENV: () => RUM_ENV,
36
36
  SLSWebTrackingAdapter: () => SLSWebTrackingAdapter,
37
+ clearRUMUserReact: () => clearUser,
37
38
  createRUMPluginVue3: () => createRUMPluginVue3,
39
+ destroyRUMReact: () => destroyRUM2,
38
40
  destroyRUMVue2: () => destroyRUM,
41
+ disableRUMReact: () => disableRUM2,
39
42
  disableRUMVue2: () => disableRUM,
43
+ enableRUMReact: () => enableRUM2,
40
44
  enableRUMVue2: () => enableRUM,
45
+ getBreadcrumbsReact: () => getBreadcrumbs2,
41
46
  getBreadcrumbsVue2: () => getBreadcrumbs,
42
47
  getMitoAdapter: () => getMitoAdapter,
48
+ getRUMConfigReact: () => getRUMConfig2,
43
49
  getRUMConfigVue2: () => getRUMConfig,
44
50
  getRUMCoreInstance: () => getRUMCoreInstance,
45
51
  getRUMEnv: () => getRUMEnv,
52
+ getRUMManagerReact: () => getRUMManager2,
46
53
  getRUMManagerVue2: () => getRUMManager,
47
54
  getUserInfo: () => getUserInfo,
55
+ handleRouteChange: () => handleRouteChange,
48
56
  initRUMCore: () => initRUMCore,
57
+ initRUMSystemReact: () => initRUMSystem2,
49
58
  initRUMSystemVue2: () => initRUMSystem,
50
59
  mitoConfig: () => mitoConfig,
60
+ react: () => plugin_react_exports,
61
+ setRUMUserReact: () => setUser,
51
62
  shouldReport: () => shouldReport,
52
63
  slsConfig: () => slsConfig,
64
+ trackActionReact: () => trackAction2,
53
65
  trackActionVue2: () => trackAction,
66
+ trackEventReact: () => trackEvent2,
54
67
  trackEventVue2: () => trackEvent,
68
+ trackMetricReact: () => trackMetric2,
55
69
  trackMetricVue2: () => trackMetric,
70
+ trackPerformanceReact: () => trackPerformance2,
56
71
  trackPerformanceVue2: () => trackPerformance,
57
72
  transformDataForSLS: () => transformDataForSLS,
58
73
  useRUM: () => useRUM,
74
+ useRUMReact: () => useRUM2,
59
75
  vue2: () => plugin_vue2_exports,
60
76
  vue3: () => plugin_vue3_exports
61
77
  });
@@ -517,6 +533,8 @@ var SLSWebTrackingAdapter = class {
517
533
  this.addApiFields(logData, data);
518
534
  } else if (logData.event_type === "click") {
519
535
  this.addClickFields(logData, data);
536
+ } else if (logData.event_type === "custom") {
537
+ this.addCustomFields(logData, data);
520
538
  }
521
539
  try {
522
540
  let hash = "";
@@ -593,6 +611,56 @@ var SLSWebTrackingAdapter = class {
593
611
  logData.click_y = Number(data.y);
594
612
  }
595
613
  }
614
+ addCustomFields(logData, data) {
615
+ if (data.name) {
616
+ logData.custom_event_name = String(data.name);
617
+ }
618
+ if (data.action) {
619
+ logData.custom_event_action = String(data.action);
620
+ }
621
+ const customFields = {};
622
+ let fieldCount = 0;
623
+ const maxFields = 10;
624
+ const reservedKeys = [
625
+ "type",
626
+ "timestamp",
627
+ "name",
628
+ "action",
629
+ "eventType",
630
+ "category",
631
+ "level",
632
+ "userId",
633
+ "userName",
634
+ "userEmail",
635
+ "accountId",
636
+ "roles",
637
+ "pageTitle",
638
+ "path",
639
+ "search",
640
+ "hash",
641
+ "url",
642
+ "referrer",
643
+ "userAgent",
644
+ "rawData",
645
+ "dimensions"
646
+ ];
647
+ for (const [key, value] of Object.entries(data)) {
648
+ if (reservedKeys.includes(key))
649
+ continue;
650
+ if (fieldCount >= maxFields)
651
+ break;
652
+ if (value === null || value === void 0 || typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
653
+ customFields[key] = value;
654
+ fieldCount++;
655
+ }
656
+ }
657
+ if (Object.keys(customFields).length > 0) {
658
+ logData.custom_event_fields = JSON.stringify(customFields);
659
+ }
660
+ if (data.detail !== void 0) {
661
+ logData.custom_event_detail = String(data.detail);
662
+ }
663
+ }
596
664
  filterSensitiveData(data) {
597
665
  const sensitivePatterns = [
598
666
  /password/i,
@@ -824,9 +892,9 @@ var MitoSLSAdapter = class {
824
892
  debug: this.options.debug,
825
893
  maxBreadcrumbs: this.options.maxBreadcrumbs,
826
894
  Vue: context.Vue || ((_a = this.options.vue) == null ? void 0 : _a.Vue),
827
- framework: {
828
- vue: true
829
- },
895
+ // 根据 framework 配置决定是否设置 Vue 框架
896
+ // 如果未指定且没有 Vue 实例,则不设置 framework,使用原生 DOM 监听
897
+ ...this.options.framework === "vue" || this.options.framework === void 0 && context.Vue ? { framework: { vue: true } } : {},
830
898
  beforeDataReport: this.handleDataReport,
831
899
  silent: !this.options.debug,
832
900
  silentConsole: !this.options.debug,
@@ -1667,34 +1735,461 @@ function createRUMPluginVue3(options) {
1667
1735
  function useRUM() {
1668
1736
  return Vue.inject("rum");
1669
1737
  }
1738
+
1739
+ // src/react/plugin.react.ts
1740
+ var plugin_react_exports = {};
1741
+ __export(plugin_react_exports, {
1742
+ clearUser: () => clearUser,
1743
+ destroyRUM: () => destroyRUM2,
1744
+ disableRUM: () => disableRUM2,
1745
+ enableRUM: () => enableRUM2,
1746
+ getBreadcrumbs: () => getBreadcrumbs2,
1747
+ getRUMConfig: () => getRUMConfig2,
1748
+ getRUMManager: () => getRUMManager2,
1749
+ handleRouteChange: () => handleRouteChange,
1750
+ initRUMSystem: () => initRUMSystem2,
1751
+ setUser: () => setUser,
1752
+ trackAction: () => trackAction2,
1753
+ trackEvent: () => trackEvent2,
1754
+ trackMetric: () => trackMetric2,
1755
+ trackPerformance: () => trackPerformance2,
1756
+ useRUM: () => useRUM2
1757
+ });
1758
+
1759
+ // src/react/RUMManager.react.ts
1760
+ var RUMManagerReact = class {
1761
+ constructor() {
1762
+ this.adapter = null;
1763
+ this.isInitialized = false;
1764
+ this.isEnabled = true;
1765
+ this.context = {};
1766
+ this.previousLocation = null;
1767
+ }
1768
+ async init(context = {}, options = {}) {
1769
+ try {
1770
+ if (!this.shouldEnableRUM()) {
1771
+ if (mitoConfig.debug) {
1772
+ console.log("[RUM] RUM system is disabled");
1773
+ }
1774
+ return;
1775
+ }
1776
+ if (this.isInitialized) {
1777
+ if (mitoConfig.debug) {
1778
+ console.log("[RUM] RUM system already initialized");
1779
+ }
1780
+ return;
1781
+ }
1782
+ this.context = {
1783
+ store: context.store,
1784
+ router: context.router,
1785
+ ...context
1786
+ };
1787
+ const mergedOptions = {
1788
+ ...mitoConfig,
1789
+ ...options,
1790
+ // React 项目不设置 Vue 框架配置
1791
+ ...options.framework === "react" ? { framework: "react" } : {}
1792
+ };
1793
+ this.adapter = await initRUMCore(
1794
+ {
1795
+ store: this.context.store,
1796
+ router: this.context.router
1797
+ },
1798
+ mergedOptions
1799
+ );
1800
+ this.setupUserInteractionTracking();
1801
+ this.isInitialized = true;
1802
+ if (mitoConfig.debug) {
1803
+ console.log("[RUM] RUM system initialized successfully");
1804
+ }
1805
+ } catch (error) {
1806
+ console.error("[RUM] Failed to initialize RUM system:", error);
1807
+ }
1808
+ }
1809
+ shouldEnableRUM() {
1810
+ if (!this.isEnabled) {
1811
+ return false;
1812
+ }
1813
+ const env = getRUMEnv();
1814
+ const rumEnabled = env.VUE_APP_RUM_ENABLED || env.VITE_RUM_ENABLED;
1815
+ if (rumEnabled !== void 0) {
1816
+ return rumEnabled === "true";
1817
+ }
1818
+ const mode = env.MODE || env.NODE_ENV;
1819
+ const isProdEnv = mode === "prod" || mode === "production";
1820
+ return !isProdEnv;
1821
+ }
1822
+ /**
1823
+ * 设置 React Router 路由监听
1824
+ * 在路由变化时调用此方法
1825
+ */
1826
+ handleRouteChange(location) {
1827
+ if (!this.adapter || !this.previousLocation) {
1828
+ this.previousLocation = location;
1829
+ return;
1830
+ }
1831
+ if (location.pathname !== this.previousLocation.pathname) {
1832
+ if (typeof this.adapter.handleRouteChange === "function") {
1833
+ this.adapter.handleRouteChange(
1834
+ { path: location.pathname, search: location.search, hash: location.hash },
1835
+ { path: this.previousLocation.pathname, search: this.previousLocation.search, hash: this.previousLocation.hash }
1836
+ );
1837
+ }
1838
+ }
1839
+ this.previousLocation = location;
1840
+ }
1841
+ setupUserInteractionTracking() {
1842
+ if (typeof window === "undefined" || typeof document === "undefined") {
1843
+ return;
1844
+ }
1845
+ document.addEventListener(
1846
+ "click",
1847
+ (event) => {
1848
+ var _a;
1849
+ try {
1850
+ const originalTarget = event.target;
1851
+ const findClosest = (el, selector) => el && typeof el.closest === "function" ? el.closest(selector) : null;
1852
+ let target = findClosest(originalTarget, "[rum-id],[rum-name]");
1853
+ if (!target) {
1854
+ target = findClosest(
1855
+ originalTarget,
1856
+ 'button, a, [role="button"], [role="link"]'
1857
+ );
1858
+ }
1859
+ if (!target && originalTarget && window.getComputedStyle) {
1860
+ const style = window.getComputedStyle(originalTarget);
1861
+ if (style.cursor === "pointer") {
1862
+ target = originalTarget;
1863
+ }
1864
+ }
1865
+ if (!target) {
1866
+ return;
1867
+ }
1868
+ const getAttr = (el, name) => el && typeof el.getAttribute === "function" ? el.getAttribute(name) : null;
1869
+ let bizId = getAttr(target, "rum-id") || getAttr(target, "rum-name") || "";
1870
+ if (!bizId) {
1871
+ const tagName = target.tagName ? target.tagName.toLowerCase() : "element";
1872
+ const classList = (target.className || "").toString().split(/\s+/).filter((c) => c.trim());
1873
+ const primaryClass = classList[0] || "no-class";
1874
+ const getLabel = (el) => {
1875
+ if (!el)
1876
+ return "";
1877
+ const text = (el.textContent || "").trim();
1878
+ if (text)
1879
+ return text;
1880
+ const ariaLabel = getAttr(el, "aria-label");
1881
+ if (ariaLabel)
1882
+ return ariaLabel;
1883
+ const title = getAttr(el, "title");
1884
+ if (title)
1885
+ return title;
1886
+ const alt = getAttr(el, "alt");
1887
+ if (alt)
1888
+ return alt;
1889
+ const placeholder = getAttr(el, "placeholder");
1890
+ if (placeholder)
1891
+ return placeholder;
1892
+ const value = typeof el.value === "string" ? el.value.trim() : "";
1893
+ if (value)
1894
+ return value;
1895
+ return "";
1896
+ };
1897
+ const label = getLabel(target);
1898
+ const safeLabel = (label || primaryClass).replace(/\s+/g, "_").slice(0, 32) || "no_label";
1899
+ const routeKey = window.location.pathname || "unknown_route";
1900
+ let indexSuffix = "";
1901
+ try {
1902
+ const parent = target.parentNode;
1903
+ if (parent && parent.children && parent.children.length) {
1904
+ const siblings = Array.from(parent.children).filter(
1905
+ (el) => el.tagName === target.tagName && el.className === target.className
1906
+ );
1907
+ const index = siblings.indexOf(target);
1908
+ if (index >= 0) {
1909
+ indexSuffix = `[${index}]`;
1910
+ }
1911
+ }
1912
+ } catch {
1913
+ }
1914
+ bizId = `${routeKey}|${tagName}.${primaryClass}${indexSuffix}|${safeLabel}`;
1915
+ }
1916
+ const targetInfo = {
1917
+ tagName: target.tagName,
1918
+ className: target.className,
1919
+ id: target.id,
1920
+ textContent: ((_a = target.textContent) == null ? void 0 : _a.substring(0, 50)) || "",
1921
+ selector: this.getCSSSelector(target)
1922
+ };
1923
+ this.trackAction("click", {
1924
+ type: "click",
1925
+ bizId,
1926
+ x: event.clientX,
1927
+ y: event.clientY,
1928
+ target: targetInfo,
1929
+ page: {
1930
+ url: window.location.href,
1931
+ title: document.title,
1932
+ path: window.location.pathname
1933
+ },
1934
+ timestamp: Date.now()
1935
+ });
1936
+ } catch (error) {
1937
+ console.warn("[RUM] Failed to track click event:", error);
1938
+ }
1939
+ },
1940
+ true
1941
+ );
1942
+ if (mitoConfig.debug) {
1943
+ console.log("[RUM] User interaction tracking configured");
1944
+ }
1945
+ }
1946
+ getCSSSelector(element) {
1947
+ if (!element || element.nodeType !== Node.ELEMENT_NODE) {
1948
+ return "";
1949
+ }
1950
+ const path = [];
1951
+ let current = element;
1952
+ while (current && current.nodeType === Node.ELEMENT_NODE) {
1953
+ let selector = current.tagName.toLowerCase();
1954
+ if (current.id) {
1955
+ selector += `#${current.id}`;
1956
+ path.unshift(selector);
1957
+ break;
1958
+ }
1959
+ if (current.className) {
1960
+ const classes = current.className.split(" ").filter((c) => c.trim());
1961
+ if (classes.length > 0) {
1962
+ selector += `.${classes[0]}`;
1963
+ }
1964
+ }
1965
+ path.unshift(selector);
1966
+ current = current.parentElement;
1967
+ if (path.length > 5) {
1968
+ break;
1969
+ }
1970
+ }
1971
+ return path.join(" > ");
1972
+ }
1973
+ trackEvent(eventName, eventData = {}) {
1974
+ if (!this.checkRUMAvailable()) {
1975
+ return;
1976
+ }
1977
+ try {
1978
+ this.adapter.trackEvent({
1979
+ name: eventName,
1980
+ ...eventData
1981
+ });
1982
+ } catch (error) {
1983
+ console.error("[RUM] Failed to track event:", error);
1984
+ }
1985
+ }
1986
+ trackPerformance(performanceData) {
1987
+ if (!this.checkRUMAvailable()) {
1988
+ return;
1989
+ }
1990
+ try {
1991
+ this.adapter.trackEvent({
1992
+ name: "performance",
1993
+ type: "performance",
1994
+ ...performanceData
1995
+ });
1996
+ } catch (error) {
1997
+ console.error("[RUM] Failed to track performance:", error);
1998
+ }
1999
+ }
2000
+ trackAction(action, actionData = {}) {
2001
+ if (!this.checkRUMAvailable()) {
2002
+ return;
2003
+ }
2004
+ try {
2005
+ this.adapter.trackEvent({
2006
+ name: "user_action",
2007
+ action,
2008
+ ...actionData
2009
+ });
2010
+ } catch (error) {
2011
+ console.error("[RUM] Failed to track action:", error);
2012
+ }
2013
+ }
2014
+ trackMetric(metricName, value, dimensions = {}) {
2015
+ if (!this.checkRUMAvailable()) {
2016
+ return;
2017
+ }
2018
+ try {
2019
+ this.adapter.trackEvent({
2020
+ name: "metric",
2021
+ metricName,
2022
+ value,
2023
+ dimensions
2024
+ });
2025
+ } catch (error) {
2026
+ console.error("[RUM] Failed to track metric:", error);
2027
+ }
2028
+ }
2029
+ /**
2030
+ * 设置用户信息
2031
+ * 在用户登录后调用
2032
+ */
2033
+ setUser(userInfo) {
2034
+ if (!this.adapter) {
2035
+ return;
2036
+ }
2037
+ try {
2038
+ this.adapter.setUser(userInfo);
2039
+ if (mitoConfig.debug) {
2040
+ console.log("[RUM] User info set:", userInfo);
2041
+ }
2042
+ } catch (error) {
2043
+ console.error("[RUM] Failed to set user info:", error);
2044
+ }
2045
+ }
2046
+ /**
2047
+ * 清除用户信息
2048
+ * 在用户登出后调用
2049
+ */
2050
+ clearUser() {
2051
+ if (!this.adapter) {
2052
+ return;
2053
+ }
2054
+ try {
2055
+ this.adapter.setUser({});
2056
+ if (mitoConfig.debug) {
2057
+ console.log("[RUM] User info cleared");
2058
+ }
2059
+ } catch (error) {
2060
+ console.error("[RUM] Failed to clear user info:", error);
2061
+ }
2062
+ }
2063
+ getBreadcrumbs() {
2064
+ if (!this.checkRUMAvailable()) {
2065
+ return [];
2066
+ }
2067
+ try {
2068
+ return this.adapter.getBreadcrumbs();
2069
+ } catch (error) {
2070
+ console.error("[RUM] Failed to get breadcrumbs:", error);
2071
+ return [];
2072
+ }
2073
+ }
2074
+ enable() {
2075
+ this.isEnabled = true;
2076
+ if (mitoConfig.debug) {
2077
+ console.log("[RUM] RUM system enabled");
2078
+ }
2079
+ }
2080
+ disable() {
2081
+ this.isEnabled = false;
2082
+ if (mitoConfig.debug) {
2083
+ console.log("[RUM] RUM system disabled");
2084
+ }
2085
+ }
2086
+ checkRUMAvailable() {
2087
+ return this.isEnabled && this.isInitialized && !!this.adapter;
2088
+ }
2089
+ getConfig() {
2090
+ return {
2091
+ isInitialized: this.isInitialized,
2092
+ isEnabled: this.isEnabled,
2093
+ environment: mitoConfig.environment,
2094
+ debug: mitoConfig.debug,
2095
+ sls: {
2096
+ enabled: slsConfig.enabled,
2097
+ configured: true,
2098
+ project: slsConfig.project,
2099
+ logstore: slsConfig.logstore
2100
+ }
2101
+ };
2102
+ }
2103
+ destroy() {
2104
+ try {
2105
+ if (this.adapter) {
2106
+ this.adapter.destroy();
2107
+ this.adapter = null;
2108
+ }
2109
+ this.isInitialized = false;
2110
+ this.context = {};
2111
+ this.previousLocation = null;
2112
+ if (mitoConfig.debug) {
2113
+ console.log("[RUM] RUM system destroyed");
2114
+ }
2115
+ } catch (error) {
2116
+ console.error("[RUM] Failed to destroy RUM system:", error);
2117
+ }
2118
+ }
2119
+ };
2120
+
2121
+ // src/react/plugin.react.ts
2122
+ var rumManagerReact = new RUMManagerReact();
2123
+ var initRUMSystem2 = (context = {}, options = {}) => rumManagerReact.init(context, options);
2124
+ var handleRouteChange = (location) => rumManagerReact.handleRouteChange(location);
2125
+ var setUser = (userInfo) => rumManagerReact.setUser(userInfo);
2126
+ var clearUser = () => rumManagerReact.clearUser();
2127
+ var trackEvent2 = (eventName, eventData) => rumManagerReact.trackEvent(eventName, eventData || {});
2128
+ var trackPerformance2 = (performanceData) => rumManagerReact.trackPerformance(performanceData);
2129
+ var trackAction2 = (action, actionData) => rumManagerReact.trackAction(action, actionData || {});
2130
+ var trackMetric2 = (metricName, value, dimensions) => rumManagerReact.trackMetric(metricName, value, dimensions || {});
2131
+ var getBreadcrumbs2 = () => rumManagerReact.getBreadcrumbs();
2132
+ var enableRUM2 = () => rumManagerReact.enable();
2133
+ var disableRUM2 = () => rumManagerReact.disable();
2134
+ var getRUMConfig2 = () => rumManagerReact.getConfig();
2135
+ var destroyRUM2 = () => rumManagerReact.destroy();
2136
+ var getRUMManager2 = () => rumManagerReact;
2137
+ var useRUM2 = () => ({
2138
+ trackEvent: trackEvent2,
2139
+ trackPerformance: trackPerformance2,
2140
+ trackAction: trackAction2,
2141
+ trackMetric: trackMetric2,
2142
+ getBreadcrumbs: getBreadcrumbs2,
2143
+ setUser,
2144
+ clearUser,
2145
+ enable: enableRUM2,
2146
+ disable: disableRUM2,
2147
+ getConfig: getRUMConfig2
2148
+ });
1670
2149
  // Annotate the CommonJS export names for ESM import in node:
1671
2150
  0 && (module.exports = {
1672
2151
  MitoSLSAdapter,
1673
2152
  RUMPluginVue2,
1674
2153
  RUM_ENV,
1675
2154
  SLSWebTrackingAdapter,
2155
+ clearRUMUserReact,
1676
2156
  createRUMPluginVue3,
2157
+ destroyRUMReact,
1677
2158
  destroyRUMVue2,
2159
+ disableRUMReact,
1678
2160
  disableRUMVue2,
2161
+ enableRUMReact,
1679
2162
  enableRUMVue2,
2163
+ getBreadcrumbsReact,
1680
2164
  getBreadcrumbsVue2,
1681
2165
  getMitoAdapter,
2166
+ getRUMConfigReact,
1682
2167
  getRUMConfigVue2,
1683
2168
  getRUMCoreInstance,
1684
2169
  getRUMEnv,
2170
+ getRUMManagerReact,
1685
2171
  getRUMManagerVue2,
1686
2172
  getUserInfo,
2173
+ handleRouteChange,
1687
2174
  initRUMCore,
2175
+ initRUMSystemReact,
1688
2176
  initRUMSystemVue2,
1689
2177
  mitoConfig,
2178
+ react,
2179
+ setRUMUserReact,
1690
2180
  shouldReport,
1691
2181
  slsConfig,
2182
+ trackActionReact,
1692
2183
  trackActionVue2,
2184
+ trackEventReact,
1693
2185
  trackEventVue2,
2186
+ trackMetricReact,
1694
2187
  trackMetricVue2,
2188
+ trackPerformanceReact,
1695
2189
  trackPerformanceVue2,
1696
2190
  transformDataForSLS,
1697
2191
  useRUM,
2192
+ useRUMReact,
1698
2193
  vue2,
1699
2194
  vue3
1700
2195
  });