-- Store document.referrer for organic / referral attribution on tel-clicks.
-- Safe to re-run: adds column only if missing.
SET @col_exists := (
  SELECT COUNT(*) FROM information_schema.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE()
    AND TABLE_NAME = 'call_clicks'
    AND COLUMN_NAME = 'referrer'
);
SET @sql := IF(
  @col_exists = 0,
  'ALTER TABLE call_clicks ADD COLUMN referrer VARCHAR(2048) NULL AFTER page_url',
  'SELECT 1'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
