Skip to content

Commit ae86fd0

Browse files
claudejsshin2022
authored andcommitted
Share reading progress with the hub and count only the deployed site
- Store reading data under cil-book-* so it does not clash with other team sites on the domain, and keep the last chapter's label and the chapter count for the hub to show. - Re-read stored progress and votes before writing so a second tab does not overwrite them. - After the first finished chapter, point to the web book reader stamp in the hub passport. - Load GA4 only on causalinferencelab.github.io and turn off Google signals and ad personalisation, matching the hub. - Treat the hub's own pages (game, lab, Builders, posts) as the hub when tracking clicks.
1 parent da6cd92 commit ae86fd0

5 files changed

Lines changed: 42 additions & 9 deletions

File tree

‎README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ cd book && quarto preview
127127

128128
`book/prepare.py`는 빌드할 때 [번역 가이드](TRANSLATION_GUIDE.md)의 용어표로 웹북의 '용어집' 페이지를 만들고, `book/postrender.py`는 렌더링이 끝난 뒤 웹북 검색 색인(`search-index.json`)을 만듭니다. `book/prepare.py`는 빌드할 때만 장 제목, 수식 표기, 접힌 코드, 옮긴이 주 상자를 책 형식으로 바꿉니다. 그래서 노트북은 원서와 같은 구조로 두면 됩니다.
129129

130+
읽기 기능(읽은 장 기록, 이어 읽기, 장 끝 평가, 허브로 가는 링크)은 `book/reader.html`, GA4 설정은 `book/analytics.html`에 있습니다. 읽은 장 기록은 독자의 브라우저(`localStorage`의 `cil-book-*`)에만 남고, 같은 도메인의 [Causal Inference Hub](https://causalinferencelab.github.io/)가 이 기록으로 진도와 활동 여권 스탬프를 보여 줍니다.
131+
130132
<br>
131133

132134
## 원서 변경 반영

‎book/analytics.html‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
<!-- Google Analytics 4 (웹북). 허브는 따로 G-C5V0X2SGD3을 쓴다. -->
2-
<script async src="https://www.googletagmanager.com/gtag/js?id=G-BEBVKYH8HL"></script>
32
<script>
3+
// 배포된 웹북에서만 집계한다(로컬·PR 미리보기 제외). 허브와 같은 설정: Google 신호·광고 개인화 끔.
44
window.dataLayer = window.dataLayer || [];
55
function gtag(){dataLayer.push(arguments);}
6-
gtag('js', new Date());
7-
gtag('config', 'G-BEBVKYH8HL', { anonymize_ip: true, page_location: location.href });
8-
// 유입 정보(utm_*)는 page_location으로 GA에 넘기고, 주소창에서는 지워 공유되는 링크를 깔끔하게 둔다
96
(function () {
7+
var ID = "G-BEBVKYH8HL";
8+
if (location.hostname === "causalinferencelab.github.io") {
9+
var s = document.createElement("script");
10+
s.async = true;
11+
s.src = "https://www.googletagmanager.com/gtag/js?id=" + ID;
12+
document.head.appendChild(s);
13+
gtag("js", new Date());
14+
gtag("config", ID, { page_location: location.href, allow_google_signals: false, allow_ad_personalization_signals: false });
15+
}
16+
// 유입 정보(utm_*)는 page_location으로 GA에 넘기고, 주소창에서는 지워 공유되는 링크를 깔끔하게 둔다
1017
try {
1118
var u = new URL(location.href), drop = [];
1219
u.searchParams.forEach(function (v, k) { if (k.indexOf("utm_") === 0) drop.push(k); });

‎book/reader.html‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
var HUB = "https://causalinferencelab.github.io/";
66
var BOOK_PATH = "/Causal-Inference-with-Python/";
77
var NB = "https://github.com/CausalInferenceLab/Causal-Inference-with-Python/blob/main/causal-inference-for-the-brave-and-true/";
8-
var KEY_READ = "cil-read", KEY_LAST = "cil-last", KEY_VOTE = "cil-vote";
8+
// 같은 도메인의 허브와 저장소를 함께 쓰므로 웹북 기록은 cil-book-* 이름을 쓴다. 허브는 이 기록으로 진도와 여권 스탬프를 보여 준다.
9+
var KEY_READ = "cil-book-read", KEY_LAST = "cil-book-last", KEY_VOTE = "cil-book-vote";
10+
var HUB_PAGES = /^\/(?:$|index\.html$|article\.html$|game\.html$|lab\.html$|404\.html$|articles\/|builders\/|badges\/)/;
911

1012
function get(k, d) { try { var v = localStorage.getItem(k); return v ? JSON.parse(v) : d; } catch (e) { return d; } }
1113
function put(k, v) { try { localStorage.setItem(k, JSON.stringify(v)); } catch (e) {} }
@@ -27,8 +29,7 @@
2729
var u;
2830
try { u = new URL(a.href, location.href); } catch (err) { return; }
2931
if (u.origin !== new URL(HUB).origin || u.pathname.indexOf(BOOK_PATH) === 0) return;
30-
var site = u.pathname.split("/")[1];
31-
site = site && !/\.html$/.test(site) ? site : "hub";
32+
var site = HUB_PAGES.test(u.pathname) ? "hub" : u.pathname.split("/")[1];
3233
var where = placement(a);
3334
if (!u.searchParams.has("utm_source")) {
3435
u.searchParams.set("utm_source", "webbook");
@@ -112,7 +113,7 @@
112113

113114
if (!main || idx < 0 || document.querySelector(".hero")) return;
114115
var ch = chapters[idx];
115-
put(KEY_LAST, { slug: ch.slug, t: Date.now() });
116+
put(KEY_LAST, { slug: ch.slug, label: label(ch), total: chapters.length, t: Date.now() });
116117

117118
// ---------- 장 첫머리: 분량, 위치, 노트북 ----------
118119
var header = document.getElementById("title-block-header");
@@ -172,6 +173,7 @@
172173
end.className = "cil-end";
173174
end.setAttribute("aria-label", "이 장을 마치며");
174175
end.innerHTML =
176+
'<p class="cil-end-stamp" hidden>처음으로 한 장을 끝까지 읽었습니다. 허브 활동 여권에 <b>웹북 독자</b> 스탬프가 찍혔어요. <a href="' + HUB + '#passport">여권 보기</a></p>' +
175177
'<div class="cil-end-row">' +
176178
'<div class="cil-end-vote" role="group" aria-label="이 장 평가">' +
177179
'<span class="q">이 장, 도움이 됐나요?</span>' +
@@ -221,6 +223,7 @@
221223
if (!b) return;
222224
var v = b.getAttribute("data-vote");
223225
if (votes[ch.slug] === v) return;
226+
votes = get(KEY_VOTE, {});
224227
votes[ch.slug] = v;
225228
put(KEY_VOTE, votes);
226229
paintVote(v);
@@ -230,12 +233,15 @@
230233
// 끝까지 내려왔고 30초 이상 머물렀으면 읽은 장으로 기록
231234
var start = Date.now(), timer = 0;
232235
function complete() {
236+
read = get(KEY_READ, {}); // 다른 탭에서 읽은 장을 덮어쓰지 않도록 다시 읽는다
233237
if (read[ch.slug]) return;
234238
read[ch.slug] = Date.now();
235239
put(KEY_READ, read);
236240
paintSidebar();
237241
paintProgress();
238-
track("chapter_complete", { chapter: ch.slug, chapter_index: idx + 1, read_count: readCount() });
242+
var n = readCount();
243+
if (n === 1) end.querySelector(".cil-end-stamp").hidden = false;
244+
track("chapter_complete", { chapter: ch.slug, chapter_index: idx + 1, read_count: n });
239245
}
240246
if ("IntersectionObserver" in window && !read[ch.slug]) {
241247
var io = new IntersectionObserver(function (entries) {

‎book/styles-dark.scss‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,3 +229,8 @@ body {
229229
border-color: #b4a5ea;
230230
color: #14151f;
231231
}
232+
233+
.cil-end-stamp {
234+
background: rgba(111, 207, 151, 0.1);
235+
border-color: rgba(111, 207, 151, 0.3);
236+
}

‎book/styles.scss‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,19 @@ main:has(.hero) #title-block-header {
659659
margin-top: 2.8rem;
660660
}
661661

662+
.cil-end-stamp {
663+
margin: 0 0 0.8rem;
664+
padding: 0.65rem 0.9rem;
665+
border-radius: 10px;
666+
background: rgba(#2b8a5a, 0.08);
667+
border: 1px solid rgba(#2b8a5a, 0.25);
668+
font-size: 0.9rem;
669+
}
670+
671+
.cil-end-stamp a {
672+
font-weight: 700;
673+
}
674+
662675
.cil-end-row {
663676
display: flex;
664677
flex-wrap: wrap;

0 commit comments

Comments
 (0)