@@ -40,21 +40,81 @@ function saveSettings(newSettings = {}) {
40
40
window . settings = saveSettings ( ) ;
41
41
// 辭典設定初始化 END
42
42
43
+ // GA 初始化 START
44
+ var hostnameForGA = 'new-amis.moedict.tw' ;
45
+ // hostnameForGA = 'new-amis.moedict.test'; // 開發端測試時拿掉這行註解
46
+
47
+ window . dataLayer = window . dataLayer || [ ] ;
48
+ function gtag ( ) { dataLayer . push ( arguments ) ; }
49
+ gtag ( 'js' , new Date ( ) ) ;
50
+
51
+ if ( window . location . hostname == hostnameForGA ) {
52
+ // 關閉自動 send_page_view,拉到 turbo:load 裡面
53
+ gtag ( 'config' , 'G-VMCGN0EBM0' , {
54
+ send_page_view : false
55
+ } ) ;
56
+ }
57
+ // GA 初始化 END
58
+
43
59
document . addEventListener ( "turbo:load" , function ( ) {
44
- var currentPath = window . location . pathname ;
60
+ var currentPath = window . location . pathname ,
61
+ currentLocation = window . location . href ;
62
+
63
+ if ( window . location . hostname == hostnameForGA ) {
64
+ // GA 的 send_page_view,放 turbo:load 事件內,切換字詞時才會送
65
+ gtag ( 'event' , 'page_view' , {
66
+ page_title : document . title ,
67
+ page_location : currentLocation
68
+ } ) ;
45
69
46
- if ( currentPath . indexOf ( "/about" ) !== - 1 ) {
70
+ // 送 GA 事件,紀錄單本字典的瀏覽狀況 START
71
+ if ( currentPath . indexOf ( "/dictionaries" ) === 0 ) {
72
+ var dictionaryId = $ ( ".dictionaries " ) [ 0 ] . id . split ( '-' ) [ 1 ] ,
73
+ termName = currentPath . split ( '/' ) . pop ( ) ;
74
+
75
+ gtag ( 'event' , 'dictionary_term' , {
76
+ page_title : document . title ,
77
+ page_location : currentLocation ,
78
+ dictionary : dictionaryId ,
79
+ term : termName
80
+ } ) ;
81
+ }
82
+ // 送 GA 事件,紀錄單本字典的瀏覽狀況 END
83
+
84
+ // 送 GA 事件,紀錄單詞的瀏覽狀況,且排除不顯示的字典 START
85
+ if ( currentPath . indexOf ( "/terms" ) === 0 ) {
86
+ $ ( ".dictionaries " ) . each ( function ( _ , el ) {
87
+ var dictionaryId = el . id . split ( '-' ) [ 1 ] ,
88
+ termName = currentPath . split ( '/' ) . pop ( ) ;
89
+
90
+ if ( settings . displayList . includes ( dictionaryId ) ) {
91
+ gtag ( 'event' , 'dictionary_term' , {
92
+ page_title : document . title ,
93
+ page_location : currentLocation ,
94
+ dictionary : dictionaryId ,
95
+ term : termName
96
+ } ) ;
97
+ }
98
+ } ) ;
99
+ }
100
+ // 送 GA 事件,紀錄單詞的瀏覽狀況,且排除不顯示的字典 START
101
+ }
102
+
103
+ // 在 /about 頁面時,點字典名稱會跳回首頁,然後自動轉向最後查找的頁面
104
+ if ( currentPath . indexOf ( "/about" ) !== - 1 ) {
47
105
$ ( "#select-dictionary-modal" ) . on ( "click" , function ( ) {
48
106
window . location . href = "/" ;
49
107
} ) ;
50
108
}
51
109
110
+ // 根據 settings 設定辭典畫面 START
111
+ // 如果是 /terms/:name,依據辭典白名單,把不顯示的 hide
112
+ // 如果是 /dictionaries/:id/terms/:name,一律顯示內容
52
113
if (
53
- ( currentPath . indexOf ( "/terms" ) !== - 1 ) ||
54
- ( currentPath . indexOf ( "/bookmarks" ) ! == - 1 )
114
+ ( currentPath . indexOf ( "/terms" ) !== - 1 ) &&
115
+ ( currentPath . indexOf ( "/dictionaries" ) = == - 1 )
55
116
) {
56
117
57
- // 根據 settings 設定辭典畫面 START
58
118
$ ( "#dictionary-name" ) . html ( DICTIONARY [ settings . mainDictionary ] ) ;
59
119
Object . keys ( DICTIONARY ) . forEach ( function ( el ) {
60
120
if ( ! settings . displayList . includes ( el ) ) {
@@ -67,7 +127,13 @@ document.addEventListener( "turbo:load", function() {
67
127
$ ( this ) . prop ( { checked : settings . displayList . includes ( $ ( this ) . val ( ) ) } ) ;
68
128
} )
69
129
$ ( `#display-dictionary input[value="${ settings . mainDictionary } "]` ) . prop ( { checked : "checked" , disabled : "disabled" } ) ;
70
- // 根據 settings 設定辭典畫面 END
130
+ }
131
+ // 根據 settings 設定辭典畫面 END
132
+
133
+ if (
134
+ ( currentPath . indexOf ( "/terms" ) !== - 1 ) ||
135
+ ( currentPath . indexOf ( "/bookmarks" ) !== - 1 )
136
+ ) {
71
137
72
138
// 搜尋功能 START
73
139
$ ( "#search" ) . autocomplete ( {
0 commit comments