# Building a User-Friendly Web Experience: From Theme Toggle to Analytics

Warish

Hatched by Warish

Jan 06, 2026

4 min read

0

Building a User-Friendly Web Experience: From Theme Toggle to Analytics

In the rapidly evolving landscape of web development, creating user-friendly interfaces and tracking user interactions has become paramount. Two essential features that can significantly enhance user experience are a light/dark mode toggle and effective website analytics. This article will guide you through implementing both functionalities, ensuring your website not only looks great but also provides valuable insights into user behavior.

Light/Dark Mode Toggle

Simplifying User Preferences

With the growing emphasis on accessibility and personalization, offering a light/dark mode toggle is a straightforward way to cater to user preferences. This functionality allows users to switch between a light theme, which is easy on the eyes during daylight, and a dark theme, which can reduce eye strain in low-light environments.

Implementation Steps

  1. HTML Structure: Start by creating a simple HTML layout. Include a button with a class of theme-toggle, an SVG icon for visual appeal, and an H1 tag that displays the current mode—either "Light Mode" or "Dark Mode." Below the H1, add a paragraph for filler text.

  2. CSS Variables: Utilize CSS variables to define colors for both themes. Set the background color and text color using these variables. This approach allows you to change the theme by simply toggling a class on the body element without modifying multiple CSS rules.

  3. JavaScript Functionality: Implement the theme toggle functionality using JavaScript. Select the toggle button and add an event listener for the click event. When the button is clicked, toggle the dark-mode class on the body element. This can be done using classList.toggle.

  4. Local Storage: Enhance user experience by remembering their last selected theme using local storage. When the page loads, check local storage for the theme key. If it exists and indicates dark mode, apply the dark-mode class to the body. This way, users return to their preferred theme even after refreshing the page.

Example Code

Here’s a simplified version of the implementation:

<button class="theme-toggle">Toggle Theme</button>  
<h1 id="mode-display">Light Mode</h1>  
<p>Your filler text here.</p>  
  
<style>  
  :root {  
    --light-bg: ffffff;  
    --dark-bg: 000000;  
    --light-text: 000000;  
    --dark-text: ffffff;  
  }  
  
  body {  
    background-color: var(--light-bg);  
    color: var(--light-text);  
  }  
  
  .dark-mode {  
    background-color: var(--dark-bg);  
    color: var(--dark-text);  
  }  
</style>  
  
<script>  
  const themeToggleButton = document.querySelector('.theme-toggle');  
  themeToggleButton.addEventListener('click', () => {  
    document.body.classList.toggle('dark-mode');  
    localStorage.setItem('theme', document.body.classList.contains('dark-mode') ? 'dark' : 'light');  
    document.getElementById('mode-display').innerText = document.body.classList.contains('dark-mode') ? 'Dark Mode' : 'Light Mode';  
  });  
  
  const currentTheme = localStorage.getItem('theme');  
  if (currentTheme === 'dark') {  
    document.body.classList.add('dark-mode');  
    document.getElementById('mode-display').innerText = 'Dark Mode';  
  }  
</script>  

Tracking Website Analytics with Umami

Understanding User Behavior

While providing a customizable experience is vital, understanding how users interact with your website is equally important. Integrating a robust analytics tool can help you collect and analyze user data effectively. Umami, an open-source web analytics platform, offers a privacy-friendly solution that allows you to track user interactions seamlessly.

Implementation Steps

  1. Fork and Clone: Start by forking the Umami repository and cloning it to your local machine. Remove any unnecessary files, such as the Docker file, to streamline your setup.

  2. Deploy on Railway: Create an account on Railway and initiate a new project. Deploy your forked Umami repository and configure essential environment variables, including HASH_SALT, PORT, DATABASE_TYPE, and HOSTNAME.

  3. Database Setup: Create a PostgreSQL database on Railway to store your analytics data. Once set up, redeploy the project to ensure the database is correctly linked.

  4. Access the Dashboard: After deployment, generate a domain through the Railway settings to access the Umami dashboard. Log in and change the default password to secure your dashboard.

  5. Integrate Tracking Code: To start tracking user interactions, copy the provided tracking code from the Umami settings and place it in the head section of your website. This code will automatically begin collecting data on user behavior.

Example Code for Tracking

Simply add this snippet to the <head> section of your HTML:

<script async src="https://your-umami-domain/script.js" data-website-id="your-website-id"></script>  

Actionable Advice

  1. Prioritize Accessibility: When implementing features like light/dark mode, consider users with visual impairments. Ensure sufficient contrast and test your designs with accessibility tools.

  2. Analyze Data Regularly: Use the insights gained from your analytics dashboard to inform design and content decisions. Regularly review user behavior to enhance engagement and retention.

  3. Encourage Feedback: Create avenues for users to provide feedback on both the light/dark mode feature and their overall experience. This can guide further improvements and foster user loyalty.

Conclusion

By integrating a light/dark mode toggle alongside a comprehensive analytics solution, you can significantly enhance user experience on your website. These features not only cater to individual preferences but also provide valuable insights into user behavior, allowing for continuous improvement. Embrace these practices to create a more engaging and personalized web experience that resonates with your audience.

Sources

← Back to Library

Hatch New Ideas with Glasp AI 🐣

Glasp AI allows you to hatch new ideas based on your curated content. Let's curate and create with Glasp AI :)

Start Hatching 🐣