- Overview
- Project Structure
- Installation
- Running Locally
- Chrome Extension Setup
- Deployment
- API Documentation
- Testing
- Future Enhancements
Digital Behavior Mirror is a full-stack web application that passively tracks browser activity and generates actionable focus insights. It consists of:
- Backend: FastAPI REST API with SQLite database
- Frontend: Next.js React application with interactive dashboards
- Extension: Chrome extension for passive browser tracking
- Analytics: AI-powered insights generation with automatic URL categorization
✅ Passive browser activity tracking
✅ Automatic URL categorization (Work, Learning, Social, Entertainment, Other)
✅ Daily session summaries with analytics
✅ Real-time charts and focus statistics
✅ AI-generated insights and recommendations
✅ Chrome extension for seamless tracking
✅ No authentication required
✅ Local SQLite database
digital-behavior-mirror/
├── backend/
│ ├── main.py # FastAPI application
│ ├── database.py # SQLite database management
│ ├── models.py # Pydantic models
│ ├── insights.py # AI insights & categorization logic
│ ├── requirements.txt # Python dependencies
│ └── behavior_mirror.db # SQLite database (auto-created)
│
├── frontend/
│ ├── pages/
│ │ ├── index.tsx # Landing/Home page
│ │ ├── dashboard.tsx # Analytics dashboard
│ │ ├── sessions.tsx # Sessions history
│ │ ├── _app.tsx # Next.js app wrapper
│ │ └── _document.tsx # HTML document
│ ├── components/
│ │ ├── Header.tsx # Navigation header
│ │ ├── Footer.tsx # Footer
│ │ ├── Layout.tsx # Page layout wrapper
│ │ ├── CategoryChart.tsx # Category time chart
│ │ ├── TabSwitchChart.tsx # Tab switch timeline
│ │ ├── FocusStats.tsx # Focus metrics display
│ │ └── AIInsights.tsx # AI insights component
│ ├── hooks/
│ │ └── useApi.ts # API integration hooks
│ ├── styles/
│ │ └── globals.css # Global styles
│ ├── package.json # Dependencies
│ ├── tailwind.config.js # TailwindCSS config
│ └── next.config.js # Next.js config
│
├── extension/
│ ├── manifest.json # Chrome extension manifest
│ ├── background.js # Background service worker
│ ├── popup.html # Extension popup UI
│ └── popup.js # Popup logic
│
├── .env.example # Environment variables template
├── README.md # This file
└── install.sh # Installation script
- Python 3.8+ - For backend
- Node.js 16+ - For frontend
- npm or yarn - Package manager
- Chrome/Chromium - For extension and testing
cd digital-behavior-mirror# Navigate to backend directory
cd backend
# Create Python virtual environment
python -m venv venv
# Activate virtual environment
# On Windows:
venv\Scripts\activate
# On macOS/Linux:
source venv/bin/activate
# Install dependencies
pip install -r requirements.txt
# Database will be auto-initialized on first run# Navigate to frontend directory
cd ../frontend
# Install Node dependencies
npm install
# Build Next.js (optional, for production)
npm run build# Create .env files from templates
cp .env.example .env.local
# Edit environment variables as needed
# Default values work for local developmentcd backend
source venv/bin/activate # or venv\Scripts\activate on Windows
uvicorn main:app --reload --host 0.0.0.0 --port 8000Backend will be available at: http://localhost:8000
API docs available at: http://localhost:8000/docs
cd frontend
npm run devFrontend will be available at: http://localhost:3000
- Open Chrome and go to
chrome://extensions - Enable "Developer mode" (toggle in top-right)
- Click "Load unpacked"
- Navigate to
digital-behavior-mirror/extensionfolder - Click "Select Folder"
The extension will appear in your toolbar with a purple icon.
If using the web version without the extension:
- Go to
http://localhost:3000 - Click "Start Session"
- Enter URLs and categories manually
- Observe real-time tracking
- Click "End Session & View Results"
With the Chrome extension installed:
- Click the extension icon in your toolbar
- Click "Start Tracking"
- Browse normally - all tabs are tracked automatically
- Click "Stop Tracking" to end the session
- View the dashboard automatically
The extension:
- Monitors active tabs in real-time
- Sends data to the backend API
- Runs in the background
- Stores session state in Chrome storage
- No typing or interaction required
- Create account at https://railway.app
- Create new project → GitHub repo
- Add repository with backend code
- Set environment variables
- Deploy automatically on push
# Backend start command
uvicorn main:app --host 0.0.0.0 --port $PORT- Create account at https://vercel.com
- Import your repository
- Set build command:
npm run build - Set environment variables:
NEXT_PUBLIC_API_URL=https://your-railway-app.railway.app - Deploy
In extension/background.js, change:
const API_URL = 'https://your-railway-app.railway.app';- Install Heroku CLI
- Create Procfile:
web: uvicorn main:app --host 0.0.0.0 --port $PORT
- Deploy:
heroku create your-app-name
git push heroku main- Connect GitHub repo to Netlify
- Set build command:
npm run build - Set publish directory:
.next - Add environment variables
Create docker-compose.yml:
version: '3.8'
services:
backend:
build:
context: ./backend
dockerfile: Dockerfile
ports:
- "8000:8000"
environment:
- DATABASE_URL=sqlite:///./behavior_mirror.db
volumes:
- ./backend:/app
command: uvicorn main:app --host 0.0.0.0 --port 8000
frontend:
build:
context: ./frontend
dockerfile: Dockerfile
ports:
- "3000:3000"
environment:
- NEXT_PUBLIC_API_URL=http://localhost:8000
depends_on:
- backendBuild and run:
docker-compose up --build- Local:
http://localhost:8000 - Production:
https://your-api-url.com
POST /session/start
Response: { session_id, start_time, message }
POST /session/{session_id}/end
Response: { session_id, end_time, duration_minutes, message }
POST /tab_activity
Body: {
"session_id": "uuid",
"url": "https://example.com",
"category": "Work|Learning|Social|Entertainment|Other",
"start_time": "2026-01-09T10:30:00Z",
"end_time": "2026-01-09T10:40:00Z"
}
Response: { id, session_id, url, category, duration_seconds, message }
GET /session/{session_id}/summary
Response: {
"session_id": "uuid",
"start_time": "2026-01-09T10:30:00Z",
"end_time": "2026-01-09T11:30:00Z",
"duration_minutes": 60,
"category_stats": [...],
"tab_switches_by_hour": [...],
"focus_stats": {...},
"ai_insights": [...],
"activity_count": 15
}
GET /sessions?limit=20
Response: [{ id, start_time, end_time, duration_minutes }, ...]
GET /health
Response: { status, timestamp }
When running locally, visit: http://localhost:8000/docs
Swagger UI with interactive endpoint testing
-
Start Backend:
uvicorn main:app --reload -
Start Frontend:
npm run dev -
Load Extension:
chrome://extensions→ Load unpacked -
Test Scenario A - Manual Web Entry:
- Go to http://localhost:3000
- Click "Start Session"
- Enter 5-10 different URLs with categories
- Click "End Session"
- Verify dashboard shows correct charts and insights
-
Test Scenario B - Extension Tracking:
- Click extension icon → "Start Tracking"
- Open multiple tabs (GitHub, YouTube, Slack, etc.)
- Switch tabs 10+ times
- Click "Stop Tracking"
- Check dashboard for auto-categorization
Run this Python script to populate sample data:
# sample_data.py
import requests
from datetime import datetime, timedelta
import json
API_URL = "http://localhost:8000"
# Sample sites with categories
SITES = [
("https://github.com/", "Work"),
("https://stackoverflow.com/", "Learning"),
("https://linkedin.com/", "Social"),
("https://youtube.com/", "Entertainment"),
("https://notion.so/", "Work"),
("https://twitter.com/", "Social"),
("https://udemy.com/", "Learning"),
]
# Create session
resp = requests.post(f"{API_URL}/session/start")
session_id = resp.json()["session_id"]
# Log activities
current_time = datetime.utcnow()
for i, (url, category) in enumerate(SITES):
start = current_time + timedelta(minutes=i*10)
end = start + timedelta(minutes=5)
requests.post(f"{API_URL}/tab_activity", json={
"session_id": session_id,
"url": url,
"category": category,
"start_time": start.isoformat() + "Z",
"end_time": end.isoformat() + "Z",
})
# End session
requests.post(f"{API_URL}/session/{session_id}/end")
print(f"Sample session created: {session_id}")
print(f"View at: http://localhost:3000/dashboard?session={session_id}")Run it:
python sample_data.pyThe system automatically categorizes URLs based on patterns:
- github.com, gitlab.com, jira, confluence, slack.com
- gmail.com, stackoverflow.com, developer sites
- Code editors: vscode.dev, codepen.io, repl.it
- coursera.com, udemy.com, khan academy
- w3schools.com, mdn mozilla docs
- YouTube learning videos
- facebook.com, instagram.com, twitter.com, tiktok.com
- reddit.com, discord.com, linkedin.com
- quora.com, medium.com
- netflix.com, youtube.com, twitch.tv, hulu.com
- Gaming sites: steampowered.com, xbox.com
- Tab Switching Analysis: Flags high switching (>30) with Pomodoro recommendations
- Distraction Detection: Alerts when distraction time exceeds 60 minutes
- Focus Capability: Celebrates long focus periods (45+ min)
- Productivity Score: Calculates % of productive vs distracting time
- Pattern Recognition: Identifies time-of-day patterns and suggests optimizations
CREATE TABLE session (
id TEXT PRIMARY KEY,
start_time TEXT NOT NULL,
end_time TEXT,
duration_minutes INTEGER,
created_at TEXT DEFAULT CURRENT_TIMESTAMP
)CREATE TABLE tab_activity (
id INTEGER PRIMARY KEY AUTOINCREMENT,
session_id TEXT NOT NULL,
url TEXT NOT NULL,
category TEXT NOT NULL,
start_time TEXT NOT NULL,
end_time TEXT,
duration_seconds INTEGER,
created_at TEXT DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (session_id) REFERENCES session(id)
)- Framework: FastAPI (async Python framework)
- Database: SQLite (local file-based)
- ORM: Raw SQL with context managers
- Validation: Pydantic models
- Server: Uvicorn ASGI server
- Framework: Next.js 14
- UI Library: React 18
- Styling: TailwindCSS
- Charts: Recharts
- Icons: Lucide React
- HTTP Client: Axios
- API: Chrome Extension Manifest V3
- Service Worker: Background scripts
- Storage: Chrome Storage API
- Communication: Chrome Runtime Messaging
- Version Control: Git
- Containerization: Docker (optional)
- Frontend Hosting: Vercel / Netlify
- Backend Hosting: Railway / Heroku
- CI/CD: GitHub Actions (optional)
- User authentication with OAuth2
- Cloud database (PostgreSQL)
- Weekly/monthly analytics views
- Custom focus goal tracking
- Website blocking during focus hours
- Personalized notifications
- Export data as CSV/JSON
- Dark mode UI
- Advanced ML predictions
- Collaborative team analytics
- Mobile app (React Native)
- Integration with Slack, GitHub
- Productivity leaderboards
- Custom categorization rules
- Browser sync across devices
# In backend/main.py - Future authentication
# from fastapi.security import OAuth2PasswordBearer
# from jose import JWTError, jwt
# oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
# In frontend - Future notifications
// import { toast } from 'react-hot-toast';
// toast.success('Session saved!');
# In extension - Future site blocking
// chrome.webRequest.onBeforeRequest.addListener(
// (details) => {
// if (blockedSites.includes(new URL(details.url).hostname)) {
// return { redirectUrl: chrome.runtime.getURL("blocked.html") };
// }
// },
// { urls: ["<all_urls>"] },
// ["blocking"]
// );Solution: Ensure port 8000 is available
# Find process using port 8000 and kill it
lsof -i :8000 # macOS/Linux
netstat -ano | findstr :8000 # WindowsSolution: Verify API URL in extension/background.js
const API_URL = 'http://localhost:8000'; // Change if differentSolution: Backend CORS is enabled for all origins in dev mode
# In main.py - already configured
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Change in production!
...
)Solution: Delete database and reinitialize
rm backend/behavior_mirror.db
cd backend
python -c "from database import init_db; init_db()"✅ Async API with FastAPI/Uvicorn
✅ React component memoization
✅ TailwindCSS purging for smaller CSS
✅ Next.js code splitting
✅ Chrome storage for session state
✅ SQLite indexes on foreign keys
- Add Redis caching for /sessions endpoint
- Implement pagination for large datasets
- Compress images in UI
- Use Next.js ISR for dashboards
- Add database connection pooling
- Implement OAuth2 authentication
- Restrict CORS to known domains
- Use PostgreSQL with encryption
- Implement rate limiting
- Add HTTPS/TLS
- Hash sensitive data
- Regular security audits
- GDPR compliance for data storage
- Check the API docs: http://localhost:8000/docs
- Review database schema in database.py
- Check insights.py for categorization logic
- Review component props in frontend files
- Create a feature branch
- Make changes
- Test thoroughly
- Create pull request
This project is open source and available under the MIT License.
Happy tracking! 🚀
For questions or issues, check the GitHub discussions or create an issue with:
- Browser version
- Steps to reproduce
- Expected vs actual behavior
- Error messages or screenshots