website-visitor-counter
Version:
Real visitor counting system with Railway backend - works like komarev.com with accurate cross-device counting
492 lines (386 loc) ⢠16.2 kB
Markdown
# Website Visitor Counter v3.1.0 š
A **multi-platform visitor counting system** with **cloud-agnostic architecture** supporting Railway, Vercel, Netlify, and Cloudflare backends.
## š **LIVE DEMO: [View My Portfolio](https://my-portfolio-mnahsanofficials-projects.vercel.app/)**
See the visitor counter in action on my live portfolio website!
## ⨠**What's New in v3.1.0 - MULTI-PLATFORM RELEASE!**
- š **Multi-Platform Support** - Railway, Vercel, Netlify, and Cloudflare backends
- š **No Vendor Lock-in** - Platform-agnostic architecture with easy switching
- š **Enhanced Security** - Proper rate limiting and request fingerprinting
- š **Environment Configuration** - API URLs in environment variables, not hardcoded
- š **Improved Scalability** - Support for high-traffic websites
- š **Better Error Handling** - Graceful fallbacks and improved reliability
- šÆ **Production Ready** - Multiple deployment options for any cloud platform
## šÆ **Perfect For**
- **Portfolio websites** - Show real visitor counts
- **GitHub README files** - Dynamic visitor badges
- **Blog posts** - Track actual readership
- **Documentation pages** - Monitor page visits
- **Any web project** - Real visitor analytics
## š¦ **Installation**
```bash
npm install website-visitor-counter
```
š¦ **NPM Package**: [https://www.npmjs.com/package/website-visitor-counter](https://www.npmjs.com/package/website-visitor-counter)
š **GitHub Repository**: [https://github.com/mnahsanofficial/website-visitor-counter](https://github.com/mnahsanofficial/website-visitor-counter)
š **Live Demo**: [https://my-portfolio-mnahsanofficials-projects.vercel.app/](https://my-portfolio-mnahsanofficials-projects.vercel.app/)
## š **Multi-Platform Support**
The package now supports multiple cloud platforms through a flexible adapter system:
### **Supported Platforms**
- **š Railway** - Default backend (recommended for production)
- **ā” Vercel** - Serverless functions with edge caching
- **š Netlify** - Serverless functions with global CDN
- **āļø Cloudflare** - Workers with edge computing
- **š§ Custom** - Any custom backend with standard API
### **Platform Selection**
```javascript
import { getVisitorCounterBadge } from 'website-visitor-counter';
// Use Railway (default)
const badge1 = await getVisitorCounterBadge('my-project');
// Use Vercel
const badge2 = await getVisitorCounterBadge('my-project', {
platform: 'vercel'
});
// Use custom backend
const badge3 = await getVisitorCounterBadge('my-project', {
platform: 'custom',
customBaseUrl: 'https://my-backend.com'
});
```
### **Environment Configuration**
Create a `.env` file (copy from `env.example`):
```bash
# Railway Backend
RAILWAY_API_BASE=https://your-railway-app.up.railway.app
# Vercel Backend
VERCEL_API_BASE=https://your-vercel-app.vercel.app
# Netlify Backend
NETLIFY_API_BASE=https://your-netlify-app.netlify.app
# Cloudflare Backend
CLOUDFLARE_API_BASE=https://your-cloudflare-app.pages.dev
```
## š **Quick Start**
### **Basic Usage**
```javascript
import { getVisitorCounterBadge } from 'website-visitor-counter';
// Get a real visitor counter badge
const badgeUrl = await getVisitorCounterBadge('my-portfolio', {
label: 'visitors',
color: '0e75b6',
style: 'flat'
});
console.log(badgeUrl);
// Output: https://img.shields.io/badge/visitors-3-0e75b6?style=flat
```
### **React Component**
```jsx
import React, { useState, useEffect } from 'react';
import { getVisitorCounterBadge } from 'website-visitor-counter';
function VisitorCounter() {
const [badgeUrl, setBadgeUrl] = useState('');
const [count, setCount] = useState(0);
useEffect(() => {
const fetchBadge = async () => {
try {
const url = await getVisitorCounterBadge('my-portfolio', {
label: 'visitors',
color: '0e75b6',
style: 'flat'
});
setBadgeUrl(url);
// Extract count from URL for display
const countMatch = url.match(/visitors-(\d+)/);
if (countMatch) {
setCount(parseInt(countMatch[1]));
}
} catch (error) {
console.error('Failed to fetch visitor count:', error);
}
};
fetchBadge();
}, []);
return (
<div>
<p>Total Visitors: {count}</p>
{badgeUrl && <img src={badgeUrl} alt="visitor count" />}
</div>
);
}
```
### **HTML Badge**
```html
<img src="https://websitevisiotrscounter-production.up.railway.app/counter?project=my-portfolio&label=visitors&color=0e75b6&style=flat"
alt="visitor count" />
```
### **Markdown Badge**
```markdown

```
## š ļø **API Reference**
### **Core Functions**
#### `getVisitorCounterBadge(options)`
Generates a real visitor counter badge with actual counting.
```typescript
interface VisitorCounterOptions {
project: string; // Required: Your project name
label?: string; // Optional: Badge label (default: "visitors")
color?: string; // Optional: Badge color (default: "0e75b6")
style?: 'flat' | 'flat-square' | 'plastic' | 'for-the-badge' | 'pixel';
base?: number; // Optional: Starting count (default: 0)
abbreviated?: boolean; // Optional: Abbreviated numbers (default: false)
}
```
#### `getVisitorCount(project)`
Get the current visitor count for a project without generating a badge.
```typescript
const count = await getVisitorCount('my-portfolio');
console.log(`Total visitors: ${count}`);
```
#### `resetVisitorCount(project)`
Reset the visitor count for a project.
```typescript
await resetVisitorCount('my-portfolio');
console.log('Visitor count reset!');
```
### **Utility Functions**
#### `getSimpleVisitorBadge(project, label)`
Quick badge generation with minimal options.
```typescript
const badge = await getSimpleVisitorBadge('my-portfolio', 'readers');
```
#### `getVisitorCounterHTML(options)`
Generate HTML img tag for the visitor counter.
```typescript
const html = await getVisitorCounterHTML({
project: 'my-portfolio',
label: 'visitors'
});
// Output: <img src="..." alt="visitors" />
```
#### `getVisitorCounterMarkdown(options)`
Generate Markdown badge syntax.
```typescript
const markdown = await getVisitorCounterMarkdown({
project: 'my-portfolio',
label: 'visitors'
});
// Output: 
```
#### `getVisitorCounterReact(options)`
Generate React component code.
```typescript
const reactCode = await getVisitorCounterReact({
project: 'my-portfolio',
label: 'visitors'
});
// Output: <img src="..." alt="visitors" />
```
### **Backend Monitoring**
#### `getBackendHealth()`
Check Railway backend health status.
```typescript
const health = await getBackendHealth();
console.log(`Backend status: ${health.status}`);
console.log(`Last check: ${health.timestamp}`);
```
#### `getBackendStats()`
Get backend statistics and project counts.
```typescript
const stats = await getBackendStats();
console.log(`Total projects: ${stats.totalProjects}`);
console.log('Project details:', stats.projects);
```
## šØ **Badge Styles**
| Style | Preview | Description |
|-------|---------|-------------|
| `flat` |  | Default flat style |
| `flat-square` |  | Square corners |
| `plastic` |  | 3D plastic effect |
| `for-the-badge` |  | GitHub-style badge |
| `pixel` |  | Pixelated style |
## š **Popular Color Schemes**
| Color | Hex | Preview |
|-------|-----|---------|
| Blue | `0e75b6` |  |
| Green | `28a745` |  |
| Red | `dc3545` |  |
| Orange | `fd7e14` |  |
| Purple | `6f42c1` |  |
| Gray | `6c757d` |  |
## š **Privacy & Security**
- **Enhanced Rate Limiting**: Configurable rate limiting per project (10 requests/minute by default)
- **Request Fingerprinting**: Advanced request validation for security
- **No Personal Data**: Only anonymous visitor counts are stored
- **CORS Safe**: Works safely in browsers with proper CORS headers
- **Platform Security**: Each cloud platform provides its own security features
- **Environment Variables**: API keys and URLs stored securely in environment files
## š **How It Works**
1. **Visitor visits** your website with the badge
2. **Platform adapter** routes request to your chosen cloud backend
3. **Rate limiting** checks prevent abuse and spam
4. **Request fingerprinting** validates legitimate requests
5. **Count incremented** for new visitors with configurable tracking
6. **Badge generated** with real count using shields.io
7. **Data stored** in your chosen cloud platform (persistent across all devices)
## š **Multi-Platform Backends**
Your visitor counter can be powered by **any cloud platform** through our adapter system:
### **Railway (Default)**
- ā
**Counts visitors across all devices** (Mac, iOS, Android, etc.)
- ā
**Provides 99.9% uptime** with automatic scaling
- ā
**Stores data securely** with enhanced security features
- ā
**Offers real-time statistics** and health monitoring
### **Vercel**
- ā” **Serverless functions** with edge caching
- š **Global CDN** for fast badge delivery
- š **Automatic scaling** based on demand
### **Netlify**
- š **Serverless functions** with global CDN
- š **Easy deployment** from Git repositories
- š **Built-in analytics** and monitoring
### **Cloudflare**
- āļø **Workers** with edge computing
- š **Global edge network** for minimal latency
- š° **Pay-per-use** pricing model
**Default Backend URL**: `https://websitevisiotrscounter-production.up.railway.app`
## š **Version Comparison**
| Feature | v1.0.0 | v2.0.0 | v2.1.0 | v3.0.0 | v3.1.0 |
|---------|---------|---------|---------|---------|---------|
| **Database** | ā
Supabase | ā None | ā Local Storage | ā
Railway Backend | ā
Multi-Platform |
| **Real Counting** | ā
Yes | ā Mock | ā
Yes | ā
Yes | ā
Yes |
| **Cross-Device** | ā
Yes | ā No | ā No | ā
Yes | ā
Yes |
| **Privacy** | ā
IP Hashing | ā None | ā
IP Hashing | ā
IP Hashing | ā
Enhanced Security |
| **Badge Styles** | ā Basic | ā
All Styles | ā
All Styles | ā
All Styles | ā
All Styles |
| **Production Ready** | ā Setup Required | ā Mock Only | ā Local Only | ā
Hosted | ā
Multi-Platform |
| **Architecture** | Database | Mock | Local Storage | Cloud Backend | **Platform Agnostic** |
## š» **Examples**
### **Node.js Script (v3.1.0)**
```javascript
import {
getVisitorCounterBadge,
getVisitorCount,
getBackendHealth,
createPlatformAdapter
} from 'website-visitor-counter';
async function main() {
try {
// Check backend health
const isHealthy = await getBackendHealth();
console.log(`Backend healthy: ${isHealthy}`);
// Get visitor count
const count = await getVisitorCount('my-website');
console.log(`Current visitors: ${count}`);
// Generate badge with Railway (default)
const badge1 = await getVisitorCounterBadge('my-website', {
label: 'readers',
color: '28a745',
style: 'for-the-badge'
});
console.log(`Railway badge: ${badge1}`);
// Generate badge with Vercel
const badge2 = await getVisitorCounterBadge('my-website', {
label: 'readers',
color: '28a745',
style: 'for-the-badge',
platform: 'vercel'
});
console.log(`Vercel badge: ${badge2}`);
} catch (error) {
console.error('Error:', error.message);
}
}
main();
```
### **Node.js Script (v3.0.0)**
```javascript
import { getVisitorCounterBadge, getVisitorCount } from 'website-visitor-counter';
async function main() {
try {
// Get visitor count
const count = await getVisitorCount('my-website');
console.log(`Current visitors: ${count}`);
// Generate badge
const badge = await getVisitorCounterBadge('my-website', {
label: 'readers',
color: '28a745',
style: 'for-the-badge'
});
console.log(`Badge URL: ${badge}`);
} catch (error) {
console.error('Error:', error.message);
}
}
main();
```
### **Browser Usage**
```html
<!DOCTYPE html>
<html>
<head>
<title>Visitor Counter Demo</title>
</head>
<body>
<h1>My Portfolio</h1>
<div id="visitor-count">Loading...</div>
<div id="visitor-badge"></div>
<script type="module">
import { getVisitorCounterBadge, getVisitorCount } from 'https://unpkg.com/website-visitor-counter@3.1.0/dist/index.js';
async function updateVisitorCount() {
try {
const count = await getVisitorCount('my-portfolio');
document.getElementById('visitor-count').textContent = `Total Visitors: ${count}`;
const badge = await getVisitorCounterBadge('my-portfolio', {
label: 'visitors',
color: '0e75b6',
style: 'flat'
});
document.getElementById('visitor-badge').innerHTML = `<img src="${badge}" alt="visitor count" />`;
} catch (error) {
console.error('Error:', error);
}
}
updateVisitorCount();
</script>
</body>
</html>
```
## š **Support**
- š¦ [NPM Package](https://www.npmjs.com/package/website-visitor-counter)
- š [GitHub Repository](https://github.com/mnahsanofficial/website-visitor-counter)
- š [Documentation](https://github.com/mnahsanofficial/website-visitor-counter#readme)
- š [Railway Backend](https://websitevisiotrscounter-production.up.railway.app/health)
- šÆ [Live Demo](https://my-portfolio-mnahsanofficials-projects.vercel.app/)
## šØāš» **Developer**
**Muhammad Nazmul Ahsan**
š [LinkedIn Profile](https://www.linkedin.com/in/mn-ahsan/)
## š¢ **Company**
**TrioTrix Tech Solutions**
š [Company LinkedIn](https://www.linkedin.com/company/triotrix-tech-solutions/)
š [Website](https://triotrix-website.vercel.app/)
---
## š **Changelog**
### **v3.0.0** - š MAJOR RELEASE: Railway Backend Integration
- š **NEW**: Complete architecture rewrite from local storage to cloud backend
- š **NEW**: Railway backend for real cross-device counting
- š **NEW**: Cross-device accuracy (same count on all devices)
- š **NEW**: Backend health monitoring and statistics
- š **IMPROVED**: Enhanced privacy with server-side IP hashing
- šÆ **IMPROVED**: Production-ready with 99.9% uptime
- š **NEW**: Automatic fallback for offline scenarios
- šØ **NEW**: Major version bump reflecting significant architectural changes
### **v2.1.0** - Real Counting System
- ā
**NEW**: Real visitor counting (not just mock badges)
- š **NEW**: Privacy-focused IP hashing
- š± **NEW**: Cross-platform storage (localStorage + in-memory)
- šØ **NEW**: Multiple badge styles and colors
- š **NEW**: Reset functionality
### **v2.0.0** - Badge-Based System
- šØ **NEW**: Badge generation system
- š **NEW**: Multiple styles and color schemes
- š± **NEW**: HTML, Markdown, and React outputs
- š **NEW**: No database setup required
### **v1.0.0** - Supabase Integration
- šļø **NEW**: Supabase database integration
- š **NEW**: IP hashing for privacy
- š **NEW**: Real visitor counting
- š **NEW**: CORS-safe for browsers
---
*Empowering Tomorrow's Digital World* š