Skip to content

Commit 8779b22

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

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-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: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
send_page_view: false, // Disable automatic pageview
20+
debug_mode: (location.hostname === 'localhost') ? true : false
21+
});
22+
}
23+
24+
function collect() {
25+
if (typeof gtag === 'undefined') {
26+
init($docsify.gtag);
27+
}
28+
29+
gtag('event', 'page_view', {
30+
page_title: location.hash,
31+
page_location: location.href
32+
});
33+
}
34+
35+
const install = function (hook) {
36+
if (!$docsify.gtag) {
37+
console.error('[Docsify] gtag is required.');
38+
return;
39+
}
40+
41+
hook.beforeEach(collect);
42+
};
43+
44+
$docsify.plugins = [].concat(install, $docsify.plugins);

0 commit comments

Comments
 (0)