CREATE TABLE IF NOT EXISTS pageviews (
  id                INT AUTO_INCREMENT PRIMARY KEY,
  url               VARCHAR(2048) NOT NULL,
  type              ENUM('pageview','event','error','performance') NOT NULL DEFAULT 'pageview',
  user_agent        VARCHAR(512),
  viewport_width    SMALLINT UNSIGNED,
  viewport_height   SMALLINT UNSIGNED,
  referrer          VARCHAR(2048),
  client_timestamp  DATETIME,
  server_timestamp  DATETIME NOT NULL,
  client_ip         VARCHAR(45),
  session_id        VARCHAR(64),
  payload           JSON,
  created_at        TIMESTAMP DEFAULT CURRENT_TIMESTAMP,

  INDEX idx_url (url(255)),
  INDEX idx_type (type),
  INDEX idx_server_timestamp (server_timestamp)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
