Skip to content

Commit 456f844

Browse files
committed
feat: add google universal analytics gtag.js plugin
1 parent 3410580 commit 456f844

File tree

3 files changed

+9
-19
lines changed

3 files changed

+9
-19
lines changed

docs/plugins.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,12 @@ Install the plugin and configure the track id.
7575

7676
```html
7777
<script>
78+
// Single ID
7879
window.$docsify = {
7980
ga: 'UA-XXXXX-Y',
8081
};
8182
82-
// or multi gtag, first tag at the beginning of G- from the array is global site gtag, each page should have only one global site tag instance.
83+
// Multiple IDs
8384
window.$docsify = {
8485
ga: [
8586
'G-XXXXXXXX', // Google Analytics 4 (GA4)

src/plugins/ga.js

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
/* eslint-disable no-console */
22
// From https://github.com/egoist/vue-ga/blob/master/src/index.js
33

4-
// prefix of global site tag
5-
const GLOBAL_SITE_TAG_PREFIX = 'G-';
6-
74
function appendScript(id) {
85
const script = document.createElement('script');
96
script.async = true;
@@ -34,24 +31,15 @@ function initAdditionalTag(id) {
3431

3532
function init(ids) {
3633
if (Array.isArray(ids)) {
37-
// default get the first id
38-
let globalSiteTag = ids[0];
39-
let globalSiteTagArr = ids.filter(
40-
id => id.indexOf(GLOBAL_SITE_TAG_PREFIX) === 0
41-
);
42-
if (globalSiteTagArr.length !== 0) {
43-
globalSiteTag = globalSiteTagArr[0];
44-
}
45-
46-
// initialized global site tag id
47-
initGlobalSiteTag(globalSiteTag);
34+
// set the first id to be a global site tag
35+
initGlobalSiteTag(ids[0]);
4836

4937
// the rest ids
50-
ids
51-
.filter(id => id !== globalSiteTag)
52-
.forEach((id, index) => {
38+
ids.forEach((id, index) => {
39+
if (index > 0) {
5340
initAdditionalTag(id);
54-
});
41+
}
42+
});
5543
} else {
5644
initGlobalSiteTag(ids);
5745
}

test/e2e/ga.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Modules, constants, and variables
2+
// npm run test:e2e ga.test.js
23
// -----------------------------------------------------------------------------
34
const docsifyInit = require('../helpers/docsify-init');
45
const { test, expect } = require('./fixtures/docsify-init-fixture');

0 commit comments

Comments
 (0)