docs YouTube Analytics API 수정 및 오디언스 인사이트 단위 수정

This commit is contained in:
skkim committed 2026-02-27 15:26:38 +09:00
1 parent a1192193e5
commit 8fe0512608
4 files changed
+84 -42

No files matched your search

+1 -1
View File
@@ -267,7 +267,7 @@ async def get_dashboard_stats(
Dashboard.platform == "youtube",
Dashboard.platform_user_id == social_account.platform_user_id,
Dashboard.uploaded_at >= start_dt,
Dashboard.uploaded_at <= kpi_end_dt,
Dashboard.uploaded_at <= end_dt,
)
)
period_video_count = count_result.scalar() or 0
+10 -16
View File
@@ -418,8 +418,8 @@ class DataProcessor:
# === 연령/성별 데이터 처리 ===
demo_rows = demographics_data.get("rows", [])
age_map: dict[str, int] = {}
gender_map = {"male": 0, "female": 0}
age_map: dict[str, float] = {}
gender_map_f: dict[str, float] = {"male": 0.0, "female": 0.0}
for row in demo_rows:
if len(row) < 3:
@@ -427,27 +427,21 @@ class DataProcessor:
age_group = row[0] # "age18-24"
gender = row[1] # "male" or "female"
views = row[2]
viewer_pct = row[2] # viewerPercentage (이미 % 값, 예: 45.5)
# 연령대별 집계 (age18-24 → 18-24)
# 연령대별 집계: 남녀 비율 합산 (age18-24 → 18-24)
age_label = age_group.replace("age", "")
age_map[age_label] = age_map.get(age_label, 0) + views
age_map[age_label] = age_map.get(age_label, 0.0) + viewer_pct
# 성별 집계
if gender in gender_map:
gender_map[gender] += views
if gender in gender_map_f:
gender_map_f[gender] += viewer_pct
# 연령대별 비율 계산
total_demo_views = sum(age_map.values())
age_groups = [
{
"label": age,
"percentage": int(
(count / total_demo_views * 100) if total_demo_views > 0 else 0
),
}
for age, count in sorted(age_map.items())
{"label": age, "percentage": int(round(pct))}
for age, pct in sorted(age_map.items())
]
gender_map = {k: int(round(v)) for k, v in gender_map_f.items()}
# === 지역 데이터 처리 ===
geo_rows = geography_data.get("rows", [])
+3 -9
View File
@@ -124,7 +124,7 @@ class YouTubeAnalyticsService:
self._fetch_monthly_data(video_ids, previous_start, previous_end, access_token),
self._fetch_top_videos(video_ids, start_date, _kpi_end, access_token),
self._fetch_demographics(start_date, _kpi_end, access_token),
self._fetch_region(video_ids, start_date, _kpi_end, access_token),
self._fetch_region(start_date, _kpi_end, access_token),
]
else: # mode == "day"
tasks = [
@@ -134,7 +134,7 @@ class YouTubeAnalyticsService:
self._fetch_daily_data(video_ids, day_previous_start, day_previous_end, access_token),
self._fetch_top_videos(video_ids, start_date, end_date, access_token),
self._fetch_demographics(start_date, end_date, access_token),
self._fetch_region(video_ids, start_date, end_date, access_token),
self._fetch_region(start_date, end_date, access_token),
]
# 병렬 실행
@@ -206,8 +206,6 @@ class YouTubeAnalyticsService:
estimatedMinutesWatched, averageViewDuration,
subscribersGained]
Note:
annotationClickThroughRate는 2019년 annotations 기능 제거로 deprecated.
"""
logger.debug(
f"[YouTubeAnalyticsService._fetch_kpi] START - video_count={len(video_ids)}"
@@ -218,7 +216,7 @@ class YouTubeAnalyticsService:
"startDate": start_date,
"endDate": end_date,
"metrics": "views,likes,comments,shares,estimatedMinutesWatched,averageViewDuration,subscribersGained",
# "filters": f"video=={','.join(video_ids)}",
"filters": f"video=={','.join(video_ids)}",
}
result = await self._call_api(params, access_token)
@@ -393,7 +391,6 @@ class YouTubeAnalyticsService:
async def _fetch_region(
self,
video_ids: list[str],
start_date: str,
end_date: str,
access_token: str,
@@ -403,7 +400,6 @@ class YouTubeAnalyticsService:
지역별 조회수 분포를 조회합니다 (상위 5개).
Args:
video_ids: YouTube 영상 ID 리스트
start_date: 조회 시작일 (YYYY-MM-DD)
end_date: 조회 종료일 (YYYY-MM-DD)
access_token: OAuth 2.0 액세스 토큰
@@ -421,9 +417,7 @@ class YouTubeAnalyticsService:
"endDate": end_date,
"dimensions": "country",
"metrics": "views",
"filters": f"video=={','.join(video_ids)}",
"sort": "-views",
"maxResults": "5",
}
result = await self._call_api(params, access_token)