Privacy-Focused-Web-Analytics-Dashboard

PrivacyMetrics - Developer Guide

One document. Simple tracking. Privacy-first analytics.


🎯 Quick Start (2 Minutes)

Step 1: Get Tracking Code

  1. Go to http://localhost:8080 (or your deployed URL)
  2. Sign up / Login
  3. Click β€œAdd Website”
  4. Copy your tracking code (e.g., pm-abc123-1706425600000)

Step 2: Add One Script Tag

Add this to your website’s <head> section:

<script src="https://your-domain.com/pm.js" 
        data-code="YOUR_TRACKING_CODE_HERE">
</script>

That’s it. No configuration, no API keys, no cookies.

Step 3: View Your Data


πŸ“Š What Gets Tracked Automatically

Privacy-first: No cookies, no fingerprinting, no personal data.


πŸ”§ Setup for Development

Prerequisites

Installation

# Clone repository
git clone <repo>
cd Privacy-Focused-Web-Analytics-Dashboard

# Install dependencies
pnpm install

# Start dev servers (frontend + backend)
pnpm dev

Servers will start on:


πŸ§ͺ Testing Locally

Option 1: Use Built-in Debug Page

# With server running, open:
http://localhost:8080/test-debug.html

Then:

  1. Replace YOUR_TRACKING_CODE with your actual code
  2. Click β€œCheck Status”
  3. Click β€œTest API Directly”
  4. Should see βœ… All green

Option 2: Create Test HTML

<!DOCTYPE html>
<html>
<head>
    <title>Test Page</title>
    <script src="http://localhost:8080/pm.js" 
            data-code="YOUR_CODE"
            data-api="http://localhost:3000/api/v1/track">
    </script>
</head>
<body>
    <h1>Testing PrivacyMetrics</h1>
    <p>Open DevTools (F12) β†’ Network tab to see POST requests</p>
</body>
</html>

Option 3: Test API Directly

$body = @{
    code = "your-tracking-code"
    sid = "pm_session_test"
    vid = "pm_visitor_test"
    url = "http://example.com/test"
    ref = "http://google.com"
    t = [int64](Get-Date -UFormat %s) * 1000
} | ConvertTo-Json

$response = Invoke-WebRequest `
    -Uri "http://localhost:3000/api/v1/track" `
    -Method POST `
    -Body $body `
    -ContentType "application/json"

Write-Host "Status: $($response.StatusCode)" # Should be 204

πŸš€ Integration Examples

React / Next.js

import { useEffect } from 'react';
import { useRouter } from 'next/router';

export default function App() {
  const router = useRouter();
  
  useEffect(() => {
    // Load tracker
    const script = document.createElement('script');
    script.src = 'https://your-domain.com/pm.js';
    script.setAttribute('data-code', 'pm-your-code');
    document.head.appendChild(script);
  }, []);

  useEffect(() => {
    // Track route changes
    if (window.pm) {
      window.pm.track();
    }
  }, [router.pathname]);

  return <>{/* Your app */}</>;
}

Vue.js

<script setup>
import { onMounted, watch } from 'vue';
import { useRoute } from 'vue-router';

const route = useRoute();

onMounted(() => {
  // Load tracker
  const script = document.createElement('script');
  script.src = 'https://your-domain.com/pm.js';
  script.setAttribute('data-code', 'pm-your-code');
  document.head.appendChild(script);
});

watch(() => route.path, () => {
  if (window.pm) window.pm.track();
});
</script>

Plain HTML/Static Site

<!DOCTYPE html>
<html>
<head>
    <title>My Site</title>
    <script src="https://your-domain.com/pm.js" 
            data-code="pm-your-code">
    </script>
</head>
<body>
    <!-- Your content -->
</body>
</html>

πŸ” Verifying It Works

Check Browser Console

// Should both have values
console.log(sessionStorage.getItem('_pm_sid'));  // Session ID
console.log(sessionStorage.getItem('_pm_vid'));  // Visitor ID

Check Network Tab

  1. Press F12 (DevTools)
  2. Go to β€œNetwork” tab
  3. Reload page
  4. Look for POST to /api/v1/track
  5. Should return 204 No Content βœ…

Check Dashboard

  1. Go to http://localhost:8080/dashboard (or your deployed URL)
  2. Select your website
  3. Should show:
    • Page views count
    • Unique visitors
    • Top pages
    • Referrers

βš™οΈ Advanced Configuration

Custom API Endpoint

<script src="https://your-domain.com/pm.js" 
        data-code="pm-your-code"
        data-api="https://custom-api.com/track">
</script>

Manual Event Tracking

// Track button clicks, form submissions, etc.
if (window.pm) {
  window.pm.track();  // Send another event with current page data
}

Multiple Websites

<!-- Site A -->
<script src="https://analytics.com/pm.js" data-code="pm-site-a"></script>

<!-- Site B -->
<script src="https://analytics.com/pm.js" data-code="pm-site-b"></script>

πŸ“ Project Structure

Privacy-Focused-Web-Analytics-Dashboard/
β”œβ”€β”€ public/
β”‚   β”œβ”€β”€ pm.js              # Tracking script
β”‚   β”œβ”€β”€ test-simple.html   # Simple test page
β”‚   └── test-debug.html    # Debug test page
β”œβ”€β”€ client/
β”‚   β”œβ”€β”€ pages/
β”‚   β”‚   β”œβ”€β”€ Index.tsx      # Home page
β”‚   β”‚   β”œβ”€β”€ Login.tsx      # Login
β”‚   β”‚   β”œβ”€β”€ Register.tsx   # Sign up
β”‚   β”‚   β”œβ”€β”€ Dashboard.tsx  # Analytics dashboard
β”‚   β”‚   └── WebsiteManagement.tsx  # Manage websites
β”‚   β”œβ”€β”€ hooks/
β”‚   β”‚   └── useDashboardData.ts   # Dashboard data fetching
β”‚   └── components/        # UI components
β”œβ”€β”€ server/
β”‚   β”œβ”€β”€ routes/
β”‚   β”‚   β”œβ”€β”€ auth.ts        # Authentication
β”‚   β”‚   β”œβ”€β”€ websites.ts    # Website CRUD
β”‚   β”‚   β”œβ”€β”€ tracking.ts    # Tracking endpoint
β”‚   β”‚   └── dashboard.ts   # Dashboard data
β”‚   β”œβ”€β”€ services/
β”‚   β”‚   β”œβ”€β”€ tracking.ts    # Event processing
β”‚   β”‚   └── aggregation.ts # Data aggregation
β”‚   └── middleware/
β”‚       └── auth.ts        # JWT authentication
β”œβ”€β”€ prisma/
β”‚   └── schema.prisma      # Database schema
└── docs/                  # Additional documentation

πŸ” Environment Variables

Create .env file:

# Database
DATABASE_URL=file:./prisma/dev.db

# Server
PORT=3000
NODE_ENV=development

# Frontend
VITE_API_URL=http://localhost:3000

πŸ› Troubleshooting

β€œNo tracking code found” warning

Fix: Add data-code attribute to script tag:

❌ <script src="/pm.js"></script>
βœ… <script src="/pm.js" data-code="pm-abc123"></script>

CORS Error

Fix: Make sure backend is running and CORS is configured:

Backend: http://localhost:3000/api/v1/track
Frontend: http://localhost:8080

Data not showing in dashboard

Check:

  1. Is the tracking code correct? (matches your website)
  2. Did the network request return 204? (Check DevTools)
  3. Is the website marked as active?
  4. Wait a few seconds - aggregation can take time
  5. Refresh the dashboard page

Database Issues

# Reset database (dev only)
rm prisma/dev.db
pnpm dev

πŸ“š API Endpoints

Public Endpoints (No auth required)

Protected Endpoints (Auth required)


πŸš€ Deployment

Netlify

pnpm build
# Deploy dist/spa folder to Netlify

Railway

Push to GitHub, connect repository to Railway:

Docker

docker build -t privacy-metrics .
docker run -p 3000:3000 -p 8080:8080 privacy-metrics

πŸ“Š What’s Different About This Tracker

Feature PrivacyMetrics Google Analytics Plausible
Self-hosted βœ… ❌ ⚠️
No cookies βœ… ❌ βœ…
No tracking code required βœ… ❌ ❌
Open source βœ… ❌ ❌
Own your data βœ… ❌ βœ…
GDPR compliant βœ… ❌ βœ…
Simple implementation βœ… ❌ βœ…

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/your-feature
  3. Commit changes: git commit -am 'Add feature'
  4. Push to branch: git push origin feature/your-feature
  5. Submit a pull request

πŸ“ License

MIT License - see LICENSE file for details


πŸ†˜ Support


βœ… Production Checklist

Before deploying:


Questions? Check the troubleshooting section above or open an issue.

Ready to track? Grab your tracking code and add one line to your site! πŸš€