Skip to content

Commit 56ccb08

Browse files
committed
chore: Enhance 404 page and analytics integration
- Refactored 404.js to integrate analytics tracking with `trackEvent`. - Updated analytics.js to add a reusable `trackEvent` function for event tracking. - Improved event tracking on index.js for buttons, social links, and copy-link functionality. - Renamed and standardized CSS class names in 404.scss and 404.ejs for consistency. - Added lazy loading for images on the 404 page and optimized clipboard functionality.
1 parent 4b19120 commit 56ccb08

File tree

5 files changed

+55
-38
lines changed

5 files changed

+55
-38
lines changed

src/js/404.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
11
import '../scss/404.scss';
2-
import "./analytics.js";
2+
import { trackEvent } from "./analytics.js";
3+
4+
document.addEventListener('DOMContentLoaded', () => {
5+
document.querySelectorAll('.clazz-error-container a').forEach(a => {
6+
a.addEventListener('click', (e) => {
7+
trackEvent("click", a);
8+
});
9+
});
10+
});

src/js/analytics.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { initializeApp } from "firebase/app";
2-
import { getAnalytics } from "firebase/analytics";
2+
import { getAnalytics, logEvent } from "firebase/analytics";
33

44
// See: https://firebase.google.com/docs/web/learn-more#config-object
55
const firebaseConfig = {
@@ -16,4 +16,16 @@ const firebaseConfig = {
1616
const app = initializeApp(firebaseConfig);
1717

1818
// Initialize Analytics and get a reference to the service
19-
export const analytics = getAnalytics(app);
19+
const analytics = getAnalytics(app);
20+
21+
export function trackEvent(event, element, additionalData = {}) {
22+
const eventName = element.getAttribute('aria-label') || obj.innerText || obj.textContent;
23+
const eventData = {
24+
name: eventName,
25+
page_location: window.location.href,
26+
page_referrer: document.referrer,
27+
page_title: document.title,
28+
...additionalData,
29+
};
30+
logEvent(analytics, event, eventData);
31+
}

src/js/index.js

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,36 @@
11
import '../scss/index.scss';
2-
import { analytics } from './analytics.js';
3-
import { logEvent } from "firebase/analytics";
2+
import { trackEvent } from "./analytics.js";
43

5-
function trackButtonClick(obj) {
6-
const event = obj.innerText || obj.textContent;
7-
logEvent(analytics, "click", { name: event });
8-
}
9-
10-
function trackSocialButtonClick(obj) {
11-
const event = obj.getAttribute('aria-label');
12-
logEvent(analytics, "click-social", { name: event });
13-
}
14-
15-
function copyLink(url) {
16-
window.navigator.clipboard.writeText(url);
17-
confirm("Page URL copied to the clipboard.");
4+
function copyLinkToClipboard(url) {
5+
if (navigator.clipboard) {
6+
navigator.clipboard.writeText(url).then(() => {
7+
alert('Page URL copied to the clipboard.');
8+
}).catch(() => {
9+
alert('Failed to copy the URL. Please copy manually.');
10+
});
11+
} else {
12+
alert('Clipboard API is not supported in this browser.');
13+
}
1814
}
1915

2016
// Attach Event Handlers
2117
document.addEventListener('DOMContentLoaded', () => {
22-
document.querySelectorAll('.clazz-button-container a').forEach(a => {
23-
a.addEventListener('click', (e) => {
24-
trackButtonClick(a);
18+
document.querySelectorAll('.clazz-button-container a').forEach(a => {
19+
a.addEventListener('click', (e) => {
20+
trackEvent("click", a);
21+
});
2522
});
26-
});
2723

28-
document.querySelectorAll('.clazz-social-links a').forEach(a => {
29-
a.addEventListener('click', (e) => {
30-
trackSocialButtonClick(a);
24+
document.querySelectorAll('.clazz-social-links a').forEach(a => {
25+
a.addEventListener('click', (e) => {
26+
trackEvent("social-click", a);
27+
});
3128
});
32-
});
3329

34-
document.querySelectorAll('.clazz-copy-link').forEach(a => {
35-
a.addEventListener('click', (e) => {
36-
e.preventDefault();
37-
copyLink(window.location.href);
30+
document.querySelectorAll('.clazz-copy-link').forEach(a => {
31+
a.addEventListener('click', (e) => {
32+
e.preventDefault();
33+
copyLinkToClipboard(window.location.href);
34+
});
3835
});
39-
});
4036
});

src/scss/404.scss

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
@use "variables" as *;
33

44
// Reset
5-
body, html {
5+
body,
6+
html {
67
margin: 0;
78
padding: 0;
89
font-family: $font-family;
@@ -15,7 +16,7 @@ body, html {
1516
}
1617

1718
// Main container
18-
.error-container {
19+
.clazz-error-container {
1920
text-align: center;
2021
max-width: 640px;
2122
margin: auto;
@@ -39,12 +40,12 @@ body, html {
3940
filter: brightness(0) saturate(100%) invert(20%) sepia(97%) saturate(1590%) hue-rotate(193deg) brightness(95%) contrast(102%);
4041
}
4142

42-
.action p {
43+
.clazz-action p {
4344
font-size: $font-size-small;
4445
margin-bottom: calc($gap / 2); // Smaller gap for less important text
4546
}
4647

47-
.home-link {
48+
.clazz-home-link {
4849
display: inline-block;
4950
padding: calc($gap / 2) $gap; // Padding with gap-based scaling
5051
font-size: $font-size-link;

src/views/404.ejs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<link rel="preload" href="../assets/images/confused-robot.png?as=webp" as="image" type="image/webp" />
99
</head>
1010
<body>
11-
<div class="error-container">
11+
<div class="clazz-error-container">
1212
<header>
1313
<h1>Page not found</h1>
1414
<p>Oops! Looks like you’ve hit a dead end.</p>
@@ -18,9 +18,9 @@
1818
<source type="image/webp" srcset="../assets/images/confused-robot.png?as=webp">
1919
<img src="../assets/images/confused-robot.png" alt="Confused robot looking for the page" loading="lazy" width="300" height="270" />
2020
</picture>
21-
<div class="action">
21+
<div class="clazz-action">
2222
<p>Don't worry, even robots get lost sometimes.</p>
23-
<a href="/" class="home-link">Take Me Home</a>
23+
<a href="/" aria-label="Visit the Home Page" class="clazz-home-link">Take Me Home</a>
2424
</div>
2525
</div>
2626
</body>

0 commit comments

Comments
 (0)