A distributed real-time ad event tracking system built with Spring Boot and Apache Kafka. This application demonstrates event-driven architecture for capturing, streaming, and analyzing advertisement interaction data.
The system consists of two Spring Boot microservices:
- Frontend Service (Port 8081): Web interface for generating ad events and data visualization
- Backend Service (Port 8080): Consumer service that processes Kafka messages and provides REST APIs
βββββββββββββββ Kafka Topics βββββββββββββββ MySQL βββββββββββββββ
β Frontend β βββββββββββββββββββΊ β Backend β βββββββββββΊ β Database β
β (Producer) β ad_viewed β (Consumer) β β β
β β ad_clicked β β β β
β β ad_closed β β β β
βββββββββββββββ βββββββββββββββ βββββββββββββββ
- Enriched Data Model: Events now include ad company and category information
- Improved UI Consistency: Unified navigation and styling across all pages
- Better Accessibility: Enhanced contrast for close buttons and consistent button layouts
- Streamlined Interface: Removed external visit buttons for cleaner ad interaction focus
- Consistent Navigation: Top-positioned navigation buttons on all pages
- Optimized Button Layout: All ad action buttons aligned in single row
- Enhanced Data Display: Company and category columns in raw data view
- Professional Styling: Gradient headers and consistent visual design
- Real-time Event Streaming: Kafka-based message processing for ad interactions
- Interactive Ad Display: Visual ad showcase with real product advertisements
- Event Tracking: Automatic tracking of ad views, clicks, and closures with company and category metadata
- Data Visualization: Interactive charts showing event distribution, trends, and ad performance analytics
- REST API: Comprehensive endpoints for accessing event data, statistics, and ad-specific analytics
- Persistent Storage: MySQL database for event history with enriched ad metadata (company, category)
- Responsive Design: Mobile-friendly interface for ad interactions
- Containerized Deployment: Docker support with Kubernetes manifests
Main interface showing interactive ad cards with View, Click, and Close tracking buttons
Comprehensive analytics with event distribution charts and detailed ad performance metrics
Detailed event history with timestamps, event types, and ad metadata including company and category information
- Java 17 - Programming language
- Spring Boot 3.5.5 - Application framework
- Apache Kafka - Event streaming platform
- MySQL - Database for event persistence
- Thymeleaf - Template engine for web UI
- Maven - Build and dependency management
- Docker - Containerization
- Kubernetes - Container orchestration
- Java 17 or higher
- Maven 3.6+
- Apache Kafka < 4.0 (local or remote)
- MySQL 8.0+
- Docker (optional, for containerized deployment)
git clone <repository-url>
cd ad-event-tracking-systemOption A: Local Setup
- Start Kafka on
localhost:9092 - Start MySQL on
localhost:3306with databaseads_db
Option B: Docker Compose (if available)
docker-compose up -d kafka mysqlCreate MySQL database and user:
CREATE DATABASE ads_db;
CREATE USER 'ads_user'@'%' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON ads_db.* TO 'ads_user'@'%';Backend Service:
cd backend
./mvnw spring-boot:runFrontend Service:
cd frontend
./mvnw spring-boot:run- Ad Showcase: http://localhost:8081 - Interactive ad display with event tracking
- Analytics Dashboard: http://localhost:8081/chart - Comprehensive ad performance analytics
- Raw Data View: http://localhost:8081/data - Detailed event logs
- Backend API: http://localhost:8080/api/events - All events endpoint
- Event Counts API: http://localhost:8080/api/events/counts - Aggregated statistics
- Apache Kafka version < 4.0 (tested with Kafka 3.8)
- Kafka must be running on
localhost:9092for default configuration
1. Start Zookeeper (in first terminal):
# Navigate to your Kafka installation directory
cd /path/to/kafka
# Start Zookeeper (runs on port 2181)
bin/zookeeper-server-start.sh config/zookeeper.properties2. Start Kafka Broker (in second terminal):
# Start Kafka server (runs on port 9092)
bin/kafka-server-start.sh config/server.propertiesCreate Required Topics (the application will auto-create these, but you can create them manually):
# Create ad_viewed topic
bin/kafka-topics.sh \
--create \
--topic ad_viewed \
--bootstrap-server localhost:9092 \
--partitions 1 \
--replication-factor 1
# Create ad_clicked topic
bin/kafka-topics.sh \
--create \
--topic ad_clicked \
--bootstrap-server localhost:9092 \
--partitions 1 \
--replication-factor 1
# Create ad_closed topic
bin/kafka-topics.sh \
--create \
--topic ad_closed \
--bootstrap-server localhost:9092 \
--partitions 1 \
--replication-factor 1List All Topics:
bin/kafka-topics.sh --list --bootstrap-server localhost:9092Monitor Ad Events (useful for debugging):
# Monitor ad_viewed events
bin/kafka-console-consumer.sh \
--bootstrap-server localhost:9092 \
--topic ad_viewed \
--from-beginning
# Monitor ad_clicked events
bin/kafka-console-consumer.sh \
--bootstrap-server localhost:9092 \
--topic ad_clicked \
--from-beginning
# Monitor ad_closed events
bin/kafka-console-consumer.sh \
--bootstrap-server localhost:9092 \
--topic ad_closed \
--from-beginningManually Send Test Events (for testing):
# Send test ad_viewed event
bin/kafka-console-producer.sh --broker-list localhost:9092 --topic ad_viewed
# Then type: {"eventType":"ad_viewed","timestamp":"2024-01-01T12:00:00Z","adId":1,"adTitle":"Test Ad","adCompany":"Test Company","adCategory":"Electronics"}
# Send test ad_clicked event
bin/kafka-console-producer.sh --broker-list localhost:9092 --topic ad_clicked
# Then type: {"eventType":"ad_clicked","timestamp":"2024-01-01T12:00:00Z","adId":1,"adTitle":"Test Ad","adCompany":"Test Company","adCategory":"Electronics"}Option 1: Use Ctrl+C in the terminal windows running Zookeeper and Kafka
Option 2: Use stop scripts:
# Stop Kafka server first
bin/kafka-server-stop.sh
# Then stop Zookeeper
bin/zookeeper-server-stop.sh- Bootstrap Servers: Default
localhost:9092 - Consumer Group:
ad-consumer-group(configured in backend) - Auto Topic Creation: Enabled by default
- Message Format: JSON with event metadata
- Partitions: Single partition per topic (suitable for development)
- Replication Factor: 1 (suitable for single-broker setup)
Backend Service:
SPRING_KAFKA_BOOTSTRAP_SERVERS=localhost:9092
SPRING_DATASOURCE_URL=jdbc:mysql://localhost:3306/ads_db
SPRING_DATASOURCE_USERNAME=ads_user
SPRING_DATASOURCE_PASSWORD=passwordFrontend Service:
KAFKA_BOOTSTRAP_SERVERS=localhost:9092
BACKEND_API_URL=http://localhost:8080/api/events
SERVER_PORT=8081# Build backend image
cd backend
docker build -t ad-tracker-backend .
# Build frontend image
cd frontend
docker build -t ad-tracker-frontend .# Run backend
docker run -p 8080:8080 \
-e SPRING_KAFKA_BOOTSTRAP_SERVERS=kafka:9092 \
-e SPRING_DATASOURCE_URL=jdbc:mysql://mysql:3306/ads_db \
ad-tracker-backend
# Run frontend
docker run -p 8081:8081 \
-e KAFKA_BOOTSTRAP_SERVERS=kafka:9092 \
-e BACKEND_API_URL=http://backend:8080/api/events \
ad-tracker-frontend| Method | Endpoint | Description |
|---|---|---|
| GET | /api/events |
Get all events |
| GET | /api/events/counts |
Get event counts by type |
| GET | /api/events/counts/by-ad |
Get total interactions per ad |
| GET | /api/events/analytics/by-ad |
Get detailed analytics breakdown per ad with company/category |
| GET | /api/events/top-ads |
Get top 10 performing ads by interactions |
| Method | Endpoint | Description |
|---|---|---|
| GET | / |
Main event generation page |
| POST | /send-event |
Send ad event to Kafka |
| GET | /chart |
Event visualization page |
| GET | /data |
Raw event data page |
- Access the ad showcase at http://localhost:8081
- Interact with displayed ads:
- View: Click the "ποΈ View" button or ad image
- Click: Click the "π±οΈ Click" button to simulate ad clicks
- Close: Click "β Close" to simulate closing an ad
- View real-time analytics at
/chartand raw data at/data - Check backend API at http://localhost:8080/api/events for all events
- Monitor console logs for Kafka message processing
βββ backend/ # Consumer service
β βββ src/main/java/
β β βββ com/example/backend/
β β βββ controller/ # REST controllers
β β βββ entity/ # JPA entities
β β βββ repository/ # Data access layer
β β βββ service/ # Business logic
β βββ Dockerfile
βββ frontend/ # Producer service & web UI
β βββ src/main/java/
β β βββ com/example/frontend/
β β βββ controller/ # Web controllers
β β βββ model/ # Data models
β β βββ service/ # Kafka producers
β βββ src/main/resources/templates/ # Thymeleaf templates
β βββ Dockerfile
βββ README.md
- Application Logs: Check console output for Kafka message processing
- Database: Query
ad_eventtable for stored events - Kafka Topics: Monitor
ad_viewed,ad_clicked,ad_closedtopics
This project is licensed under the MIT License - see the LICENSE file for details.
Common Issues:
- Kafka Connection Failed: Ensure Kafka is running and accessible
- Database Connection Error: Verify MySQL is running and credentials are correct
- Port Already in Use: Check if ports 8080/8081 are available
- Maven Build Fails: Ensure Java 17+ is installed and JAVA_HOME is set
Logs Location:
- Application logs: Console output
- Kafka logs: Check Kafka server logs
- Database logs: MySQL error logs