Objective
Implement comprehensive video view tracking with watch time, completion rates, and engagement analytics. This enables understanding viewer behavior and content performance.
Step 1: Create Videos Table
Create a table for trackable videos.
CREATE TABLE tracked_videos (
id INTEGER PRIMARY KEY,
title VARCHAR(255) NOT NULL,
video_file VIDEO(MP4),
duration_seconds INTEGER NOT NULL,
category VARCHAR(50),
total_views INTEGER DEFAULT 0,
unique_viewers INTEGER DEFAULT 0,
total_watch_time INTEGER DEFAULT 0,
avg_completion_rate DECIMAL(5, 2) DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
Step 2: Create View Events Table
Track individual view sessions.
CREATE TABLE video_views (
id INTEGER PRIMARY KEY,
video_id INTEGER NOT NULL,
viewer_id VARCHAR(100),
session_id VARCHAR(100) NOT NULL,
watch_start TIMESTAMP NOT NULL,
watch_end TIMESTAMP,
watch_duration_seconds INTEGER DEFAULT 0,
completion_percent DECIMAL(5, 2) DEFAULT 0,
playback_speed DECIMAL(3, 2) DEFAULT 1.00,
device_type VARCHAR(50),
browser VARCHAR(50),
country VARCHAR(50),
referrer VARCHAR(255),
is_autoplay BOOLEAN DEFAULT FALSE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (video_id) REFERENCES tracked_videos(id)
);
Step 3: Create View Milestones Table
Track completion milestones.
CREATE TABLE view_milestones (
id INTEGER PRIMARY KEY,
view_id INTEGER NOT NULL,
milestone_percent INTEGER NOT NULL,
reached_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (view_id) REFERENCES video_views(id)
);
Step 4: Insert Sample Videos
Add videos to track.
INSERT INTO tracked_videos (id, title, duration_seconds, category) VALUES
(1, 'Product Introduction', 300, 'marketing'),
(2, 'Tutorial: Getting Started', 900, 'education'),
(3, 'Feature Deep Dive', 1500, 'product'),
(4, 'Quick Tips', 180, 'tips'),
(5, 'Webinar Recording', 3600, 'webinar');
Step 5: Insert View Events
Add sample view data.
INSERT INTO video_views (id, video_id, viewer_id, session_id, watch_start, watch_end, watch_duration_seconds, completion_percent, device_type, browser, country, is_autoplay) VALUES
-- Product Introduction views
(1, 1, 'user_001', 'sess_001', '2024-01-15 10:00:00', '2024-01-15 10:05:00', 300, 100.00, 'desktop', 'Chrome', 'US', FALSE),
(2, 1, 'user_002', 'sess_002', '2024-01-15 11:30:00', '2024-01-15 11:33:00', 180, 60.00, 'mobile', 'Safari', 'UK', TRUE),
(3, 1, 'user_003', 'sess_003', '2024-01-15 14:00:00', '2024-01-15 14:04:30', 270, 90.00, 'tablet', 'Chrome', 'CA', FALSE),
(4, 1, 'user_001', 'sess_004', '2024-01-16 09:00:00', '2024-01-16 09:05:00', 300, 100.00, 'desktop', 'Chrome', 'US', FALSE),
-- Tutorial views
(5, 2, 'user_004', 'sess_005', '2024-01-15 13:00:00', '2024-01-15 13:15:00', 900, 100.00, 'desktop', 'Firefox', 'DE', FALSE),
(6, 2, 'user_005', 'sess_006', '2024-01-15 15:00:00', '2024-01-15 15:08:00', 480, 53.33, 'mobile', 'Chrome', 'US', FALSE),
(7, 2, 'user_006', 'sess_007', '2024-01-16 10:00:00', '2024-01-16 10:12:00', 720, 80.00, 'desktop', 'Edge', 'US', FALSE),
-- Feature Deep Dive views
(8, 3, 'user_007', 'sess_008', '2024-01-15 16:00:00', '2024-01-15 16:25:00', 1500, 100.00, 'desktop', 'Chrome', 'US', FALSE),
(9, 3, 'user_008', 'sess_009', '2024-01-16 11:00:00', '2024-01-16 11:10:00', 600, 40.00, 'mobile', 'Safari', 'AU', TRUE),
-- Quick Tips views
(10, 4, 'user_009', 'sess_010', '2024-01-15 09:00:00', '2024-01-15 09:03:00', 180, 100.00, 'mobile', 'Chrome', 'US', FALSE),
(11, 4, 'user_010', 'sess_011', '2024-01-15 12:00:00', '2024-01-15 12:02:30', 150, 83.33, 'desktop', 'Chrome', 'UK', FALSE),
(12, 4, 'user_011', 'sess_012', '2024-01-16 08:00:00', '2024-01-16 08:03:00', 180, 100.00, 'mobile', 'Safari', 'US', FALSE);