Skip to content

Commit aff82ec

Browse files
committed
feat(analytics): GA4 tracking on landing and book + WeChat cover
Tag G-4QDL6QMBNS attached to: - landing/index.html (sync inline) - /book/ all pages via mdbook additional-js (theme/gtag.js) Pages report will distinguish landing (/) vs book chapters (/book/...). Also adds 1080x1080 WeChat-friendly cover (cover-wechat-square.png).
1 parent 2b52637 commit aff82ec

4 files changed

Lines changed: 32 additions & 0 deletions

File tree

book/book.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ git-repository-icon = "fa-github"
1414
edit-url-template = "https://github.com/jnMetaCode/ai-coding-guide/edit/main/book/{path}"
1515
site-url = "/book/"
1616
additional-css = ["theme/custom.css"]
17+
additional-js = ["theme/gtag.js"]
1718

1819
[output.html.search]
1920
enable = true

book/theme/gtag.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Google Analytics 4 — AI 不止语
2+
// Loaded for every page in /book/ via mdbook additional-js
3+
(function() {
4+
// Async-load gtag.js
5+
var s = document.createElement('script');
6+
s.async = true;
7+
s.src = 'https://www.googletagmanager.com/gtag/js?id=G-4QDL6QMBNS';
8+
document.head.appendChild(s);
9+
10+
// Initialize dataLayer + gtag
11+
window.dataLayer = window.dataLayer || [];
12+
function gtag(){ dataLayer.push(arguments); }
13+
window.gtag = gtag;
14+
15+
gtag('js', new Date());
16+
gtag('config', 'G-4QDL6QMBNS', {
17+
// Tag the book pages so reports can split landing vs book traffic
18+
page_path: window.location.pathname,
19+
custom_map: { dimension1: 'book_chapter' },
20+
book_chapter: document.title || 'unknown'
21+
});
22+
})();

landing/cover-wechat-square.png

707 KB
Loading

landing/index.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@
3333
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin/>
3434
<link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700;900&family=Noto+Sans+SC:wght@300;400;500;600;700&family=JetBrains+Mono:wght@400;500;600&family=LXGW+WenKai+TC:wght@300;400;700&display=swap" rel="stylesheet"/>
3535
<link rel="stylesheet" href="styles.css"/>
36+
37+
<!-- Google Analytics 4 -->
38+
<script async src="https://www.googletagmanager.com/gtag/js?id=G-4QDL6QMBNS"></script>
39+
<script>
40+
window.dataLayer = window.dataLayer || [];
41+
function gtag(){dataLayer.push(arguments);}
42+
gtag('js', new Date());
43+
gtag('config', 'G-4QDL6QMBNS');
44+
</script>
3645
</head>
3746
<body>
3847
<div id="root"></div>

0 commit comments

Comments
 (0)