diff --git a/app/modules/performance/CLAUDE.md b/app/modules/performance/CLAUDE.md index 7fd78a3..3e78db4 100644 --- a/app/modules/performance/CLAUDE.md +++ b/app/modules/performance/CLAUDE.md @@ -58,7 +58,8 @@ - 칸 = "광고비(ROAS%)"(ROAS = 그 쇼핑몰 매출 ÷ 광고비). 구글·메타·네이버는 계산 안 함 — 광고 보고서 ROAS(마이그레이션 006 `roas`·`metrics`). - 날짜 칸 직접 입력 + 쿠팡(윙)·쿠팡(로켓)·네이버 광고 보고서 업로드. - 쿠팡: 1열 날짜·19열 청구금액(+부가가치세). - - 네이버: 3열 기간·4열 총비용·12열 ROAS, 머리글 클릭 = 일자별 보고서 항목. + - 네이버: 3열 기간·4열 총비용·12열 ROAS(구매완료 광고수익률(%)), 머리글 클릭 = 일자별 보고서 항목. + - 일자별 보고서 창: ROAS 열은 **광고비 열 바로 오른쪽**, 네이버 "구매완료 광고수익률(%)" 머리글은 "ROAS(%)"로 표기(`ad_spend._roas_next_to_spend` → `labels`, 저장 키는 파일 머리글 그대로 — 예전 업로드도 그대로 적용). - 매출 대비 광고비 KPI. - 광고 API 연동(마이그레이션 007 `perf_ad_credentials`·`perf_ad_source_status`) — 실적 설정 **광고 API 연동** 카드, 쇼핑몰과 같은 마스터 키로 암호화(AAD `perf:ad:…`), `ad_sources/`(채널 하나 = 파일 하나) + `ad_sync.py`, 매출 자동 업데이트 회차에 함께. 결과는 `perf_ad_spend` `source='api'`(초록 숫자, API 가 안 준 날은 예전 API 값만 지움). - **구글 = Google Ads API**(2026-10-15) — 고객 ID·(MCC)·서비스 계정 JSON·API 버전(기본 v26). 개발자 토큰은 2026-09-09 구글 폐지 — 접근 등급은 서비스 계정의 Cloud 프로젝트, 머리글 안 보냄. 광고비 탭 [구글 광고 불러오기](보는 기간). 광고비 = 비용 × 1.1(부가세 포함)·ROAS(전환 가치 ÷ 비용, 구글 값 그대로)·항목. diff --git a/app/modules/performance/ad_spend.py b/app/modules/performance/ad_spend.py index a160aac..bf49839 100644 --- a/app/modules/performance/ad_spend.py +++ b/app/modules/performance/ad_spend.py @@ -378,7 +378,30 @@ def detail_table(rows: list[dict[str, Any]]) -> dict[str, Any]: out_rows = [{"date": r["date"], "amount": int(r.get("amount") or 0), "roas": float(r["roas"]) if r.get("roas") is not None else None, "values": {k: v for k, v in (r.get("metrics") or {}).items() if not k.startswith("_")}} for r in rows] - return {"columns": cols, "rows": out_rows, "total": total} + cols, labels = _roas_next_to_spend(cols) + return {"columns": cols, "labels": labels, "rows": out_rows, "total": total} + + +# 상세 표의 광고비 열 — 네이버 "총비용"(4열), 구글 "광고비(부가세 포함)", 메타 "지출 금액(부가세 포함)" +_SPEND_COL_MARKS = ("총비용", "광고비(", "지출 금액(부가세") + + +def _roas_next_to_spend(cols: list[str]) -> tuple[list[str], dict[str, str]]: + """ROAS 열을 광고비 열 바로 오른쪽으로 옮긴다(2026-10-20 사용자 요청 — 네이버 파일은 12열이라 멀리 떨어져 있었다). + 네이버 "구매완료 광고수익률(%)" 은 머리글을 "ROAS(%)" 로 보여 준다(값·키는 파일 머리글 그대로, labels 로만). + 반환: (열 순서, {열: 화면 머리글}).""" + roas_col = next((k for k in cols if "수익률" in k or "ROAS" in k.upper()), None) + spend_col = next((k for k in cols if any(m in k for m in _SPEND_COL_MARKS)), None) + labels: dict[str, str] = {} + if roas_col is None: + return cols, labels + if "ROAS" not in roas_col.upper(): + labels[roas_col] = "ROAS(%)" + if spend_col is None: + return cols, labels + out = [k for k in cols if k != roas_col] + out.insert(out.index(spend_col) + 1, roas_col) + return out, labels def detail_tabs(rows: list[dict[str, Any]]) -> dict[str, Any]: diff --git a/app/modules/performance/templates/performance/ads.html b/app/modules/performance/templates/performance/ads.html index 06044d5..735979e 100644 --- a/app/modules/performance/templates/performance/ads.html +++ b/app/modules/performance/templates/performance/ads.html @@ -224,4 +224,4 @@ } | tojson }} {% endblock %} -{% block scripts %}{% endblock %} +{% block scripts %}{% endblock %} diff --git a/app/modules/performance/templates/performance/index.html b/app/modules/performance/templates/performance/index.html index c85b2ca..045cfda 100644 --- a/app/modules/performance/templates/performance/index.html +++ b/app/modules/performance/templates/performance/index.html @@ -274,4 +274,4 @@ } | tojson }} {% endblock %} -{% block scripts %}{% endblock %} +{% block scripts %}{% endblock %} diff --git a/app/modules/performance/templates/performance/settings.html b/app/modules/performance/templates/performance/settings.html index 2e9661c..49728dd 100644 --- a/app/modules/performance/templates/performance/settings.html +++ b/app/modules/performance/templates/performance/settings.html @@ -219,4 +219,4 @@ {% endblock %} -{% block scripts %}{% endblock %} +{% block scripts %}{% endblock %} diff --git a/app/modules/performance/tests/test_ad_spend.py b/app/modules/performance/tests/test_ad_spend.py index e494327..b8353cc 100644 --- a/app/modules/performance/tests/test_ad_spend.py +++ b/app/modules/performance/tests/test_ad_spend.py @@ -276,7 +276,9 @@ def test_naver_detail_table_totals(): {"date": "2026-09-24", "amount": 0, "roas": Decimal("0"), "metrics": {"총비용": 0, "노출수": 0, "클릭수": 0, "클릭률(%)": 0.0, "구매완료 광고수익률(%)": 0}}] t = ads.detail_table(rows) - assert t["columns"] == ["총비용", "노출수", "클릭수", "클릭률(%)", "구매완료 광고수익률(%)"] + # ROAS(12열)는 광고비(총비용) 바로 오른쪽, 머리글은 "ROAS(%)"(값 키는 파일 머리글 그대로) + assert t["columns"] == ["총비용", "구매완료 광고수익률(%)", "노출수", "클릭수", "클릭률(%)"] + assert t["labels"] == {"구매완료 광고수익률(%)": "ROAS(%)"} assert t["total"]["총비용"] == 600489 and t["total"]["노출수"] == 61104 and t["total"]["클릭수"] == 902 assert t["total"]["클릭률(%)"] == round(902 * 100 / 61104, 2) # 가중: 클릭수 ÷ 노출수 assert t["total"]["구매완료 광고수익률(%)"] == 133.09 # 광고비 가중 평균 (198.79×299,817 + 67.58×300,672) ÷ 600,489 diff --git a/app/static/performance.js b/app/static/performance.js index 07456a3..0fda688 100644 --- a/app/static/performance.js +++ b/app/static/performance.js @@ -434,8 +434,12 @@ }; const table = function (t) { const tcols = t.columns || cols; + const labels = t.labels || res.labels || {}; return '
| 날짜 | ' + - tcols.map(function (c) { return "" + esc(c) + " | "; }).join("") + "' + esc(labels[c]) + " | " : "" + esc(c) + " | "; + }).join("") + "" + (t.rows || []).map(function (r) { return '
|---|---|
| ' + esc(day(r.date)) + " | " + tcols.map(function (c) { return '' + cell((r.values || {})[c]) + " | "; }).join("") + "