From 7fc17b90321b7dad80feeee018eebb01b1311f24 Mon Sep 17 00:00:00 2001 From: king Date: Wed, 21 Oct 2026 16:28:06 +0900 Subject: [PATCH] =?UTF-8?q?chore(performance):=20=EB=A9=94=ED=83=80=20insi?= =?UTF-8?q?ghts=20=EC=9B=90=EB=B3=B8=20=EC=9D=91=EB=8B=B5=20=ED=99=95?= =?UTF-8?q?=EC=9D=B8=20=EC=8A=A4=ED=81=AC=EB=A6=BD=ED=8A=B8(=EC=A7=84?= =?UTF-8?q?=EB=8B=A8,=20=EC=9D=BD=EA=B8=B0=20=EC=A0=84=EC=9A=A9)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 지출·노출·클릭만 0 으로 들어오는 원인 확인용 — 저장된 인증정보로 하루치를 조회해 원본 JSON(토큰 가림)을 출력. Co-Authored-By: Claude Opus 5.5 --- scripts/meta_insights_raw.py | 60 ++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 scripts/meta_insights_raw.py diff --git a/scripts/meta_insights_raw.py b/scripts/meta_insights_raw.py new file mode 100644 index 0000000..f115cf6 --- /dev/null +++ b/scripts/meta_insights_raw.py @@ -0,0 +1,60 @@ +"""메타 광고 insights 원본 응답 확인(진단, 읽기 전용) — 실적 설정에 저장된 메타 인증정보로 하루치를 조회해 원본 JSON 을 보여 준다. + +2026-10-21: 광고비 탭 메타 일자별 보고서에서 구매수·링크 클릭·구매 전환값은 들어오는데 지출 금액·노출수·클릭수만 0 → + 메타가 그 칸을 어떤 모양으로 보내는지 원본을 보려고 만듦. DB·메타 설정은 바꾸지 않는다(GET 만). 토큰·appsecret_proof 는 출력하지 않는다. + +운영 서버(웹 컨테이너 dbx-main 안에서, 호스트의 이 파일을 stdin 으로 넘긴다 — 이미지 다시 빌드 불필요): + cd /opt/www/main && git pull && docker exec -i dbx-main python - 2026-10-20 < scripts/meta_insights_raw.py +날짜를 빼면 어제(KST). +""" +from __future__ import annotations + +import json +import os +import re +import sys +from datetime import date, timedelta + +import httpx + +from app.modules.performance.ad_sources.meta_ads import API_BASE, INSIGHT_FIELDS, MetaAdsSource +from app.modules.performance.ad_sync import AdSyncService, ad_credentials +from app.modules.performance.common import key_ring +from app.modules.performance.db import PerformanceDBStore +from app.timezone import today_kst + + +def _redact(obj: object, secrets: list[str]) -> object: + text = json.dumps(obj, ensure_ascii=False, indent=2) + for s in secrets: + if s: + text = text.replace(s, "***") + return re.sub(r"(access_token|appsecret_proof)=[^&\"\s]+", r"\1=***", text) + + +def main() -> None: + day = date.fromisoformat(sys.argv[1]) if len(sys.argv) > 1 else today_kst() - timedelta(days=1) + store = PerformanceDBStore(os.environ["PERFORMANCE_DB_URL"]) + sync = AdSyncService(store, ad_credentials(store, key_ring)) + ctx = sync._ctx("meta") + src = MetaAdsSource() + accounts, version, auth = src._setup(ctx) + secrets = [v for v in auth.values() if v] + tr = json.dumps({"since": day.isoformat(), "until": day.isoformat()}) + print(f"# 메타 insights 원본 — {day} · API {version} · 계정 {len(accounts)}개") + with httpx.Client(timeout=30.0) as client: + for acc in accounts: + url = f"{API_BASE}/{version}/act_{acc}/insights" + for label, fields in (("앱이 쓰는 요청", INSIGHT_FIELDS), ("지출·노출·클릭만", "spend,impressions,clicks,account_currency")): + resp = client.get(url, params={**auth, "level": "account", "time_increment": 1, "fields": fields, "time_range": tr}) + try: + body: object = resp.json() + except ValueError: + body = resp.text[:2000] + print(f"\n## act_{acc} · {label} · HTTP {resp.status_code}") + print(_redact(body, secrets)) + store.close() + + +if __name__ == "__main__": + main()