Skip to content

Commit 16e306b

Browse files
committed
New Google Analytics 4 plugin
1 parent 81fc1b7 commit 16e306b

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

build/build.js

+1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ async function buildAllPlugin() {
8080
var plugins = [
8181
{name: 'search', input: 'search/index.js'},
8282
{name: 'ga', input: 'ga.js'},
83+
{name: 'ga4', input: 'ga4.js'},
8384
{name: 'matomo', input: 'matomo.js'},
8485
{name: 'emoji', input: 'emoji.js'},
8586
{name: 'external-script', input: 'external-script.js'},

docs/plugins.md

+23-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ This plugin ignores diacritical marks when performing a full text search (e.g.,
6969
<script src="//polyfill.io/v3/polyfill.min.js?features=String.prototype.normalize"></script>
7070
```
7171

72-
## Google Analytics
72+
## Google Universal Analytics (GA3)
7373

7474
Install the plugin and configure the track id.
7575

@@ -91,6 +91,28 @@ Configure by `data-ga`.
9191
<script src="//cdn.jsdelivr.net/npm/docsify/lib/plugins/ga.min.js"></script>
9292
```
9393

94+
## Google Analytics 4 (GA4)
95+
96+
Install the plugin and configure the gtag id.
97+
98+
```html
99+
<script>
100+
window.$docsify = {
101+
gtag: 'G-XXXXXXXXX',
102+
};
103+
</script>
104+
<script src="//cdn.jsdelivr.net/npm/docsify/lib/docsify.min.js"></script>
105+
<script src="//cdn.jsdelivr.net/npm/docsify/lib/plugins/ga4.min.js"></script>
106+
```
107+
108+
Configure by `data-gtag`.
109+
110+
<!-- prettier-ignore -->
111+
```html
112+
<script src="//cdn.jsdelivr.net/npm/docsify/lib/docsify.min.js" data-gtag="G-XXXXXXXXX"></script>
113+
<script src="//cdn.jsdelivr.net/npm/docsify/lib/plugins/ga.min.js"></script>
114+
```
115+
94116
## Emoji
95117

96118
Renders a larger collection of emoji shorthand codes. Without this plugin, Docsify is able to render only a limited number of emoji shorthand codes.

src/plugins/ga4.js

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/* eslint-disable no-console */
2+
// Inspired by https://github.com/egoist/vue-ga/blob/master/src/index.js
3+
function appendScript(id) {
4+
const script = document.createElement('script');
5+
script.async = true;
6+
script.src = 'https://www.googletagmanager.com/gtag/js?id='+id;
7+
document.body.appendChild(script);
8+
}
9+
10+
function init(id) {
11+
appendScript(id);
12+
13+
window.dataLayer = window.dataLayer || [];
14+
function gtag(){dataLayer.push(arguments);}
15+
gtag('js', new Date());
16+
17+
gtag('config', id);
18+
}
19+
20+
function collect() {
21+
if (typeof gtag === 'undefined') {
22+
init($docsify.gtag);
23+
}
24+
25+
gtag('event', 'screen_view', {
26+
'screen_name': location.hash
27+
});
28+
}
29+
30+
const install = function (hook) {
31+
if (!$docsify.gtag) {
32+
console.error('[Docsify] gtag is required.');
33+
return;
34+
}
35+
36+
hook.beforeEach(collect);
37+
};
38+
39+
$docsify.plugins = [].concat(install, $docsify.plugins);

0 commit comments

Comments
 (0)