Skip to content

Commit d7f57b3

Browse files
committed
📈 Add analytics or track code.
1 parent 619b4e9 commit d7f57b3

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

js/lib/analytics.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/* global gtag */
2+
export function initAnalytics() {
3+
/**
4+
* GA4 Custom Event Sending Function
5+
*/
6+
function sendGAEvent(eventName, eventParams = {}) {
7+
if (typeof gtag !== 'undefined') {
8+
gtag('event', eventName, eventParams);
9+
}
10+
}
11+
12+
13+
/**
14+
* Get configure events
15+
*/
16+
document.querySelector('#configure')?.addEventListener('click', function () {
17+
sendGAEvent('configure', {
18+
'event_category': 'engagement',
19+
'event_label': 'configure_button'
20+
});
21+
});
22+
23+
/**
24+
* Get help events
25+
*/
26+
document.querySelector('#help_button')?.addEventListener('click', function () {
27+
sendGAEvent('help', {
28+
'event_category': 'engagement',
29+
'event_label': 'help_button'
30+
});
31+
});
32+
33+
/**
34+
* Get export events
35+
*/
36+
document.querySelector('#download_formatted_log')?.addEventListener('click', function () {
37+
sendGAEvent('export_log', {
38+
'event_category': 'engagement',
39+
'event_label': 'export'
40+
});
41+
});
42+
43+
/**
44+
* Get PWA install events
45+
*/
46+
window.addEventListener('appinstalled', () => {
47+
sendGAEvent('pwa_install', {
48+
'event_category': 'engagement',
49+
'event_label': 'install_pwa'
50+
});
51+
});
52+
}

js/main.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { $$one, $$all, $$disableConsole } from './lib/indolence.min.js';
22
import { LOG_DATA_KEY, ROUNDING_UNIT_MINUTE_KEY, trimNewLine, appendTime, installPWA, autoSetTheme } from './lib/utils.js';
33
import Multilingualization from './lib/multilingualization.js';
44
import { downloadLog, generateFormattedLog } from './lib/download.js';
5+
import { initAnalytics } from './lib/analytics.js';
56

67
/* global bootstrap */
78

@@ -58,6 +59,7 @@ function saveLogs() {
5859
*/
5960
document.addEventListener('DOMContentLoaded', async () => {
6061
$$disableConsole();
62+
initAnalytics();
6163

6264
// Set the theme automatically
6365
autoSetTheme();

0 commit comments

Comments
 (0)