Skip to content

Commit 6e8ad66

Browse files
feat: add optional third-party analytics config (Clarity)
1 parent c29c8d6 commit 6e8ad66

4 files changed

Lines changed: 46 additions & 18 deletions

File tree

src/config.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
ShareConfig,
1616
SidebarLayoutConfig,
1717
SiteConfig,
18+
ThirdPartyAnalyticsConfig,
1819
} from "./types/config";
1920
import { LinkPreset } from "./types/config";
2021

@@ -240,6 +241,11 @@ export const siteConfig: SiteConfig = {
240241
height: 3, // 进度条高度 3px
241242
duration: 6000, // 动画时长 6s
242243
},
244+
245+
thirdPartyAnalytics: {
246+
enable: false, // 是否启用第三方统计(Microsoft Clarity),默认关闭,启用可能影响 Lighthouse 评分
247+
clarityId: "", // Clarity 项目 ID
248+
},
243249
};
244250
export const fullscreenWallpaperConfig: FullscreenWallpaperConfig = {
245251
src: {

src/layouts/Layout.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ if (siteConfig.font) {
161161
>
162162
<head>
163163
<!-- 第三方分析脚本 -->
164-
<AnalyticsScripts />
164+
<AnalyticsScripts thirdPartyAnalytics={siteConfig.thirdPartyAnalytics} />
165165

166166
<!-- SEO Meta Tags, Open Graph, Twitter Card, Favicons, Theme Init -->
167167
<HeadTags

src/layouts/partials/AnalyticsScripts.astro

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,32 @@
55
*
66
* 包含:
77
* - Google Tag Manager (GTM)
8-
* - Microsoft Clarity
8+
* - Microsoft Clarity (可选,通过 thirdPartyAnalytics 配置控制)
99
*
1010
* 使用 requestIdleCallback 延迟加载以优化性能
1111
*/
1212
1313
interface Props {
1414
gtmId?: string;
15-
clarityId?: string;
15+
thirdPartyAnalytics?: {
16+
enable: boolean;
17+
clarityId?: string;
18+
};
1619
}
1720
18-
const { gtmId = "GTM-KRX3XGVH", clarityId = "tjr3vkhj8i" } = Astro.props;
21+
const {
22+
gtmId = "GTM-KRX3XGVH",
23+
thirdPartyAnalytics = { enable: false, clarityId: "" },
24+
} = Astro.props;
25+
26+
const clarityEnable = thirdPartyAnalytics.enable && thirdPartyAnalytics.clarityId;
1927
---
2028

2129
<!-- 第三方分析脚本 - 延迟加载优化 -->
22-
<script is:inline define:vars={{ gtmId, clarityId }}>
30+
<script
31+
is:inline
32+
define:vars={{ gtmId, clarityEnable, clarityId: thirdPartyAnalytics.clarityId || "" }}
33+
>
2334
// 使用 requestIdleCallback 延迟加载第三方脚本
2435
// 标记脚本是否已加载,防止 swup 导航时重复加载
2536
window.analyticsLoaded = window.analyticsLoaded || false;
@@ -45,19 +56,21 @@ const { gtmId = "GTM-KRX3XGVH", clarityId = "tjr3vkhj8i" } = Astro.props;
4556
f.parentNode.insertBefore(j, f);
4657
})(window, document, "script", "dataLayer", gtmId);
4758

48-
// Clarity
49-
(function (c, l, a, r, i, t, y) {
50-
c[a] =
51-
c[a] ||
52-
function () {
53-
(c[a].q = c[a].q || []).push(arguments);
54-
};
55-
t = l.createElement(r);
56-
t.async = 1;
57-
t.src = "https://www.clarity.ms/tag/" + i;
58-
y = l.getElementsByTagName(r)[0];
59-
y.parentNode.insertBefore(t, y);
60-
})(window, document, "clarity", "script", clarityId);
59+
// Clarity (仅在启用时加载)
60+
if (clarityEnable && clarityId) {
61+
(function (c, l, a, r, i, t, y) {
62+
c[a] =
63+
c[a] ||
64+
function () {
65+
(c[a].q = c[a].q || []).push(arguments);
66+
};
67+
t = l.createElement(r);
68+
t.async = 1;
69+
t.src = "https://www.clarity.ms/tag/" + i;
70+
y = l.getElementsByTagName(r)[0];
71+
y.parentNode.insertBefore(t, y);
72+
})(window, document, "clarity", "script", clarityId);
73+
}
6174
}
6275

6376
// 在浏览器空闲时加载,或者在 3 秒后加载

src/types/config.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ export interface SiteConfig {
193193
favicon: Favicon[];
194194
showLastModified: boolean; // 控制"上次编辑"卡片显示的开关
195195
pageProgressBar?: PageProgressBarConfig; // 页面顶部进度条配置
196+
thirdPartyAnalytics?: ThirdPartyAnalyticsConfig; // 第三方统计配置
196197
}
197198

198199
export interface Favicon {
@@ -513,3 +514,11 @@ export interface PageProgressBarConfig {
513514
height?: number; // 进度条高度,默认 3px
514515
duration?: number; // 动画时长,默认 8000ms
515516
}
517+
518+
/**
519+
* 第三方统计配置(可能影响 Lighthouse 评分)
520+
*/
521+
export interface ThirdPartyAnalyticsConfig {
522+
enable: boolean; // 是否启用第三方统计(Microsoft Clarity),默认关闭
523+
clarityId?: string; // Clarity 项目 ID
524+
}

0 commit comments

Comments
 (0)