Skip to content

Commit a1d66f2

Browse files
committed
feat(*): introduce Google Analytics
1 parent 7779f64 commit a1d66f2

File tree

6 files changed

+55
-4
lines changed

6 files changed

+55
-4
lines changed

chapters/_layouts/website/page.html

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,38 @@
66
<meta name="theme-color" content="#d0ab6b">
77
<link rel="preload" href="/assets/styles/page.css" as="style">
88
<link rel="stylesheet" href="/assets/styles/page.css">
9+
<script>
10+
window.__gaId__ = 'UA-25380313-6';
11+
12+
document.addEventListener('DOMContentLoaded', () => {
13+
const button = document.querySelector('#optout-button');
14+
const output = document.querySelector('#optout-status');
15+
16+
const optoutEnabledText = 'オプトアウトは有効になっています。';
17+
const optoutDisabledText = 'オプトアウトは無効になっています。';
18+
19+
gaOptout.gaId = window.__gaId__;
20+
21+
if (gaOptout.enabled()) {
22+
gaOptout.enable();
23+
output.textContent = optoutEnabledText;
24+
} else {
25+
output.textContent = optoutDisabledText;
26+
}
27+
28+
button.addEventListener('click', event => {
29+
if (gaOptout.enabled()) {
30+
gaOptout.disable();
31+
output.textContent = optoutDisabledText;
32+
} else {
33+
gaOptout.enable();
34+
output.textContent = optoutEnabledText;
35+
}
36+
});
37+
});
38+
</script>
39+
<script src="https://www.googletagmanager.com/gtag/js?id=UA-25380313-6" defer></script>
40+
<script src="/assets/scripts/gaInitialize.js" type="module"></script>
941
{% endblock %}
1042

1143
{% block body %}

chapters/assets/scripts/gaInitialize.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ function gaInitialize() {
1111
const optout = new GAOptout(storage);
1212

1313
window.gaOptout = optout || {};
14+
const gaId = window.__gaId__ || '';
1415

1516
if (optout.enabled()) {
1617
optout.gaId = gaId;
1718
optout.enable();
1819
}
1920

20-
const gaId = window.__gaId__ || '';
2121
gtag('js', new Date());
2222
gtag('config', gaId);
2323
}

chapters/assets/scripts/gaOptout.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ export class GAOptout {
3535
}
3636

3737
enabled() {
38-
return (
39-
window[this.gaOptoutKey] || this._storage.getKey() !== null || this._isDoNotTrackEnabled()
40-
);
38+
return window[this.gaOptoutKey] || this._storage.getKey() !== null || this._isDoNotTrackEnabled();
4139
}
4240

4341
_isDoNotTrackEnabled() {

chapters/assets/styles/page.css

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,10 @@ body,
6363
.navigation {
6464
top: calc(50px + 1em + 8px * 2);
6565
}
66+
67+
.optout-status {
68+
display: block;
69+
padding: 8px;
70+
border: 1px solid #000;
71+
margin-bottom: 1px;
72+
}

chapters/index.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,15 @@ Steins;Gitは「Gitの使い方を、Steins;Gateの世界観を使って説明
2424

2525
- Steins;Gateのアニメを全話見た、もしくはゲームで全てのエンディングを見た人
2626
- Gitを触れたことがない、もしくは、Gitを触り始めたばかりの人
27+
28+
## Google Analyticsについて
29+
30+
当サイトでは内容の改善などを目的として[Google アナリティクス](https://marketingplatform.google.com/intl/ja/about/analytics/)を使っています。
31+
32+
アクセス情報は匿名のトラフィックデータという形で収集・解析しています。
33+
34+
データ収集の状態を変更したい場合は以下のボタンから変更する事ができます。
35+
36+
<!-- textlint-disable web-plus-db -->
37+
{% include "./static/optout.html" %}
38+
<!-- textlint-enable web-plus-db -->

chapters/static/optout.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<button type="button" id="optout-button">アクセス解析の状態を変更する</button>
2+
<output name="optout-status" id="optout-status" class="optout-status" for="optout-button">オプトアウトは有効になっています。</output>

0 commit comments

Comments
 (0)