|
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 | + } |
21 | 8 |
|
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); |
25 | 12 |
|
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 | + }; |
31 | 19 |
|
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 | + } |
36 | 23 |
|
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); |
45 | 28 | }
|
46 |
| -} |
47 | 29 |
|
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 | + } |
51 | 44 | }
|
52 | 45 |
|
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 | + } |
62 | 50 |
|
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 | + }); |
67 | 59 | }
|
| 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 | + } |
68 | 66 |
|
69 |
| - hook.beforeEach(collect); |
70 |
| -}; |
| 67 | + hook.beforeEach(collect); |
| 68 | + }; |
71 | 69 |
|
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