Skip to content

Commit f581dba

Browse files
add CustomEvents
Update main.js Update sw.js Update main.js Update index.html Update sw.js
1 parent 6825c78 commit f581dba

File tree

3 files changed

+82
-16
lines changed

3 files changed

+82
-16
lines changed

assets/js/main.js

+80-14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,65 @@
1+
class CustomEvents {
2+
static triggerService(serviceName) {
3+
gtag('event', 'use_service', {
4+
event_category: 'Service Interactions',
5+
event_label: serviceName,
6+
value: 1,
7+
});
8+
console.log("[GA] Triggered 'use_service' event for service:", serviceName);
9+
}
10+
11+
static convertServiceError(serviceName, errorMessage) {
12+
gtag('event', 'service_error', {
13+
event_category: 'Service Errors',
14+
event_label: serviceName,
15+
value: 1,
16+
error_message: errorMessage,
17+
});
18+
console.log("[GA] Triggered 'service_error' event for service:", serviceName);
19+
}
20+
21+
static selectServiceTriggered(serviceName) {
22+
gtag('event', 'select_service', {
23+
event_category: 'Service Interactions',
24+
event_label: serviceName,
25+
value: 1,
26+
});
27+
console.log("[GA] Triggered 'select_service' event for service:", serviceName);
28+
}
29+
30+
static triggerCopyOutput() {
31+
gtag('event', 'copy_output', {
32+
event_category: 'Output Actions',
33+
event_label: 'Copy Output',
34+
value: 1,
35+
});
36+
}
37+
38+
static triggerShareOutput() {
39+
gtag('event', 'share_output', {
40+
event_category: 'Output Actions',
41+
event_label: 'Share Output',
42+
value: 1,
43+
});
44+
}
45+
46+
static triggerInstallPWA() {
47+
gtag('event', 'install_pwa', {
48+
event_category: 'PWA Actions',
49+
event_label: 'Install PWA',
50+
value: 1,
51+
});
52+
}
53+
54+
static triggerAboutUs() {
55+
gtag('event', 'about_us', {
56+
event_category: 'About Us',
57+
event_label: 'About Us',
58+
value: 1,
59+
});
60+
}
61+
}
62+
163
const SERVICES = [
264
{
365
title: 'الرسم العربي القديم',
@@ -115,6 +177,8 @@ function selectService(functionName, id) {
115177

116178
// save last selected service
117179
localStorage.setItem('lastSelectedService', functionName);
180+
181+
CustomEvents.selectServiceTriggered(selectedServiceFunction);
118182
}
119183

120184
function convert() {
@@ -123,20 +187,13 @@ function convert() {
123187
outputTextArea.value = '';
124188
return;
125189
}
126-
const url = `https://arabic-services-api-v1.onrender.com/services?method=${selectedServiceFunction}`;
127-
fetch(url, {
128-
method: 'POST',
129-
body: JSON.stringify({ input: inputText }),
130-
headers: { 'Content-Type': 'application/json' },
131-
})
132-
.then((response) => response.text())
133-
.then((data) => {
134-
outputTextArea.value = data;
135-
})
136-
.catch((error) => {
137-
console.error('Error:', error);
138-
outputTextArea.value = ArabicServices[selectedServiceFunction](inputText);
139-
});
190+
try {
191+
outputTextArea.value = ArabicServices[selectedServiceFunction](inputText);
192+
CustomEvents.triggerService(selectedServiceFunction);
193+
} catch (error) {
194+
console.error(error);
195+
CustomEvents.convertServiceError(selectedServiceFunction, error.message);
196+
}
140197
}
141198

142199
document.addEventListener('DOMContentLoaded', function () {
@@ -175,6 +232,7 @@ slider.addEventListener('mouseleave', mouseleave);
175232
slider.addEventListener('mouseup', () => {
176233
if (!isDrag) {
177234
selectService(selectedServiceFunction, selectedSlideId);
235+
console.log('when mouseup');
178236
}
179237
mouseleave();
180238
});
@@ -205,6 +263,8 @@ function copyOutput() {
205263
showConfirmButton: false,
206264
timer: 1500,
207265
});
266+
267+
CustomEvents.triggerCopyOutput();
208268
}
209269

210270
// --------------------- Share -----------------------------
@@ -214,6 +274,8 @@ function shareOutput() {
214274
return;
215275
}
216276

277+
CustomEvents.triggerShareOutput();
278+
217279
// Check if the Web Share API is available
218280
if (navigator.share) {
219281
navigator
@@ -275,6 +337,8 @@ function downloadHandlerForPWA() {
275337
}
276338
deferredPrompt = null;
277339
});
340+
341+
CustomEvents.triggerInstallPWA();
278342
}
279343

280344
window.addEventListener('appinstalled', () => {
@@ -315,6 +379,8 @@ function promptInstallPWA() {
315379
// --------------------- About Us -----------------------------
316380

317381
function showAboutUs() {
382+
CustomEvents.triggerAboutUs();
383+
318384
Swal.fire({
319385
title: 'من نحن',
320386
width: '90%',

index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ <h2>للتواصل والاقتراحات</h2>
182182
}
183183
gtag('js', new Date());
184184

185-
gtag('config', 'G-5ZS6R0YVEF');
185+
gtag('config', 'G-5ZS6R0YVEF', { debug_mode: true });
186186
</script>
187187
<script>
188188
window.addEventListener('load', () => {

sw.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const version = '2.0.1-' + new Date().getTime();
1+
const version = '2.0.3-' + new Date().getTime();
22
const CACHE_NAME = `services-v${version}`;
33
const staticAssets = [
44
'./',

0 commit comments

Comments
 (0)