@rxdrag/website-lib-core 0.0.127 → 0.0.129
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/README.md +1 -1
- package/index.ts +1 -1
- package/package.json +4 -4
- package/src/astro/animation.ts +146 -146
- package/src/astro/background.ts +82 -86
- package/src/astro/base.ts +7 -0
- package/src/astro/business.ts +13 -0
- package/src/astro/grid/consts.ts +80 -80
- package/src/astro/grid/index.ts +2 -2
- package/src/astro/grid/types.ts +35 -35
- package/src/astro/image.ts +239 -239
- package/src/astro/index.ts +12 -10
- package/src/astro/link.ts +20 -21
- package/src/astro/media.ts +123 -123
- package/src/astro/nav.ts +13 -13
- package/src/astro/section/index.ts +7 -12
- package/src/component-logic/index.ts +1 -1
- package/src/component-logic/link-client.ts +32 -32
- package/src/component-logic/link.ts +61 -61
- package/src/design-tokens.ts +160 -160
- package/src/entify/Entify.ts +111 -101
- package/src/entify/IEntify.ts +157 -177
- package/src/entify/index.ts +4 -4
- package/src/entify/lib/collectCategoryIds.ts +20 -20
- package/src/entify/lib/fulltextSearch.ts +63 -62
- package/src/entify/lib/langFields.ts +14 -14
- package/src/entify/lib/listToTree.ts +23 -23
- package/src/entify/lib/newAvatarQueryOptions.ts +4 -4
- package/src/entify/lib/newOgImageQueryOptions.ts +5 -5
- package/src/entify/lib/newQueryPostOptions.ts +42 -45
- package/src/entify/lib/newQueryProductOptions.ts +96 -98
- package/src/entify/lib/newQueryProductsMediaOptions.ts +28 -28
- package/src/entify/lib/queryAllProducts.ts +40 -40
- package/src/entify/lib/queryBulletin.ts +28 -16
- package/src/entify/lib/queryEntityList.ts +41 -41
- package/src/entify/lib/queryFeaturedProducts.ts +69 -68
- package/src/entify/lib/queryLangs.ts +36 -60
- package/src/entify/lib/queryLatestPosts.ts +92 -91
- package/src/entify/lib/queryOneEntity.ts +63 -63
- package/src/entify/lib/queryOneMedia.ts +27 -27
- package/src/entify/lib/queryOnePostById.ts +21 -21
- package/src/entify/lib/queryOnePostBySlug.ts +40 -39
- package/src/entify/lib/queryOnePostCategoryBySlug.ts +35 -35
- package/src/entify/lib/queryOneProductById.ts +20 -20
- package/src/entify/lib/queryOneProductBySlug.ts +53 -52
- package/src/entify/lib/queryOneProductCategoryBySlug.ts +50 -49
- package/src/entify/lib/queryOneTheme.ts +54 -72
- package/src/entify/lib/queryOneUser.ts +38 -38
- package/src/entify/lib/queryPostCategories.ts +67 -67
- package/src/entify/lib/queryPostSlugs.ts +37 -37
- package/src/entify/lib/queryPosts.ts +175 -174
- package/src/entify/lib/queryProductCategories.ts +59 -59
- package/src/entify/lib/queryProducts.ts +145 -144
- package/src/entify/lib/queryProductsInMenu.ts +45 -45
- package/src/entify/lib/queryTagCategories.ts +58 -58
- package/src/entify/lib/queryTags.ts +57 -57
- package/src/entify/lib/queryUserIds.ts +24 -24
- package/src/entify/lib/queryUserPosts.ts +80 -79
- package/src/entify/lib/queryWebSiteSettings.ts +28 -28
- package/src/entify/lib/queryWebsite.ts +43 -43
- package/src/entify/lib/sendEmail.ts +7 -7
- package/src/entify/lib/toQueryOptions.ts +19 -19
- package/src/entify/lib/upsertEntity.ts +8 -8
- package/src/entify/types/index.ts +1 -1
- package/src/entify/types/utils.ts +11 -6
- package/src/entify/types/variables.ts +0 -1
- package/src/entify/view-model/funcs.ts +230 -230
- package/src/entify/view-model/index.ts +1 -1
- package/src/entify/view-model/models.ts +135 -135
- package/src/global.d.ts +7 -7
- package/src/index.ts +8 -8
- package/src/lib/formatDate.ts +15 -15
- package/src/lib/index.ts +3 -3
- package/src/lib/pagination.ts +114 -114
- package/src/lib/utils.ts +135 -135
- package/src/react/components/Analytics/eventHandlers.ts +173 -173
- package/src/react/components/Analytics/index.tsx +21 -21
- package/src/react/components/Analytics/singleton.ts +214 -214
- package/src/react/components/Analytics/tracking.ts +221 -221
- package/src/react/components/Analytics/types.ts +60 -60
- package/src/react/components/Analytics/utils.ts +95 -95
- package/src/react/components/AttachmentIcon/index.tsx +53 -53
- package/src/react/components/BackgroundHlsVideoPlayer.tsx +97 -97
- package/src/react/components/BackgroundVideoPlayer.tsx +32 -32
- package/src/react/components/Bulletin.tsx +30 -30
- package/src/react/components/ContactForm/ContactForm.tsx +289 -289
- package/src/react/components/ContactForm/Input.tsx +48 -48
- package/src/react/components/ContactForm/Input2.tsx +59 -59
- package/src/react/components/ContactForm/Submit.tsx +48 -48
- package/src/react/components/ContactForm/TelInput.tsx +215 -215
- package/src/react/components/ContactForm/Textarea.tsx +48 -48
- package/src/react/components/ContactForm/Textarea2.tsx +89 -89
- package/src/react/components/ContactForm/funcs.ts +64 -64
- package/src/react/components/ContactForm/index.ts +7 -7
- package/src/react/components/ContactForm/types.ts +68 -68
- package/src/react/components/GoogleConsent/CookieItemPanel.tsx +80 -80
- package/src/react/components/GoogleConsent/CumtomizedModal.tsx +148 -148
- package/src/react/components/GoogleConsent/GoogleConsent.tsx +100 -100
- package/src/react/components/GoogleConsent/gtags.ts +67 -67
- package/src/react/components/GoogleConsent/index.ts +2 -2
- package/src/react/components/GoogleConsent/types.ts +18 -18
- package/src/react/components/GoogleConsent//345/217/202/350/200/203.md +4 -4
- package/src/react/components/Icon/index.tsx +19 -19
- package/src/react/components/Medias/MainMedia.tsx +257 -257
- package/src/react/components/Medias/Thumbnail.tsx +62 -62
- package/src/react/components/Medias/VideoPlayer.tsx +114 -114
- package/src/react/components/Medias/index.tsx +271 -271
- package/src/react/components/ProductCard/ProductCard.tsx +24 -24
- package/src/react/components/ProductCard/ProductCta/index.tsx +28 -28
- package/src/react/components/ProductCard/ProductCta/style.css +3 -3
- package/src/react/components/ProductCard/ProductDescription/index.tsx +12 -12
- package/src/react/components/ProductCard/ProductDescription/style.css +5 -5
- package/src/react/components/ProductCard/ProductMedia/index.tsx +35 -35
- package/src/react/components/ProductCard/ProductMedia/style.css +5 -5
- package/src/react/components/ProductCard/ProductTitle/index.tsx +7 -7
- package/src/react/components/ProductCard/ProductTitle/style.css +3 -3
- package/src/react/components/ProductCard/ProductView.tsx +36 -36
- package/src/react/components/ProductCard/index.ts +4 -4
- package/src/react/components/ProductCard/useQueryProduct.ts +32 -32
- package/src/react/components/ReactModalTrigger.tsx +28 -28
- package/src/react/components/ReactVideoPlayer.tsx +29 -52
- package/src/react/components/RichTextOutline/index.tsx +75 -75
- package/src/react/components/RichTextOutline/useAnchorScroll.ts +23 -23
- package/src/react/components/Scroller.tsx +39 -39
- package/src/react/components/SearchInput.tsx +21 -21
- package/src/react/components/Share/index.tsx +86 -86
- package/src/react/components/Share/socials.tsx +79 -77
- package/src/react/components/Share//350/265/204/346/226/231.md +7 -7
- package/src/react/components/ToTop.tsx +72 -72
- package/src/react/components/VideoPlayIcon.tsx +43 -0
- package/src/react/components/all.ts +38 -38
- package/src/react/components/index.ts +16 -16
- package/src/robots.ts +4 -4
- package/src/entify/lib/newPageMetaOptions.ts +0 -18
- package/src/entify/lib/newQueryPageOptions.ts +0 -14
- package/src/entify/lib/queryOneIcon.ts +0 -27
- package/src/entify/lib/queryPageBySlug.ts +0 -43
- package/src/entify/lib/queryPageByType.ts +0 -44
|
@@ -1,221 +1,221 @@
|
|
|
1
|
-
// Analytics 跟踪相关函数
|
|
2
|
-
import type { AnalyticsState, PageData, TrackingData } from './types';
|
|
3
|
-
import {
|
|
4
|
-
getDeviceInfo,
|
|
5
|
-
getPlatform,
|
|
6
|
-
getBrowserLanguage,
|
|
7
|
-
detectPageType,
|
|
8
|
-
getCookie
|
|
9
|
-
} from './utils';
|
|
10
|
-
|
|
11
|
-
// 更新活跃时间
|
|
12
|
-
export function updateActiveTime(state: AnalyticsState): void {
|
|
13
|
-
if (state.isPageVisible) {
|
|
14
|
-
const now = Date.now();
|
|
15
|
-
state.activeTime += now - state.lastActiveTime;
|
|
16
|
-
state.lastActiveTime = now;
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
// 重置页面跟踪(用于页面切换)
|
|
21
|
-
export function resetPageTracking(state: AnalyticsState, pageData: PageData): void {
|
|
22
|
-
const newUrl = window.location.href;
|
|
23
|
-
// 只有当 URL 真正改变时才重置
|
|
24
|
-
if (newUrl !== state.currentPageUrl) {
|
|
25
|
-
state.currentPageUrl = newUrl;
|
|
26
|
-
pageData.start_time = Date.now();
|
|
27
|
-
state.pageLoadTime = Date.now();
|
|
28
|
-
state.isPageTracked = false;
|
|
29
|
-
// 重置活跃时间追踪
|
|
30
|
-
state.activeTime = 0;
|
|
31
|
-
state.lastActiveTime = Date.now();
|
|
32
|
-
state.isPageVisible = !document.hidden;
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
// 检查是否为真正的页面访问(简化版)
|
|
37
|
-
export function isRealPageVisit(state: AnalyticsState): boolean {
|
|
38
|
-
const now = Date.now();
|
|
39
|
-
|
|
40
|
-
// 如果页面已经被跟踪过,则不是新访问
|
|
41
|
-
if (state.isPageTracked) {
|
|
42
|
-
return false;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
// 检查是否在短时间内重复跟踪(全局防重复)
|
|
46
|
-
if (state.lastTrackTime > 0 && now - state.lastTrackTime < 1000) {
|
|
47
|
-
return false;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
// 检查页面是否可见(避免在后台标签页中统计)
|
|
51
|
-
if (document.hidden || document.visibilityState === "hidden") {
|
|
52
|
-
return false;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
return true;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
// 处理队列中的下一个请求
|
|
59
|
-
export function processQueue(state: AnalyticsState, apiEndpoint: string): void {
|
|
60
|
-
state.isRequestPending = false;
|
|
61
|
-
|
|
62
|
-
if (state.requestQueue.length > 0) {
|
|
63
|
-
const { data, eventType } = state.requestQueue.shift()!;
|
|
64
|
-
// 小延迟避免请求过于频繁
|
|
65
|
-
setTimeout(() => {
|
|
66
|
-
sendImmediately(data, eventType, state, apiEndpoint);
|
|
67
|
-
}, 100);
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
// 立即发送数据
|
|
72
|
-
export function sendImmediately(
|
|
73
|
-
data: TrackingData,
|
|
74
|
-
eventType: string,
|
|
75
|
-
state: AnalyticsState,
|
|
76
|
-
apiEndpoint: string
|
|
77
|
-
): void {
|
|
78
|
-
state.isRequestPending = true;
|
|
79
|
-
|
|
80
|
-
fetch(apiEndpoint, {
|
|
81
|
-
method: "POST",
|
|
82
|
-
headers: {
|
|
83
|
-
"Content-Type": "application/json",
|
|
84
|
-
"X-Requested-With": "XMLHttpRequest",
|
|
85
|
-
},
|
|
86
|
-
body: JSON.stringify(data),
|
|
87
|
-
keepalive: true,
|
|
88
|
-
})
|
|
89
|
-
.then(() => {
|
|
90
|
-
if (eventType === "pageView") {
|
|
91
|
-
state.isPageTracked = true;
|
|
92
|
-
state.lastTrackTime = Date.now();
|
|
93
|
-
|
|
94
|
-
// 更新全局状态(用于跨组件实例共享)
|
|
95
|
-
if (typeof window !== 'undefined') {
|
|
96
|
-
(window as any).__analyticsGlobalState = {
|
|
97
|
-
lastPageUrl: window.location.href,
|
|
98
|
-
lastTrackTime: Date.now(),
|
|
99
|
-
};
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
// 处理队列中的下一个请求
|
|
104
|
-
processQueue(state, apiEndpoint);
|
|
105
|
-
})
|
|
106
|
-
.catch(() => {
|
|
107
|
-
processQueue(state, apiEndpoint);
|
|
108
|
-
});
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
// 优化的网络请求函数
|
|
112
|
-
export function sendTrackingData(
|
|
113
|
-
data: TrackingData,
|
|
114
|
-
eventType: string,
|
|
115
|
-
state: AnalyticsState,
|
|
116
|
-
apiEndpoint: string
|
|
117
|
-
): void {
|
|
118
|
-
// 对于 pageLeave 事件,立即发送(用户可能马上离开)
|
|
119
|
-
if (eventType === "pageLeave") {
|
|
120
|
-
sendImmediately(data, eventType, state, apiEndpoint);
|
|
121
|
-
return;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
// 对于 pageView 事件,可以稍微延迟或批量处理
|
|
125
|
-
if (state.isRequestPending) {
|
|
126
|
-
// 如果有请求正在进行,加入队列
|
|
127
|
-
state.requestQueue.push({ data, eventType });
|
|
128
|
-
return;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
sendImmediately(data, eventType, state, apiEndpoint);
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
// 创建跟踪数据
|
|
135
|
-
export function createTrackingData(
|
|
136
|
-
pageData: PageData,
|
|
137
|
-
state: AnalyticsState,
|
|
138
|
-
eventType: string,
|
|
139
|
-
effectiveDuration: number
|
|
140
|
-
): TrackingData {
|
|
141
|
-
return {
|
|
142
|
-
visitor_id: pageData.visitor_id,
|
|
143
|
-
session_id: pageData.session_id,
|
|
144
|
-
url: window.location.href,
|
|
145
|
-
path: window.location.pathname,
|
|
146
|
-
referrer: state.currentReferrer,
|
|
147
|
-
user_agent: pageData.user_agent,
|
|
148
|
-
duration_ms: effectiveDuration, // 使用活跃时间
|
|
149
|
-
timestamp: Date.now(),
|
|
150
|
-
type: eventType, // 添加事件类型
|
|
151
|
-
// 设备和平台信息
|
|
152
|
-
device: getDeviceInfo(),
|
|
153
|
-
platform: getPlatform(),
|
|
154
|
-
browser_language: getBrowserLanguage(),
|
|
155
|
-
// 页面信息
|
|
156
|
-
page_title: document.title || null,
|
|
157
|
-
// 屏幕信息
|
|
158
|
-
screen_width: screen.width,
|
|
159
|
-
screen_height: screen.height,
|
|
160
|
-
viewport_width: window.innerWidth,
|
|
161
|
-
viewport_height: window.innerHeight,
|
|
162
|
-
// 时区信息
|
|
163
|
-
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
|
|
164
|
-
// 是否新用户(简单判断:第一次访问该域名)
|
|
165
|
-
is_new_user:
|
|
166
|
-
!getCookie("visitor_id") ||
|
|
167
|
-
getCookie("visitor_id") === pageData.visitor_id,
|
|
168
|
-
};
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
// 发送访客数据(支持不同类型)
|
|
172
|
-
export function trackPageEvent(
|
|
173
|
-
eventType: string,
|
|
174
|
-
pageData: PageData,
|
|
175
|
-
state: AnalyticsState,
|
|
176
|
-
apiEndpoint: string,
|
|
177
|
-
debugLog: (...args: any[]) => void
|
|
178
|
-
): void {
|
|
179
|
-
// 对于 pageView 事件,检测页面类型
|
|
180
|
-
if (eventType === "pageView") {
|
|
181
|
-
eventType = detectPageType(eventType);
|
|
182
|
-
debugLog("Detected page type:", eventType);
|
|
183
|
-
|
|
184
|
-
// 检查是否为真正的页面访问
|
|
185
|
-
if (!isRealPageVisit(state)) {
|
|
186
|
-
return;
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
// 计算活跃时间
|
|
191
|
-
let effectiveDuration = 0;
|
|
192
|
-
if (eventType === "pageLeave") {
|
|
193
|
-
updateActiveTime(state); // 更新到当前时间
|
|
194
|
-
effectiveDuration = state.activeTime;
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
debugLog("Using referrer:", state.currentReferrer, "for event:", eventType);
|
|
198
|
-
|
|
199
|
-
const data = createTrackingData(pageData, state, eventType, effectiveDuration);
|
|
200
|
-
|
|
201
|
-
// 优化的网络请求处理
|
|
202
|
-
sendTrackingData(data, eventType, state, apiEndpoint);
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
// 兼容性函数,保持向后兼容
|
|
206
|
-
export function trackPageVisit(
|
|
207
|
-
pageData: PageData,
|
|
208
|
-
state: AnalyticsState,
|
|
209
|
-
apiEndpoint: string,
|
|
210
|
-
debugLog: (...args: any[]) => void
|
|
211
|
-
): void {
|
|
212
|
-
// 检查是否在短时间内(200ms)重复调用同一个 URL 的 pageView
|
|
213
|
-
const currentUrl = window.location.href;
|
|
214
|
-
const now = Date.now();
|
|
215
|
-
|
|
216
|
-
if (state.isPageTracked && state.currentPageUrl === currentUrl && now - state.lastTrackTime < 200) {
|
|
217
|
-
return;
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
trackPageEvent("pageView", pageData, state, apiEndpoint, debugLog);
|
|
221
|
-
}
|
|
1
|
+
// Analytics 跟踪相关函数
|
|
2
|
+
import type { AnalyticsState, PageData, TrackingData } from './types';
|
|
3
|
+
import {
|
|
4
|
+
getDeviceInfo,
|
|
5
|
+
getPlatform,
|
|
6
|
+
getBrowserLanguage,
|
|
7
|
+
detectPageType,
|
|
8
|
+
getCookie
|
|
9
|
+
} from './utils';
|
|
10
|
+
|
|
11
|
+
// 更新活跃时间
|
|
12
|
+
export function updateActiveTime(state: AnalyticsState): void {
|
|
13
|
+
if (state.isPageVisible) {
|
|
14
|
+
const now = Date.now();
|
|
15
|
+
state.activeTime += now - state.lastActiveTime;
|
|
16
|
+
state.lastActiveTime = now;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// 重置页面跟踪(用于页面切换)
|
|
21
|
+
export function resetPageTracking(state: AnalyticsState, pageData: PageData): void {
|
|
22
|
+
const newUrl = window.location.href;
|
|
23
|
+
// 只有当 URL 真正改变时才重置
|
|
24
|
+
if (newUrl !== state.currentPageUrl) {
|
|
25
|
+
state.currentPageUrl = newUrl;
|
|
26
|
+
pageData.start_time = Date.now();
|
|
27
|
+
state.pageLoadTime = Date.now();
|
|
28
|
+
state.isPageTracked = false;
|
|
29
|
+
// 重置活跃时间追踪
|
|
30
|
+
state.activeTime = 0;
|
|
31
|
+
state.lastActiveTime = Date.now();
|
|
32
|
+
state.isPageVisible = !document.hidden;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// 检查是否为真正的页面访问(简化版)
|
|
37
|
+
export function isRealPageVisit(state: AnalyticsState): boolean {
|
|
38
|
+
const now = Date.now();
|
|
39
|
+
|
|
40
|
+
// 如果页面已经被跟踪过,则不是新访问
|
|
41
|
+
if (state.isPageTracked) {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// 检查是否在短时间内重复跟踪(全局防重复)
|
|
46
|
+
if (state.lastTrackTime > 0 && now - state.lastTrackTime < 1000) {
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// 检查页面是否可见(避免在后台标签页中统计)
|
|
51
|
+
if (document.hidden || document.visibilityState === "hidden") {
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// 处理队列中的下一个请求
|
|
59
|
+
export function processQueue(state: AnalyticsState, apiEndpoint: string): void {
|
|
60
|
+
state.isRequestPending = false;
|
|
61
|
+
|
|
62
|
+
if (state.requestQueue.length > 0) {
|
|
63
|
+
const { data, eventType } = state.requestQueue.shift()!;
|
|
64
|
+
// 小延迟避免请求过于频繁
|
|
65
|
+
setTimeout(() => {
|
|
66
|
+
sendImmediately(data, eventType, state, apiEndpoint);
|
|
67
|
+
}, 100);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// 立即发送数据
|
|
72
|
+
export function sendImmediately(
|
|
73
|
+
data: TrackingData,
|
|
74
|
+
eventType: string,
|
|
75
|
+
state: AnalyticsState,
|
|
76
|
+
apiEndpoint: string
|
|
77
|
+
): void {
|
|
78
|
+
state.isRequestPending = true;
|
|
79
|
+
|
|
80
|
+
fetch(apiEndpoint, {
|
|
81
|
+
method: "POST",
|
|
82
|
+
headers: {
|
|
83
|
+
"Content-Type": "application/json",
|
|
84
|
+
"X-Requested-With": "XMLHttpRequest",
|
|
85
|
+
},
|
|
86
|
+
body: JSON.stringify(data),
|
|
87
|
+
keepalive: true,
|
|
88
|
+
})
|
|
89
|
+
.then(() => {
|
|
90
|
+
if (eventType === "pageView") {
|
|
91
|
+
state.isPageTracked = true;
|
|
92
|
+
state.lastTrackTime = Date.now();
|
|
93
|
+
|
|
94
|
+
// 更新全局状态(用于跨组件实例共享)
|
|
95
|
+
if (typeof window !== 'undefined') {
|
|
96
|
+
(window as any).__analyticsGlobalState = {
|
|
97
|
+
lastPageUrl: window.location.href,
|
|
98
|
+
lastTrackTime: Date.now(),
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// 处理队列中的下一个请求
|
|
104
|
+
processQueue(state, apiEndpoint);
|
|
105
|
+
})
|
|
106
|
+
.catch(() => {
|
|
107
|
+
processQueue(state, apiEndpoint);
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// 优化的网络请求函数
|
|
112
|
+
export function sendTrackingData(
|
|
113
|
+
data: TrackingData,
|
|
114
|
+
eventType: string,
|
|
115
|
+
state: AnalyticsState,
|
|
116
|
+
apiEndpoint: string
|
|
117
|
+
): void {
|
|
118
|
+
// 对于 pageLeave 事件,立即发送(用户可能马上离开)
|
|
119
|
+
if (eventType === "pageLeave") {
|
|
120
|
+
sendImmediately(data, eventType, state, apiEndpoint);
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// 对于 pageView 事件,可以稍微延迟或批量处理
|
|
125
|
+
if (state.isRequestPending) {
|
|
126
|
+
// 如果有请求正在进行,加入队列
|
|
127
|
+
state.requestQueue.push({ data, eventType });
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
sendImmediately(data, eventType, state, apiEndpoint);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// 创建跟踪数据
|
|
135
|
+
export function createTrackingData(
|
|
136
|
+
pageData: PageData,
|
|
137
|
+
state: AnalyticsState,
|
|
138
|
+
eventType: string,
|
|
139
|
+
effectiveDuration: number
|
|
140
|
+
): TrackingData {
|
|
141
|
+
return {
|
|
142
|
+
visitor_id: pageData.visitor_id,
|
|
143
|
+
session_id: pageData.session_id,
|
|
144
|
+
url: window.location.href,
|
|
145
|
+
path: window.location.pathname,
|
|
146
|
+
referrer: state.currentReferrer,
|
|
147
|
+
user_agent: pageData.user_agent,
|
|
148
|
+
duration_ms: effectiveDuration, // 使用活跃时间
|
|
149
|
+
timestamp: Date.now(),
|
|
150
|
+
type: eventType, // 添加事件类型
|
|
151
|
+
// 设备和平台信息
|
|
152
|
+
device: getDeviceInfo(),
|
|
153
|
+
platform: getPlatform(),
|
|
154
|
+
browser_language: getBrowserLanguage(),
|
|
155
|
+
// 页面信息
|
|
156
|
+
page_title: document.title || null,
|
|
157
|
+
// 屏幕信息
|
|
158
|
+
screen_width: screen.width,
|
|
159
|
+
screen_height: screen.height,
|
|
160
|
+
viewport_width: window.innerWidth,
|
|
161
|
+
viewport_height: window.innerHeight,
|
|
162
|
+
// 时区信息
|
|
163
|
+
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
|
|
164
|
+
// 是否新用户(简单判断:第一次访问该域名)
|
|
165
|
+
is_new_user:
|
|
166
|
+
!getCookie("visitor_id") ||
|
|
167
|
+
getCookie("visitor_id") === pageData.visitor_id,
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// 发送访客数据(支持不同类型)
|
|
172
|
+
export function trackPageEvent(
|
|
173
|
+
eventType: string,
|
|
174
|
+
pageData: PageData,
|
|
175
|
+
state: AnalyticsState,
|
|
176
|
+
apiEndpoint: string,
|
|
177
|
+
debugLog: (...args: any[]) => void
|
|
178
|
+
): void {
|
|
179
|
+
// 对于 pageView 事件,检测页面类型
|
|
180
|
+
if (eventType === "pageView") {
|
|
181
|
+
eventType = detectPageType(eventType);
|
|
182
|
+
debugLog("Detected page type:", eventType);
|
|
183
|
+
|
|
184
|
+
// 检查是否为真正的页面访问
|
|
185
|
+
if (!isRealPageVisit(state)) {
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// 计算活跃时间
|
|
191
|
+
let effectiveDuration = 0;
|
|
192
|
+
if (eventType === "pageLeave") {
|
|
193
|
+
updateActiveTime(state); // 更新到当前时间
|
|
194
|
+
effectiveDuration = state.activeTime;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
debugLog("Using referrer:", state.currentReferrer, "for event:", eventType);
|
|
198
|
+
|
|
199
|
+
const data = createTrackingData(pageData, state, eventType, effectiveDuration);
|
|
200
|
+
|
|
201
|
+
// 优化的网络请求处理
|
|
202
|
+
sendTrackingData(data, eventType, state, apiEndpoint);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// 兼容性函数,保持向后兼容
|
|
206
|
+
export function trackPageVisit(
|
|
207
|
+
pageData: PageData,
|
|
208
|
+
state: AnalyticsState,
|
|
209
|
+
apiEndpoint: string,
|
|
210
|
+
debugLog: (...args: any[]) => void
|
|
211
|
+
): void {
|
|
212
|
+
// 检查是否在短时间内(200ms)重复调用同一个 URL 的 pageView
|
|
213
|
+
const currentUrl = window.location.href;
|
|
214
|
+
const now = Date.now();
|
|
215
|
+
|
|
216
|
+
if (state.isPageTracked && state.currentPageUrl === currentUrl && now - state.lastTrackTime < 200) {
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
trackPageEvent("pageView", pageData, state, apiEndpoint, debugLog);
|
|
221
|
+
}
|
|
@@ -1,60 +1,60 @@
|
|
|
1
|
-
// Analytics 相关的类型定义
|
|
2
|
-
|
|
3
|
-
export interface AnalyticsProps {
|
|
4
|
-
apiEndpoint?: string;
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
export interface PageData {
|
|
8
|
-
visitor_id: string;
|
|
9
|
-
session_id: string;
|
|
10
|
-
url: string;
|
|
11
|
-
path: string;
|
|
12
|
-
referrer: string | null;
|
|
13
|
-
user_agent: string;
|
|
14
|
-
start_time: number;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export interface TrackingData {
|
|
18
|
-
visitor_id: string;
|
|
19
|
-
session_id: string;
|
|
20
|
-
url: string;
|
|
21
|
-
path: string;
|
|
22
|
-
referrer: string | null;
|
|
23
|
-
user_agent: string;
|
|
24
|
-
duration_ms: number;
|
|
25
|
-
timestamp: number;
|
|
26
|
-
type: string;
|
|
27
|
-
device: string;
|
|
28
|
-
platform: string;
|
|
29
|
-
browser_language: string;
|
|
30
|
-
page_title: string | null;
|
|
31
|
-
screen_width: number;
|
|
32
|
-
screen_height: number;
|
|
33
|
-
viewport_width: number;
|
|
34
|
-
viewport_height: number;
|
|
35
|
-
timezone: string;
|
|
36
|
-
is_new_user: boolean;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export interface RequestQueueItem {
|
|
40
|
-
data: TrackingData;
|
|
41
|
-
eventType: string;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export interface AnalyticsState {
|
|
45
|
-
isPageTracked: boolean;
|
|
46
|
-
currentPageUrl: string;
|
|
47
|
-
pageLoadTime: number;
|
|
48
|
-
lastTrackTime: number;
|
|
49
|
-
currentReferrer: string | null;
|
|
50
|
-
activeTime: number;
|
|
51
|
-
lastActiveTime: number;
|
|
52
|
-
isPageVisible: boolean;
|
|
53
|
-
requestQueue: RequestQueueItem[];
|
|
54
|
-
isRequestPending: boolean;
|
|
55
|
-
isAstroEnvironment: boolean;
|
|
56
|
-
pageLeaveTracked: boolean;
|
|
57
|
-
initialLoadHandled: boolean;
|
|
58
|
-
interactionThrottled: boolean;
|
|
59
|
-
lastInteractionTime: number;
|
|
60
|
-
}
|
|
1
|
+
// Analytics 相关的类型定义
|
|
2
|
+
|
|
3
|
+
export interface AnalyticsProps {
|
|
4
|
+
apiEndpoint?: string;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export interface PageData {
|
|
8
|
+
visitor_id: string;
|
|
9
|
+
session_id: string;
|
|
10
|
+
url: string;
|
|
11
|
+
path: string;
|
|
12
|
+
referrer: string | null;
|
|
13
|
+
user_agent: string;
|
|
14
|
+
start_time: number;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface TrackingData {
|
|
18
|
+
visitor_id: string;
|
|
19
|
+
session_id: string;
|
|
20
|
+
url: string;
|
|
21
|
+
path: string;
|
|
22
|
+
referrer: string | null;
|
|
23
|
+
user_agent: string;
|
|
24
|
+
duration_ms: number;
|
|
25
|
+
timestamp: number;
|
|
26
|
+
type: string;
|
|
27
|
+
device: string;
|
|
28
|
+
platform: string;
|
|
29
|
+
browser_language: string;
|
|
30
|
+
page_title: string | null;
|
|
31
|
+
screen_width: number;
|
|
32
|
+
screen_height: number;
|
|
33
|
+
viewport_width: number;
|
|
34
|
+
viewport_height: number;
|
|
35
|
+
timezone: string;
|
|
36
|
+
is_new_user: boolean;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface RequestQueueItem {
|
|
40
|
+
data: TrackingData;
|
|
41
|
+
eventType: string;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface AnalyticsState {
|
|
45
|
+
isPageTracked: boolean;
|
|
46
|
+
currentPageUrl: string;
|
|
47
|
+
pageLoadTime: number;
|
|
48
|
+
lastTrackTime: number;
|
|
49
|
+
currentReferrer: string | null;
|
|
50
|
+
activeTime: number;
|
|
51
|
+
lastActiveTime: number;
|
|
52
|
+
isPageVisible: boolean;
|
|
53
|
+
requestQueue: RequestQueueItem[];
|
|
54
|
+
isRequestPending: boolean;
|
|
55
|
+
isAstroEnvironment: boolean;
|
|
56
|
+
pageLeaveTracked: boolean;
|
|
57
|
+
initialLoadHandled: boolean;
|
|
58
|
+
interactionThrottled: boolean;
|
|
59
|
+
lastInteractionTime: number;
|
|
60
|
+
}
|