Skip to content

Commit 52c3bd0

Browse files
committed
Migrate Universal Analytics to Google Analytics 4
1 parent 21d71fe commit 52c3bd0

File tree

3 files changed

+26
-20
lines changed

3 files changed

+26
-20
lines changed

book.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
"root": "_src",
33
"title": "Serde",
44
"language": "en",
5-
"plugins": ["-search", "-sharing", "sane-sidebar", "codeblock-omit", "codeblock-rust", "edit-link", "ga", "bulk-redirect", "checklist", "anchors"],
5+
"plugins": ["-search", "-sharing", "sane-sidebar", "codeblock-omit", "codeblock-rust", "edit-link", "ga4", "bulk-redirect", "checklist", "anchors"],
66
"pluginsConfig": {
77
"edit-link": {
88
"base": "https://github.com/serde-rs/serde-rs.github.io/edit/master/_src",
99
"label": "Edit"
1010
},
11-
"ga": {
12-
"token": "UA-82804814-1"
11+
"ga4": {
12+
"tag": "G-T5QD1R7FZW",
13+
"anonymize_ip": true
1314
},
1415
"bulk-redirect": {
1516
"redirectsFile": "redirects.json"

gitbook/gitbook-plugin-ga/plugin.js

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
require(["gitbook"], function(gitbook) {
2+
gitbook.events.bind("start", function(e, config) {
3+
window.dataLayer = window.dataLayer || [];
4+
window.gtag = window.gtag || function() {
5+
window.dataLayer.push(arguments);
6+
};
7+
window.gtag('js', new Date());
8+
window.gtag('config', config.ga4.tag, {
9+
'anonymize_ip': config.ga4.anonymize_ip ?? false
10+
});
11+
var script = document.createElement("script");
12+
script.type = "text/javascript";
13+
script.async = true;
14+
script.src = "https://www.googletagmanager.com/gtag/js?id=" + config.ga4.tag;
15+
document.getElementsByTagName("head")[0].appendChild(script);
16+
});
17+
gitbook.events.bind("page.change", function() {
18+
window.gtag('event', 'page_view', {
19+
'page_location': window.location.pathname+window.location.search
20+
});
21+
});
22+
});

0 commit comments

Comments
 (0)