A lightweight, privacy-focused web analytics tracking server built with Node.js and PostgreSQL. Track website visits, analyze user behavior, and generate beautiful SVG charts - all without compromising user privacy.
- Real-time Visit Tracking: Capture page visits with detailed metadata
- Multi-site Support: Track analytics for multiple websites from a single instance
- Beautiful SVG Charts: Generate dynamic spline charts with neon-glow effects
- Comprehensive Analytics:
- Visits over time
- Top pages analysis
- Site-by-site breakdown
- Unique visitor tracking
- Referrer tracking
- Privacy-Focused: IP address tracking with minimal data collection
- RESTful API: Easy integration with any frontend
- PostgreSQL Backend: Reliable and scalable data storage
- CORS Enabled: Cross-origin resource sharing support
- Render.com Ready: Pre-configured deployment setup
- Node.js 16+
- PostgreSQL database
- npm or yarn
- Clone the repository:
git clone https://github.com/Mohinth-32/Web-Analytics.git
cd Web-Analytics- Install dependencies:
npm install- Create a
.envfile in the root directory:
DATABASE_URL=postgresql://username:password@host:port/database
PORT=3000- Set up your PostgreSQL database with the required schema:
CREATE TABLE site_visits (
id SERIAL PRIMARY KEY,
site VARCHAR(255) NOT NULL,
page VARCHAR(500) NOT NULL,
referrer VARCHAR(500),
user_agent TEXT,
ip_address VARCHAR(45),
screen VARCHAR(50),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Create indexes for better query performance
CREATE INDEX idx_site_visits_created_at ON site_visits(created_at);
CREATE INDEX idx_site_visits_site ON site_visits(site);
CREATE INDEX idx_site_visits_page ON site_visits(page);- Start the server:
npm startThe server will run on http://localhost:3000
GET /Returns server status
POST /track
Content-Type: application/json
{
"site": "example.com",
"page": "/home",
"referrer": "https://google.com",
"userAgent": "Mozilla/5.0...",
"screen": "1920x1080"
}GET /analytics/visits-over-time?site=example.com&days=30Returns daily visit counts for the specified period.
Query Parameters:
site(optional): Filter by specific sitedays(optional, default: 30): Number of days to analyze
GET /analytics/by-site?days=30Returns visit counts grouped by site.
Query Parameters:
days(optional, default: 30): Number of days to analyze
GET /analytics/by-site/svg?days=30Returns a beautiful SVG spline chart showing visits by site.
Query Parameters:
days(optional, default: 30): Number of days to visualize
Features:
- Neon green color scheme
- Smooth spline curves
- Dynamic scaling
- Labeled data points
GET /analytics/top-pages?site=example.com&days=30&limit=10Returns the most visited pages.
Query Parameters:
site(optional): Filter by specific sitedays(optional, default: 30): Number of days to analyzelimit(optional, default: 10, max: 1000): Maximum number of results
GET /analytics/visits?site=example.com&page=/home&days=30&limit=100&offset=0Returns detailed visit records with pagination.
Query Parameters:
site(optional): Filter by specific sitepage(optional): Filter by specific pagedays(optional, default: 30): Number of days to analyzelimit(optional, default: 100, max: 1000): Results per pageoffset(optional, default: 0): Pagination offset
GET /analytics/summary?site=example.com&days=30Returns aggregated statistics.
Response:
{
"total_visits": "1234",
"total_sites": "5",
"total_pages": "42",
"unique_visitors": "890"
}Add this to your website to track page visits:
<script>
// Track page view
fetch('https://your-analytics-server.com/track', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
site: window.location.hostname,
page: window.location.pathname,
referrer: document.referrer,
userAgent: navigator.userAgent,
screen: `${window.screen.width}x${window.screen.height}`
})
}).catch(err => console.error('Analytics error:', err));
</script>import { useEffect } from 'react';
const useAnalytics = () => {
useEffect(() => {
const trackPageView = async () => {
try {
await fetch('https://your-analytics-server.com/track', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
site: window.location.hostname,
page: window.location.pathname,
referrer: document.referrer,
userAgent: navigator.userAgent,
screen: `${window.screen.width}x${window.screen.height}`
})
});
} catch (error) {
console.error('Analytics tracking failed:', error);
}
};
trackPageView();
}, []);
};
export default useAnalytics;This project includes a render.yaml configuration file for easy deployment:
- Push your code to GitHub
- Connect your repository to Render.com
- Render will automatically detect the
render.yamlfile - Set the
DATABASE_URLenvironment variable in Render dashboard - Deploy!
Required environment variables:
DATABASE_URL: PostgreSQL connection stringPORT: Server port (automatically set by most hosting platforms)
- Node.js: Runtime environment
- Express: Web framework
- PostgreSQL: Database
- Postgres.js: PostgreSQL client
- CORS: Cross-origin resource sharing
- dotenv: Environment variable management
Table: site_visits
ββββββββββββββββ¬βββββββββββββββ¬ββββββββββββββ
β Column β Type β Constraints β
ββββββββββββββββΌβββββββββββββββΌββββββββββββββ€
β id β SERIAL β PRIMARY KEY β
β site β VARCHAR(255) β NOT NULL β
β page β VARCHAR(500) β NOT NULL β
β referrer β VARCHAR(500) β β
β user_agent β TEXT β β
β ip_address β VARCHAR(45) β β
β screen β VARCHAR(50) β β
β created_at β TIMESTAMP β DEFAULT NOW β
ββββββββββββββββ΄βββββββββββββββ΄ββββββββββββββ