Skip to content

Commit dab8c93

Browse files
committed
Merge branch 'footer-js-and-ga4'
2 parents a2afa17 + e50682e commit dab8c93

File tree

3 files changed

+73
-17
lines changed

3 files changed

+73
-17
lines changed

app/javascript/application.js

+72-6
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,81 @@ function saveSettings(newSettings = {}) {
4040
window.settings = saveSettings();
4141
// 辭典設定初始化 END
4242

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+
4359
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+
});
4569

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 ) {
47105
$( "#select-dictionary-modal" ).on( "click", function() {
48106
window.location.href = "/";
49107
});
50108
}
51109

110+
// 根據 settings 設定辭典畫面 START
111+
// 如果是 /terms/:name,依據辭典白名單,把不顯示的 hide
112+
// 如果是 /dictionaries/:id/terms/:name,一律顯示內容
52113
if (
53-
(currentPath.indexOf( "/terms") !== -1 ) ||
54-
(currentPath.indexOf( "/bookmarks") !== -1 )
114+
( currentPath.indexOf( "/terms") !== -1 ) &&
115+
( currentPath.indexOf( "/dictionaries" ) === -1 )
55116
) {
56117

57-
// 根據 settings 設定辭典畫面 START
58118
$( "#dictionary-name" ).html( DICTIONARY[settings.mainDictionary] );
59119
Object.keys(DICTIONARY).forEach(function(el){
60120
if (!settings.displayList.includes(el)) {
@@ -67,7 +127,13 @@ document.addEventListener( "turbo:load", function() {
67127
$(this).prop( { checked: settings.displayList.includes($(this).val()) } );
68128
})
69129
$( `#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+
) {
71137

72138
// 搜尋功能 START
73139
$( "#search" ).autocomplete({

app/views/layouts/application.html.erb

-10
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,5 @@
119119
</div>
120120

121121
<div id="overlay" class="hidden opacity-50 transition-all duration-300 ease-in-out fixed top-0 left-0 z-[1040] bg-black w-screen h-screen"></div>
122-
123-
<!-- Google tag (gtag.js) -->
124-
<script async src="https://www.googletagmanager.com/gtag/js?id=G-VMCGN0EBM0"></script>
125-
<script>
126-
window.dataLayer = window.dataLayer || [];
127-
function gtag(){dataLayer.push(arguments);}
128-
gtag('js', new Date());
129-
130-
gtag('config', 'G-VMCGN0EBM0');
131-
</script>
132122
</body>
133123
</html>

app/views/terms/show.html.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
<div class="mt-4 space-y-4">
5454
<% @terms.each do |term| %>
5555
<% dictionary = term.dictionary %>
56-
<div id="dictionary-<%= dictionary.id %>">
56+
<div id="dictionary-<%= dictionary.id %>" class="dictionaries">
5757
<div class="bg-<%= term.dictionary.color %>-600 text-white p-2"><%= term.dictionary.name %></div>
5858

5959
<div class="term-into">

0 commit comments

Comments
 (0)