Skip to content

Commit c399f3c

Browse files
committed
test: IIFE
1 parent 634b948 commit c399f3c

File tree

2 files changed

+63
-62
lines changed

2 files changed

+63
-62
lines changed

build/build.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ async function buildCore() {
7373
build({
7474
input: 'src/core/index.js',
7575
output: 'docsify.min.js',
76-
// plugins: [uglify()]
76+
plugins: [uglify()],
7777
})
7878
);
7979
}
@@ -108,7 +108,7 @@ async function buildAllPlugin() {
108108
build({
109109
input: 'src/plugins/' + item.input,
110110
output: 'plugins/' + item.name + '.min.js',
111-
// plugins: [uglify()]
111+
plugins: [uglify()],
112112
})
113113
);
114114
});

src/plugins/gtag.js

Lines changed: 61 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,73 @@
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-
};
1+
(function () {
2+
function appendScript(id) {
3+
const script = document.createElement('script');
4+
script.async = true;
5+
script.src = 'https://www.googletagmanager.com/gtag/js?id=' + id;
6+
document.body.appendChild(script);
7+
}
218

22-
window.gtag('js', new Date());
23-
window.gtag('config', id);
24-
}
9+
// global site tag instance initialized
10+
function initGlobalSiteTag(id) {
11+
appendScript(id);
2512

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-
}
13+
window.dataLayer = window.dataLayer || [];
14+
window.gtag =
15+
window.gtag ||
16+
function () {
17+
window.dataLayer.push(arguments);
18+
};
3119

32-
function init(ids) {
33-
if (Array.isArray(ids)) {
34-
// set the first id to be a global site tag
35-
initGlobalSiteTag(ids[0]);
20+
window.gtag('js', new Date());
21+
window.gtag('config', id);
22+
}
3623

37-
// the rest ids
38-
ids.forEach((id, index) => {
39-
if (index > 0) {
40-
initAdditionalTag(id);
41-
}
42-
});
43-
} else {
44-
initGlobalSiteTag(ids);
24+
// add additional products to your tag
25+
// https://developers.google.com/tag-platform/gtagjs/install
26+
function initAdditionalTag(id) {
27+
window.gtag('config', id);
4528
}
46-
}
4729

48-
function collect() {
49-
if (!window.gtag) {
50-
init($docsify.gtag);
30+
function init(ids) {
31+
if (Array.isArray(ids)) {
32+
// set the first id to be a global site tag
33+
initGlobalSiteTag(ids[0]);
34+
35+
// the rest ids
36+
ids.forEach((id, index) => {
37+
if (index > 0) {
38+
initAdditionalTag(id);
39+
}
40+
});
41+
} else {
42+
initGlobalSiteTag(ids);
43+
}
5144
}
5245

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-
}
46+
function collect() {
47+
if (!window.gtag) {
48+
init($docsify.gtag);
49+
}
6250

63-
const install = function (hook) {
64-
if (!$docsify.gtag) {
65-
console.error('[Docsify] gtag is required.');
66-
return;
51+
// usage: https://developers.google.com/analytics/devguides/collection/gtagjs/pages
52+
window.gtag('event', 'page_view', {
53+
/* eslint-disable camelcase */
54+
page_title: document.title,
55+
page_location: location.href,
56+
page_path: location.pathname,
57+
/* eslint-disable camelcase */
58+
});
6759
}
60+
const gtagPlugin = function (hook, vm) {
61+
if (!$docsify.gtag) {
62+
/* eslint-disable-next-line no-console */
63+
console.error('[Docsify] gtag is required.');
64+
return;
65+
}
6866

69-
hook.beforeEach(collect);
70-
};
67+
hook.beforeEach(collect);
68+
};
7169

72-
$docsify.plugins = [].concat(install, $docsify.plugins);
70+
// Add plugin to docsify's plugin array
71+
$docsify = $docsify || {};
72+
$docsify.plugins = [].concat($docsify.plugins || [], gtagPlugin);
73+
})();

0 commit comments

Comments
 (0)