website visitor counter code javascript codepen

Track Website Visitors Live: Easy JavaScript CodePen Counter

By James Martin
AD

Are you seeking a simple yet robust way to track your website's visitors? Look no further! This article will guide you through creating a visitor counter using JavaScript and HTML. We'll harness the power of CodePen, a robust online code editor, to make this task hassle-free. Let's dive right into the world of web development!

Before we embark on our coding journey, it's crucial to understand that this counter won't be entirely accurate as it won't differentiate between unique and repeat visitors. However, it serves as a basic starting point and can be enhanced with server-side scripting or external tools for more precise tracking.

Setting Up Your CodePen Environment

Firstly, head over to CodePen and create a new Pen. You'll see three panels: CSS, HTML, and JavaScript. We'll focus on the HTML and JavaScript panels for thisallenge.

AD
Input Character Counter JS - Code Snippet
Input Character Counter JS - Code Snippet
Bun Server Code Snippet
Bun Server Code Snippet
an image of some type of webpage with dark colors and blue text on it
an image of some type of webpage with dark colors and blue text on it

Ensuring the HTML and JavaScript panels are open, let's initialized our basic HTML structure:

HTML Structure

Let's initialize our basic HTML structure:

Highlight Text to Specific Range in JavaScript
Highlight Text to Specific Range in JavaScript
an image of a computer screen with the text'jjs script mouse events '
an image of a computer screen with the text'jjs script mouse events '
Animated Curved Nav Using JavaScript
Animated Curved Nav Using JavaScript
```html Visitor Counter
0
```

The 'div' element with the ID 'counter' will hold our visitor count. We've linked an external 'script.js' file to handle our JavaScript functionality, which we'll set up next.

Creating the Visitor Counter Functionality

Now, let's create a JavaScript function to increment our visitor counter and store the data.

AD
```javascript // Check if visitor count is already stored in local storage let visitorCount = localStorage.getItem('visitor_count') ? parseInt(localStorage.getItem('visitor_count')) : 0; // Display visitor count on the page document.getElementById('counter').innerText = visitorCount; // Increment visitor count by 1 visitorCount++; // Store updated visitor count in local storage localStorage.setItem('visitor_count', visitorCount); ```

Whenever a user visits this page, the 'Visitor counter' will increment by one. The localStorage API stores this number, ensuring the count persists rather than resetting to zero upon page refresh.

Simple Online Spacebar Counter with Restart (JS, CSS, HTML) - Code Snippet
Simple Online Spacebar Counter with Restart (JS, CSS, HTML) - Code Snippet
CSS expanding search bar animation #csstricks
CSS expanding search bar animation #csstricks
the word hey is written in white on a black background
the word hey is written in white on a black background
the counter clock is displayed in black and red
the counter clock is displayed in black and red
an image of a web page with the text'voyage slider '
an image of a web page with the text'voyage slider '
Botão css
Botão css

Using the Counter on Your Website

To use this counter on your website, firstly save the complete HTML and JavaScript code in a new HTML file on your server. Secondly, open this file in a web browser to see your visitor counter in action.

Remember, the visitor counter relies on the browser's localStorage API, which is not shared between different browsers or devices. Therefore, it might not provide a comprehensive total for all users browsing your website with varying browsers or devices.

AD

Expanding the Counter with Cookies for Unique Visitors

To differentiate between unique visitors, we can use cookies, another data storage tool. Cookies are shared between different browser tabs on the same device but not across devices. Here's how you'd modify the JS code for cookies:

CSS cursor effect
CSS cursor effect
an animated image slider is shown with the text, and it appears to be being displayed
an animated image slider is shown with the text, and it appears to be being displayed
an image of a computer screen with the words sticky cursor on it
an image of a computer screen with the words sticky cursor on it
CSS custom cursor
CSS custom cursor
CSS sliding menu
CSS sliding menu
css counter.mp4
css counter.mp4
Bootstrap 5 Counter Up Animation with JavaScript
Bootstrap 5 Counter Up Animation with JavaScript
Statistics Counter Animation with CSS and JS
Statistics Counter Animation with CSS and JS
CSS navigation tab
CSS navigation tab
an image of a computer screen with text
an image of a computer screen with text
Meet CodePen: Dribbble for Coders
Meet CodePen: Dribbble for Coders
```javascript // Check if visitor has been counted today let today = new Date().toISOString().slice(0,10); let visitorCount = localStorage.getItem(`visitor_count_${today}`) ? parseInt(localStorage.getItem(`visitor_count_${today}`)) : 0; let isCounted = document.cookie.includes(`counted_${today}`); // If not counted today, increment, show result and set cookie if (!isCounted) { visitorCount++; document.getElementById('counter').innerText = visitorCount; document.cookie = `counted_${today} = true; max-age=86400; path=/`; localStorage.setItem(`visitor_count_${today}`, visitorCount); } else { document.getElementById('counter').innerText = visitorCount; } ```

This code snippet will give you a daily unique visitor count on your website.

And that's a wrap! You've successfully created a basic website visitor counter using JavaScript, HTML, and the code editor CodePen. Happy coding!

AD
AD