Skip to content

Commit 449d00a

Browse files
committed
test: IIFE (ga & gtag)
1 parent c399f3c commit 449d00a

File tree

1 file changed

+36
-32
lines changed

1 file changed

+36
-32
lines changed

src/plugins/ga.js

+36-32
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,44 @@
1-
/* eslint-disable no-console */
2-
// From https://github.com/egoist/vue-ga/blob/master/src/index.js
3-
function appendScript() {
4-
const script = document.createElement('script');
5-
script.async = true;
6-
script.src = 'https://www.google-analytics.com/analytics.js';
7-
document.body.appendChild(script);
8-
}
9-
10-
function init(id) {
11-
appendScript();
12-
window.ga =
13-
window.ga ||
14-
function () {
15-
(window.ga.q = window.ga.q || []).push(arguments);
16-
};
1+
(function () {
2+
// From https://github.com/egoist/vue-ga/blob/master/src/index.js
3+
function appendScript() {
4+
const script = document.createElement('script');
5+
script.async = true;
6+
script.src = 'https://www.google-analytics.com/analytics.js';
7+
document.body.appendChild(script);
8+
}
179

18-
window.ga.l = Number(new Date());
19-
window.ga('create', id, 'auto');
20-
}
10+
function init(id) {
11+
appendScript();
12+
window.ga =
13+
window.ga ||
14+
function () {
15+
(window.ga.q = window.ga.q || []).push(arguments);
16+
};
2117

22-
function collect() {
23-
if (!window.ga) {
24-
init($docsify.ga);
18+
window.ga.l = Number(new Date());
19+
window.ga('create', id, 'auto');
2520
}
2621

27-
window.ga('set', 'page', location.hash);
28-
window.ga('send', 'pageview');
29-
}
22+
function collect() {
23+
if (!window.ga) {
24+
init($docsify.ga);
25+
}
3026

31-
const install = function (hook) {
32-
if (!$docsify.ga) {
33-
console.error('[Docsify] ga is required.');
34-
return;
27+
window.ga('set', 'page', location.hash);
28+
window.ga('send', 'pageview');
3529
}
3630

37-
hook.beforeEach(collect);
38-
};
31+
const gaPlugin = function (hook, vm) {
32+
if (!$docsify.ga) {
33+
/* eslint-disable-next-line no-console */
34+
console.error('[Docsify] ga is required.');
35+
return;
36+
}
37+
38+
hook.beforeEach(collect);
39+
};
3940

40-
$docsify.plugins = [].concat(install, $docsify.plugins);
41+
// Add plugin to docsify's plugin array
42+
$docsify = $docsify || {};
43+
$docsify.plugins = [].concat($docsify.plugins || [], gaPlugin);
44+
})();

0 commit comments

Comments
 (0)