Skip to content

Commit e819976

Browse files
committed
docs: fix google analytics
1 parent cbaf7d5 commit e819976

File tree

2 files changed

+74
-3
lines changed

2 files changed

+74
-3
lines changed

docs/_assets/gtag.js

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/* eslint-disable no-console */
2+
// From ./ga.js
3+
4+
function appendScript(id) {
5+
const script = document.createElement('script');
6+
script.async = true;
7+
script.src = 'https://www.googletagmanager.com/gtag/js?id=' + id;
8+
document.body.appendChild(script);
9+
}
10+
11+
// global site tag instance initialized
12+
function initGlobalSiteTag(id) {
13+
appendScript(id);
14+
15+
window.dataLayer = window.dataLayer || [];
16+
window.gtag =
17+
window.gtag ||
18+
function () {
19+
window.dataLayer.push(arguments);
20+
};
21+
22+
window.gtag('js', new Date());
23+
window.gtag('config', id);
24+
}
25+
26+
// add additional products to your tag
27+
// https://developers.google.com/tag-platform/gtagjs/install
28+
function initAdditionalTag(id) {
29+
window.gtag('config', id);
30+
}
31+
32+
function init(ids) {
33+
if (Array.isArray(ids)) {
34+
// set the first id to be a global site tag
35+
initGlobalSiteTag(ids[0]);
36+
37+
// the rest ids
38+
ids.forEach((id, index) => {
39+
if (index > 0) {
40+
initAdditionalTag(id);
41+
}
42+
});
43+
} else {
44+
initGlobalSiteTag(ids);
45+
}
46+
}
47+
48+
function collect() {
49+
if (!window.gtag) {
50+
init($docsify.gtag);
51+
}
52+
53+
// usage: https://developers.google.com/analytics/devguides/collection/gtagjs/pages
54+
window.gtag('event', 'page_view', {
55+
/* eslint-disable camelcase */
56+
page_title: document.title,
57+
page_location: location.href,
58+
page_path: location.pathname,
59+
/* eslint-disable camelcase */
60+
});
61+
}
62+
63+
const install = function (hook) {
64+
if (!$docsify.gtag) {
65+
console.error('[Docsify] gtag is required.');
66+
return;
67+
}
68+
69+
hook.beforeEach(collect);
70+
};
71+
72+
$docsify.plugins = [].concat(install, $docsify.plugins);

docs/index.html

+2-3
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,14 @@
2020
loadSidebar: true,
2121
search: 'auto',
2222
themeColor: '#E57A44',
23-
ga: 'G-4W227SRJTC'
23+
gtag: 'G-4W227SRJTC'
2424
}
2525
</script>
2626
<!-- Docsify v4 -->
2727
<script src="//cdn.jsdelivr.net/npm/docsify@4"></script>
2828
<script src="//cdn.jsdelivr.net/npm/docsify-copy-code/dist/docsify-copy-code.min.js"></script>
2929
<script src="//cdn.jsdelivr.net/npm/[email protected]/components/prism-json.min.js"></script>
30-
<script src="//cdn.jsdelivr.net/npm/docsify/lib/plugins/ga.min.js"></script>
30+
<script src="_assets/gtag.js"></script>
3131
<script src="_assets/prism-rpa.js"></script>
32-
<!--<script src="//cdn.jsdelivr.net/npm/docsify-darklight-theme@latest/dist/index.min.js" type="text/javascript"></script>-->
3332
</body>
3433
</html>

0 commit comments

Comments
 (0)