@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,214 +1,214 @@
|
|
|
1
|
-
// Analytics 单例管理器
|
|
2
|
-
import type { AnalyticsState, PageData } from './types';
|
|
3
|
-
import {
|
|
4
|
-
createDebugLogger,
|
|
5
|
-
generateId,
|
|
6
|
-
getVisitorId
|
|
7
|
-
} from './utils';
|
|
8
|
-
import { trackPageEvent } from './tracking';
|
|
9
|
-
|
|
10
|
-
class AnalyticsSingleton {
|
|
11
|
-
private static instance: AnalyticsSingleton | null = null;
|
|
12
|
-
private initialized = false;
|
|
13
|
-
private state: AnalyticsState;
|
|
14
|
-
private pageData: PageData;
|
|
15
|
-
private apiEndpoint: string;
|
|
16
|
-
private debugLog: (...args: any[]) => void;
|
|
17
|
-
private eventHandlers: Array<() => void> = [];
|
|
18
|
-
|
|
19
|
-
private constructor(apiEndpoint: string) {
|
|
20
|
-
this.apiEndpoint = apiEndpoint;
|
|
21
|
-
this.debugLog = createDebugLogger();
|
|
22
|
-
|
|
23
|
-
// 初始化访客和会话信息
|
|
24
|
-
const visitorId = getVisitorId();
|
|
25
|
-
let sessionId = sessionStorage.getItem('session_id');
|
|
26
|
-
if (!sessionId) {
|
|
27
|
-
sessionId = generateId('s');
|
|
28
|
-
sessionStorage.setItem('session_id', sessionId);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
// 页面信息
|
|
32
|
-
this.pageData = {
|
|
33
|
-
visitor_id: visitorId,
|
|
34
|
-
session_id: sessionId,
|
|
35
|
-
url: window.location.href,
|
|
36
|
-
path: window.location.pathname,
|
|
37
|
-
referrer: document.referrer || null,
|
|
38
|
-
user_agent: navigator.userAgent,
|
|
39
|
-
start_time: Date.now(),
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
// 状态管理
|
|
43
|
-
this.state = {
|
|
44
|
-
isPageTracked: false,
|
|
45
|
-
currentPageUrl: window.location.href,
|
|
46
|
-
pageLoadTime: Date.now(),
|
|
47
|
-
lastTrackTime: 0,
|
|
48
|
-
currentReferrer: document.referrer || null,
|
|
49
|
-
activeTime: 0,
|
|
50
|
-
lastActiveTime: Date.now(),
|
|
51
|
-
isPageVisible: !document.hidden,
|
|
52
|
-
requestQueue: [],
|
|
53
|
-
isRequestPending: false,
|
|
54
|
-
isAstroEnvironment: false,
|
|
55
|
-
pageLeaveTracked: false,
|
|
56
|
-
initialLoadHandled: false,
|
|
57
|
-
interactionThrottled: false,
|
|
58
|
-
lastInteractionTime: 0,
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
static getInstance(apiEndpoint = "/api/track-visitor"): AnalyticsSingleton {
|
|
64
|
-
if (!AnalyticsSingleton.instance) {
|
|
65
|
-
AnalyticsSingleton.instance = new AnalyticsSingleton(apiEndpoint);
|
|
66
|
-
}
|
|
67
|
-
return AnalyticsSingleton.instance;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
static reset(): void {
|
|
71
|
-
if (AnalyticsSingleton.instance) {
|
|
72
|
-
AnalyticsSingleton.instance.cleanup();
|
|
73
|
-
AnalyticsSingleton.instance = null;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
init(): void {
|
|
78
|
-
if (this.initialized) {
|
|
79
|
-
return;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
this.initialized = true;
|
|
83
|
-
this.setupEventListeners();
|
|
84
|
-
this.handleInitialPageLoad();
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
private setupEventListeners(): void {
|
|
88
|
-
// 页面离开事件
|
|
89
|
-
const handlePageLeave = () => {
|
|
90
|
-
if (this.state.pageLeaveTracked || this.state.lastTrackTime === 0) {
|
|
91
|
-
return;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
this.state.pageLeaveTracked = true;
|
|
95
|
-
trackPageEvent("pageLeave", this.pageData, this.state, this.apiEndpoint, this.debugLog);
|
|
96
|
-
};
|
|
97
|
-
|
|
98
|
-
// 页面离开事件监听器
|
|
99
|
-
const handleBeforeUnload = () => handlePageLeave();
|
|
100
|
-
const handlePageHide = () => handlePageLeave();
|
|
101
|
-
|
|
102
|
-
window.addEventListener('beforeunload', handleBeforeUnload);
|
|
103
|
-
window.addEventListener('pagehide', handlePageHide);
|
|
104
|
-
|
|
105
|
-
// Astro 事件监听
|
|
106
|
-
const handleAstroBeforeSwap = () => {
|
|
107
|
-
handlePageLeave();
|
|
108
|
-
};
|
|
109
|
-
|
|
110
|
-
const handleAstroAfterSwap = () => {
|
|
111
|
-
this.state.isAstroEnvironment = true;
|
|
112
|
-
|
|
113
|
-
const newUrl = window.location.href;
|
|
114
|
-
if (newUrl !== this.state.currentPageUrl) {
|
|
115
|
-
// 更新 referrer:页面切换时,referrer 是上一个页面
|
|
116
|
-
this.state.currentReferrer = this.state.currentPageUrl;
|
|
117
|
-
|
|
118
|
-
// 更新页面状态
|
|
119
|
-
this.state.currentPageUrl = newUrl;
|
|
120
|
-
this.pageData.url = newUrl;
|
|
121
|
-
this.pageData.path = window.location.pathname;
|
|
122
|
-
this.pageData.start_time = Date.now();
|
|
123
|
-
this.state.pageLoadTime = Date.now();
|
|
124
|
-
this.state.isPageTracked = false;
|
|
125
|
-
this.state.pageLeaveTracked = false;
|
|
126
|
-
this.state.activeTime = 0;
|
|
127
|
-
this.state.lastActiveTime = Date.now();
|
|
128
|
-
this.state.isPageVisible = !document.hidden;
|
|
129
|
-
|
|
130
|
-
// 发送新页面统计
|
|
131
|
-
this.trackPageVisit();
|
|
132
|
-
}
|
|
133
|
-
};
|
|
134
|
-
|
|
135
|
-
document.addEventListener('astro:before-swap', handleAstroBeforeSwap);
|
|
136
|
-
document.addEventListener('astro:after-swap', handleAstroAfterSwap);
|
|
137
|
-
|
|
138
|
-
// 页面可见性变化
|
|
139
|
-
const handleVisibilityChange = () => {
|
|
140
|
-
const now = Date.now();
|
|
141
|
-
if (document.visibilityState === 'visible') {
|
|
142
|
-
this.state.isPageVisible = true;
|
|
143
|
-
this.state.lastActiveTime = now;
|
|
144
|
-
} else {
|
|
145
|
-
if (this.state.isPageVisible) {
|
|
146
|
-
this.state.activeTime += now - this.state.lastActiveTime;
|
|
147
|
-
}
|
|
148
|
-
this.state.isPageVisible = false;
|
|
149
|
-
}
|
|
150
|
-
};
|
|
151
|
-
|
|
152
|
-
document.addEventListener('visibilitychange', handleVisibilityChange);
|
|
153
|
-
|
|
154
|
-
// 用户交互事件
|
|
155
|
-
const handleUserInteraction = () => {
|
|
156
|
-
if (!this.state.isPageVisible || this.state.interactionThrottled) return;
|
|
157
|
-
|
|
158
|
-
const now = Date.now();
|
|
159
|
-
if (now - this.state.lastInteractionTime < 1000) return;
|
|
160
|
-
|
|
161
|
-
this.state.lastInteractionTime = now;
|
|
162
|
-
this.state.interactionThrottled = true;
|
|
163
|
-
|
|
164
|
-
if (now - this.state.lastActiveTime > 30000) {
|
|
165
|
-
this.state.lastActiveTime = now;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
setTimeout(() => {
|
|
169
|
-
this.state.interactionThrottled = false;
|
|
170
|
-
}, 1000);
|
|
171
|
-
};
|
|
172
|
-
|
|
173
|
-
const interactionEvents = ['click', 'keydown', 'scroll'];
|
|
174
|
-
interactionEvents.forEach((event) => {
|
|
175
|
-
document.addEventListener(event, handleUserInteraction, {
|
|
176
|
-
passive: true,
|
|
177
|
-
capture: false,
|
|
178
|
-
});
|
|
179
|
-
});
|
|
180
|
-
|
|
181
|
-
// 保存事件处理器引用,用于清理
|
|
182
|
-
this.eventHandlers = [
|
|
183
|
-
() => window.removeEventListener('beforeunload', handleBeforeUnload),
|
|
184
|
-
() => window.removeEventListener('pagehide', handlePageHide),
|
|
185
|
-
() => document.removeEventListener('astro:before-swap', handleAstroBeforeSwap),
|
|
186
|
-
() => document.removeEventListener('astro:after-swap', handleAstroAfterSwap),
|
|
187
|
-
() => document.removeEventListener('visibilitychange', handleVisibilityChange),
|
|
188
|
-
...interactionEvents.map(event =>
|
|
189
|
-
() => document.removeEventListener(event, handleUserInteraction)
|
|
190
|
-
)
|
|
191
|
-
];
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
private handleInitialPageLoad(): void {
|
|
195
|
-
if (this.state.isPageTracked || this.state.initialLoadHandled) {
|
|
196
|
-
return;
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
this.state.initialLoadHandled = true;
|
|
200
|
-
this.trackPageVisit();
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
private trackPageVisit(): void {
|
|
204
|
-
trackPageEvent("pageView", this.pageData, this.state, this.apiEndpoint, this.debugLog);
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
cleanup(): void {
|
|
208
|
-
this.eventHandlers.forEach(cleanup => cleanup());
|
|
209
|
-
this.eventHandlers = [];
|
|
210
|
-
this.initialized = false;
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
export default AnalyticsSingleton;
|
|
1
|
+
// Analytics 单例管理器
|
|
2
|
+
import type { AnalyticsState, PageData } from './types';
|
|
3
|
+
import {
|
|
4
|
+
createDebugLogger,
|
|
5
|
+
generateId,
|
|
6
|
+
getVisitorId
|
|
7
|
+
} from './utils';
|
|
8
|
+
import { trackPageEvent } from './tracking';
|
|
9
|
+
|
|
10
|
+
class AnalyticsSingleton {
|
|
11
|
+
private static instance: AnalyticsSingleton | null = null;
|
|
12
|
+
private initialized = false;
|
|
13
|
+
private state: AnalyticsState;
|
|
14
|
+
private pageData: PageData;
|
|
15
|
+
private apiEndpoint: string;
|
|
16
|
+
private debugLog: (...args: any[]) => void;
|
|
17
|
+
private eventHandlers: Array<() => void> = [];
|
|
18
|
+
|
|
19
|
+
private constructor(apiEndpoint: string) {
|
|
20
|
+
this.apiEndpoint = apiEndpoint;
|
|
21
|
+
this.debugLog = createDebugLogger();
|
|
22
|
+
|
|
23
|
+
// 初始化访客和会话信息
|
|
24
|
+
const visitorId = getVisitorId();
|
|
25
|
+
let sessionId = sessionStorage.getItem('session_id');
|
|
26
|
+
if (!sessionId) {
|
|
27
|
+
sessionId = generateId('s');
|
|
28
|
+
sessionStorage.setItem('session_id', sessionId);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// 页面信息
|
|
32
|
+
this.pageData = {
|
|
33
|
+
visitor_id: visitorId,
|
|
34
|
+
session_id: sessionId,
|
|
35
|
+
url: window.location.href,
|
|
36
|
+
path: window.location.pathname,
|
|
37
|
+
referrer: document.referrer || null,
|
|
38
|
+
user_agent: navigator.userAgent,
|
|
39
|
+
start_time: Date.now(),
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
// 状态管理
|
|
43
|
+
this.state = {
|
|
44
|
+
isPageTracked: false,
|
|
45
|
+
currentPageUrl: window.location.href,
|
|
46
|
+
pageLoadTime: Date.now(),
|
|
47
|
+
lastTrackTime: 0,
|
|
48
|
+
currentReferrer: document.referrer || null,
|
|
49
|
+
activeTime: 0,
|
|
50
|
+
lastActiveTime: Date.now(),
|
|
51
|
+
isPageVisible: !document.hidden,
|
|
52
|
+
requestQueue: [],
|
|
53
|
+
isRequestPending: false,
|
|
54
|
+
isAstroEnvironment: false,
|
|
55
|
+
pageLeaveTracked: false,
|
|
56
|
+
initialLoadHandled: false,
|
|
57
|
+
interactionThrottled: false,
|
|
58
|
+
lastInteractionTime: 0,
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
static getInstance(apiEndpoint = "/api/track-visitor"): AnalyticsSingleton {
|
|
64
|
+
if (!AnalyticsSingleton.instance) {
|
|
65
|
+
AnalyticsSingleton.instance = new AnalyticsSingleton(apiEndpoint);
|
|
66
|
+
}
|
|
67
|
+
return AnalyticsSingleton.instance;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
static reset(): void {
|
|
71
|
+
if (AnalyticsSingleton.instance) {
|
|
72
|
+
AnalyticsSingleton.instance.cleanup();
|
|
73
|
+
AnalyticsSingleton.instance = null;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
init(): void {
|
|
78
|
+
if (this.initialized) {
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
this.initialized = true;
|
|
83
|
+
this.setupEventListeners();
|
|
84
|
+
this.handleInitialPageLoad();
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
private setupEventListeners(): void {
|
|
88
|
+
// 页面离开事件
|
|
89
|
+
const handlePageLeave = () => {
|
|
90
|
+
if (this.state.pageLeaveTracked || this.state.lastTrackTime === 0) {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
this.state.pageLeaveTracked = true;
|
|
95
|
+
trackPageEvent("pageLeave", this.pageData, this.state, this.apiEndpoint, this.debugLog);
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
// 页面离开事件监听器
|
|
99
|
+
const handleBeforeUnload = () => handlePageLeave();
|
|
100
|
+
const handlePageHide = () => handlePageLeave();
|
|
101
|
+
|
|
102
|
+
window.addEventListener('beforeunload', handleBeforeUnload);
|
|
103
|
+
window.addEventListener('pagehide', handlePageHide);
|
|
104
|
+
|
|
105
|
+
// Astro 事件监听
|
|
106
|
+
const handleAstroBeforeSwap = () => {
|
|
107
|
+
handlePageLeave();
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
const handleAstroAfterSwap = () => {
|
|
111
|
+
this.state.isAstroEnvironment = true;
|
|
112
|
+
|
|
113
|
+
const newUrl = window.location.href;
|
|
114
|
+
if (newUrl !== this.state.currentPageUrl) {
|
|
115
|
+
// 更新 referrer:页面切换时,referrer 是上一个页面
|
|
116
|
+
this.state.currentReferrer = this.state.currentPageUrl;
|
|
117
|
+
|
|
118
|
+
// 更新页面状态
|
|
119
|
+
this.state.currentPageUrl = newUrl;
|
|
120
|
+
this.pageData.url = newUrl;
|
|
121
|
+
this.pageData.path = window.location.pathname;
|
|
122
|
+
this.pageData.start_time = Date.now();
|
|
123
|
+
this.state.pageLoadTime = Date.now();
|
|
124
|
+
this.state.isPageTracked = false;
|
|
125
|
+
this.state.pageLeaveTracked = false;
|
|
126
|
+
this.state.activeTime = 0;
|
|
127
|
+
this.state.lastActiveTime = Date.now();
|
|
128
|
+
this.state.isPageVisible = !document.hidden;
|
|
129
|
+
|
|
130
|
+
// 发送新页面统计
|
|
131
|
+
this.trackPageVisit();
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
document.addEventListener('astro:before-swap', handleAstroBeforeSwap);
|
|
136
|
+
document.addEventListener('astro:after-swap', handleAstroAfterSwap);
|
|
137
|
+
|
|
138
|
+
// 页面可见性变化
|
|
139
|
+
const handleVisibilityChange = () => {
|
|
140
|
+
const now = Date.now();
|
|
141
|
+
if (document.visibilityState === 'visible') {
|
|
142
|
+
this.state.isPageVisible = true;
|
|
143
|
+
this.state.lastActiveTime = now;
|
|
144
|
+
} else {
|
|
145
|
+
if (this.state.isPageVisible) {
|
|
146
|
+
this.state.activeTime += now - this.state.lastActiveTime;
|
|
147
|
+
}
|
|
148
|
+
this.state.isPageVisible = false;
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
document.addEventListener('visibilitychange', handleVisibilityChange);
|
|
153
|
+
|
|
154
|
+
// 用户交互事件
|
|
155
|
+
const handleUserInteraction = () => {
|
|
156
|
+
if (!this.state.isPageVisible || this.state.interactionThrottled) return;
|
|
157
|
+
|
|
158
|
+
const now = Date.now();
|
|
159
|
+
if (now - this.state.lastInteractionTime < 1000) return;
|
|
160
|
+
|
|
161
|
+
this.state.lastInteractionTime = now;
|
|
162
|
+
this.state.interactionThrottled = true;
|
|
163
|
+
|
|
164
|
+
if (now - this.state.lastActiveTime > 30000) {
|
|
165
|
+
this.state.lastActiveTime = now;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
setTimeout(() => {
|
|
169
|
+
this.state.interactionThrottled = false;
|
|
170
|
+
}, 1000);
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
const interactionEvents = ['click', 'keydown', 'scroll'];
|
|
174
|
+
interactionEvents.forEach((event) => {
|
|
175
|
+
document.addEventListener(event, handleUserInteraction, {
|
|
176
|
+
passive: true,
|
|
177
|
+
capture: false,
|
|
178
|
+
});
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
// 保存事件处理器引用,用于清理
|
|
182
|
+
this.eventHandlers = [
|
|
183
|
+
() => window.removeEventListener('beforeunload', handleBeforeUnload),
|
|
184
|
+
() => window.removeEventListener('pagehide', handlePageHide),
|
|
185
|
+
() => document.removeEventListener('astro:before-swap', handleAstroBeforeSwap),
|
|
186
|
+
() => document.removeEventListener('astro:after-swap', handleAstroAfterSwap),
|
|
187
|
+
() => document.removeEventListener('visibilitychange', handleVisibilityChange),
|
|
188
|
+
...interactionEvents.map(event =>
|
|
189
|
+
() => document.removeEventListener(event, handleUserInteraction)
|
|
190
|
+
)
|
|
191
|
+
];
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
private handleInitialPageLoad(): void {
|
|
195
|
+
if (this.state.isPageTracked || this.state.initialLoadHandled) {
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
this.state.initialLoadHandled = true;
|
|
200
|
+
this.trackPageVisit();
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
private trackPageVisit(): void {
|
|
204
|
+
trackPageEvent("pageView", this.pageData, this.state, this.apiEndpoint, this.debugLog);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
cleanup(): void {
|
|
208
|
+
this.eventHandlers.forEach(cleanup => cleanup());
|
|
209
|
+
this.eventHandlers = [];
|
|
210
|
+
this.initialized = false;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
export default AnalyticsSingleton;
|