CREATE TABLE IF NOT EXISTS page_views (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
    viewed_path VARCHAR(255) NOT NULL,
    page_type VARCHAR(32) NOT NULL DEFAULT 'page',
    entity_type VARCHAR(32) NULL,
    entity_id INT UNSIGNED NULL,
    page_title VARCHAR(255) NULL,
    referrer_url VARCHAR(500) NULL,
    referrer_path VARCHAR(255) NULL,
    session_key VARCHAR(128) NULL,
    user_agent VARCHAR(255) NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (id),
    KEY idx_page_views_path_created_at (viewed_path, created_at),
    KEY idx_page_views_entity (entity_type, entity_id, created_at),
    KEY idx_page_views_referrer_path (referrer_path, created_at),
    KEY idx_page_views_session_key (session_key, created_at),
    KEY idx_page_views_created_at (created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
