Skip to content

Commit 1c14bf3

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

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

build/build.js

Lines changed: 1 addition & 0 deletions
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

Lines changed: 23 additions & 1 deletion
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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
15+
gtag = function() {dataLayer.push(arguments)};
16+
gtag('js', new Date());
17+
18+
gtag('config', id);
19+
}
20+
21+
function collect() {
22+
if (typeof gtag === 'undefined') {
23+
init($docsify.gtag);
24+
}
25+
26+
gtag('event', 'page_view', {
27+
page_title: location.hash
28+
});
29+
}
30+
31+
const install = function (hook) {
32+
if (!$docsify.gtag) {
33+
console.error('[Docsify] gtag is required.');
34+
return;
35+
}
36+
37+
hook.beforeEach(collect);
38+
};
39+
40+
$docsify.plugins = [].concat(install, $docsify.plugins);

0 commit comments

Comments
 (0)