From 56ccb08d9936e5cbf94ce438f1ba6f7ab0bd1037 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Varga?= Date: Sat, 30 Nov 2024 13:49:16 +0100 Subject: [PATCH] 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. --- src/js/404.js | 10 ++++++++- src/js/analytics.js | 16 ++++++++++++-- src/js/index.js | 52 +++++++++++++++++++++------------------------ src/scss/404.scss | 9 ++++---- src/views/404.ejs | 6 +++--- 5 files changed, 55 insertions(+), 38 deletions(-) diff --git a/src/js/404.js b/src/js/404.js index 509cee4..c22b31c 100644 --- a/src/js/404.js +++ b/src/js/404.js @@ -1,2 +1,10 @@ import '../scss/404.scss'; -import "./analytics.js"; \ No newline at end of file +import { trackEvent } from "./analytics.js"; + +document.addEventListener('DOMContentLoaded', () => { + document.querySelectorAll('.clazz-error-container a').forEach(a => { + a.addEventListener('click', (e) => { + trackEvent("click", a); + }); + }); +}); \ No newline at end of file diff --git a/src/js/analytics.js b/src/js/analytics.js index ceb3d83..c4512fd 100644 --- a/src/js/analytics.js +++ b/src/js/analytics.js @@ -1,5 +1,5 @@ import { initializeApp } from "firebase/app"; -import { getAnalytics } from "firebase/analytics"; +import { getAnalytics, logEvent } from "firebase/analytics"; // See: https://firebase.google.com/docs/web/learn-more#config-object const firebaseConfig = { @@ -16,4 +16,16 @@ const firebaseConfig = { const app = initializeApp(firebaseConfig); // Initialize Analytics and get a reference to the service -export const analytics = getAnalytics(app); +const analytics = getAnalytics(app); + +export function trackEvent(event, element, additionalData = {}) { + const eventName = element.getAttribute('aria-label') || obj.innerText || obj.textContent; + const eventData = { + name: eventName, + page_location: window.location.href, + page_referrer: document.referrer, + page_title: document.title, + ...additionalData, + }; + logEvent(analytics, event, eventData); +} \ No newline at end of file diff --git a/src/js/index.js b/src/js/index.js index ddd69aa..a3e44f5 100644 --- a/src/js/index.js +++ b/src/js/index.js @@ -1,40 +1,36 @@ import '../scss/index.scss'; -import { analytics } from './analytics.js'; -import { logEvent } from "firebase/analytics"; +import { trackEvent } from "./analytics.js"; -function trackButtonClick(obj) { - const event = obj.innerText || obj.textContent; - logEvent(analytics, "click", { name: event }); -} - -function trackSocialButtonClick(obj) { - const event = obj.getAttribute('aria-label'); - logEvent(analytics, "click-social", { name: event }); -} - -function copyLink(url) { - window.navigator.clipboard.writeText(url); - confirm("Page URL copied to the clipboard."); +function copyLinkToClipboard(url) { + if (navigator.clipboard) { + navigator.clipboard.writeText(url).then(() => { + alert('Page URL copied to the clipboard.'); + }).catch(() => { + alert('Failed to copy the URL. Please copy manually.'); + }); + } else { + alert('Clipboard API is not supported in this browser.'); + } } // Attach Event Handlers document.addEventListener('DOMContentLoaded', () => { - document.querySelectorAll('.clazz-button-container a').forEach(a => { - a.addEventListener('click', (e) => { - trackButtonClick(a); + document.querySelectorAll('.clazz-button-container a').forEach(a => { + a.addEventListener('click', (e) => { + trackEvent("click", a); + }); }); - }); - document.querySelectorAll('.clazz-social-links a').forEach(a => { - a.addEventListener('click', (e) => { - trackSocialButtonClick(a); + document.querySelectorAll('.clazz-social-links a').forEach(a => { + a.addEventListener('click', (e) => { + trackEvent("social-click", a); + }); }); - }); - document.querySelectorAll('.clazz-copy-link').forEach(a => { - a.addEventListener('click', (e) => { - e.preventDefault(); - copyLink(window.location.href); + document.querySelectorAll('.clazz-copy-link').forEach(a => { + a.addEventListener('click', (e) => { + e.preventDefault(); + copyLinkToClipboard(window.location.href); + }); }); - }); }); \ No newline at end of file diff --git a/src/scss/404.scss b/src/scss/404.scss index 5a6470f..4e7a98b 100644 --- a/src/scss/404.scss +++ b/src/scss/404.scss @@ -2,7 +2,8 @@ @use "variables" as *; // Reset -body, html { +body, +html { margin: 0; padding: 0; font-family: $font-family; @@ -15,7 +16,7 @@ body, html { } // Main container -.error-container { +.clazz-error-container { text-align: center; max-width: 640px; margin: auto; @@ -39,12 +40,12 @@ body, html { filter: brightness(0) saturate(100%) invert(20%) sepia(97%) saturate(1590%) hue-rotate(193deg) brightness(95%) contrast(102%); } - .action p { + .clazz-action p { font-size: $font-size-small; margin-bottom: calc($gap / 2); // Smaller gap for less important text } - .home-link { + .clazz-home-link { display: inline-block; padding: calc($gap / 2) $gap; // Padding with gap-based scaling font-size: $font-size-link; diff --git a/src/views/404.ejs b/src/views/404.ejs index a00aab6..50973f3 100644 --- a/src/views/404.ejs +++ b/src/views/404.ejs @@ -8,7 +8,7 @@ -
+

Page not found

Oops! Looks like you’ve hit a dead end.

@@ -18,9 +18,9 @@ Confused robot looking for the page -
+

Don't worry, even robots get lost sometimes.

- Take Me Home + Take Me Home