Skip to content

This project tracks and analyzes user interactions such as page views, button clicks, and geolocation events from a website. It collects these events in real-time, stores them in a database, and provides an interface to visualize the data using charts. Users can filter event data by date and type to understand engagement patterns.

Notifications You must be signed in to change notification settings

bilalhassankhan007/User_Event_Analytics_api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

OBJECTIVES

This project is a web analytics service that collects user interaction data (like page views, button clicks, and location) from a website, stores it in a database, and provides APIs to analyze the data.

PROJECT ARCHITECTURE

web-analytics-service/ │ ├── app/ # Main application code │ ├── init.py # Flask app initialization │ ├── models.py # Database models │ ├── routes.py # API endpoints │ ├── services.py # Business logic │ └── validators.py # Request validation │ ├── scripts/ │ └── generate_events.py # Data generation script │ ├── static/ # For frontend files │ ├── index.html # Demo client │ └── service-worker.js # Service worker │ ├── tests/ # Test cases │ ├── .env # Environment variables ├── config.py # Configuration ├── requirements.txt # Dependencies └── README.md # Project documentation

FEATURES
  • Collect user interaction events via REST API
  • Store events in PostgreSQL database
  • Query aggregated analytics data
  • Sample frontend integration with service worker
TECHNOLOGIES
  • Language: Python 3.8+
  • Framework: Flask
  • Database: PostgreSQL
  • Testing: Postman
  • Additional Libraries:
    • Flask-SQLAlchemy (ORM)
    • Faker (data generation)
    • python-dotenv (environment variables)
Setup Instructions
  1. Clone the repository:

    git clone https://github.com/yourusername/web-analytics-service.git
    cd web-analytics-service
    
  2. Set up virtual environment python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate

  3. Install Dependencies pip install -r requirements.txt

  4. Configure environment Create a .env file with: DB_USER=postgres ` DB_PASSWORD=yourpassword DB_HOST=localhost DB_PORT=5432 DB_NAME=analytics_db FLASK_APP=app FLASK_ENV=development

  5. Create PostgreSQL DB CREATE DATABASE analytics_db;

  6. Generate sample data python -m app.scripts.generate_events

  7. Run the app python run.py

  8. Access Frontend in your Local Browser http://localhost:5000 in your browser.

📬API Documentation
  1. POST /api/events

    Purpose: Receive and store a new event. Request Body (example): { "user_id": "user-123", "event_type": "click", "payload": { "element_id": "btn-submit", "text": "Submit", "xpath": "//button[@id='btn-submit']" } }

Responses: 202 Accepted – Event stored successfully 400 Bad Request – Validation failure 500 Internal Server Error – DB failure or unhandled exception

  1. GET/api/analytics/event-counts

Purpose: Retrieve total number of events Query Params:

  • event_type – optional ("view", "click", "location")
  • start_date / end_date – optional (ISO format) Response (example): { "total_events": 2089 }
  1. GET /api/analytics/event-counts-by-type

Purpose: Group event counts by type Query Params:

  • start_date / end_date – optional
  • Response (example): { "view": 1021, "click": 789, "location": 279 }
DATABASE SCHEMA

The "events" table has the following structure: event_id: UUID (Primary Key) user_id: String (Indexed) event_type: String (Indexed) timestamp: DateTime (Indexed) payload: JSON

Indexing Strategy:##

user_id: Indexed for quick user-specific queries event_type: Indexed for filtering by event type timestamp: Indexed for efficient date range queries

CHALLENGES & SOLUTIONS
  1. Database Performance Problem: Analytics queries could be slow with thousands of events.

Solution:

Added database indexes on: CREATE INDEX idx_event_type ON events(event_type); CREATE INDEX idx_timestamp ON events(timestamp); Used efficient GROUP BY queries for analytics.

  1. Frontend-Backend Communication: Browser → Service Worker → Flask API chain could fail silently.

Solution:

Added dual fallback (Service Worker + direct fetch()): // Try Service Worker first if (navigator.serviceWorker?.controller) { sendViaServiceWorker(); } else { sendDirectly(); // Fallback } Added user-friendly status updates in the UI.

FUTURE IMPROVEMENTS

Authentication: Add API key or JWT authentication Rate Limiting: Prevent abuse of the API Asynchronous Processing: Use a message queue (Redis, RabbitMQ) for event ingestion Real-time Analytics: Add WebSocket support for real-time dashboards Advanced Location Queries: Implement geospatial indexing for location-based queries Caching: Add Redis caching for frequent analytics queries Monitoring: Add Prometheus metrics and health checks Containerization: Dockerize the application for easy deployment

About

This project tracks and analyzes user interactions such as page views, button clicks, and geolocation events from a website. It collects these events in real-time, stores them in a database, and provides an interface to visualize the data using charts. Users can filter event data by date and type to understand engagement patterns.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published