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
+ } ;
21
+
22
+ window . gtag ( 'js' , new Date ( ) ) ;
23
+ window . gtag ( 'config' , id ) ;
24
+ }
25
+
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
+ }
31
+
32
+ function init ( ids ) {
33
+ if ( Array . isArray ( ids ) ) {
34
+ // set the first id to be a global site tag
35
+ initGlobalSiteTag ( ids [ 0 ] ) ;
36
+
37
+ // the rest ids
38
+ ids . forEach ( ( id , index ) => {
39
+ if ( index > 0 ) {
40
+ initAdditionalTag ( id ) ;
41
+ }
42
+ } ) ;
43
+ } else {
44
+ initGlobalSiteTag ( ids ) ;
45
+ }
46
+ }
47
+
48
+ function collect ( ) {
49
+ if ( ! window . gtag ) {
50
+ init ( $docsify . gtag ) ;
51
+ }
52
+
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
+ }
62
+
63
+ const install = function ( hook ) {
64
+ if ( ! $docsify . gtag ) {
65
+ console . error ( '[Docsify] gtag is required.' ) ;
66
+ return ;
67
+ }
68
+
69
+ hook . beforeEach ( collect ) ;
70
+ } ;
71
+
72
+ $docsify . plugins = [ ] . concat ( install , $docsify . plugins ) ;
0 commit comments