95 lines
3.2 KiB
Bash
Executable File
95 lines
3.2 KiB
Bash
Executable File
#!/bin/sh
|
||
set -eu
|
||
|
||
SCRIPT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
|
||
export REPORT_DIR="$SCRIPT_DIR"
|
||
|
||
python3 - <<'PY'
|
||
import csv
|
||
import json
|
||
import os
|
||
from collections import defaultdict
|
||
from datetime import datetime, timezone
|
||
from pathlib import Path
|
||
|
||
root = Path(os.environ["REPORT_DIR"])
|
||
package = "com.amazon.mShop.android.shopping"
|
||
geo = "ZA"
|
||
|
||
spend = defaultdict(float)
|
||
with (root / "source-delivery.csv").open(encoding="utf-8-sig", newline="") as f:
|
||
for row in csv.DictReader(f):
|
||
if row.get("广告包") == package and row.get("国家") == geo:
|
||
day = row.get("日期", "")
|
||
if len(day) == 8 and day.isdigit():
|
||
spend[day] += float(row.get("平台消耗") or 0)
|
||
|
||
events = defaultdict(lambda: defaultdict(int))
|
||
with (root / "source-events.csv").open(encoding="utf-8-sig", newline="") as f:
|
||
reader = csv.reader(f)
|
||
next(reader, None)
|
||
for row in reader:
|
||
if len(row) < 7 or row[2] != package or row[3] != geo:
|
||
continue
|
||
name, day = row[0], row[1]
|
||
if name not in {"af_app_opened", "event1", "event2"}:
|
||
continue
|
||
# The source export leaves its last three headers blank. The second
|
||
# numeric column after midas_campaign_id is the repostback count.
|
||
try:
|
||
repostback = int(float(row[6] or 0))
|
||
except (ValueError, TypeError):
|
||
repostback = 0
|
||
key = "install" if name == "af_app_opened" else name
|
||
events[day][key] += repostback
|
||
|
||
days = sorted(set(spend) | set(events))
|
||
daily = []
|
||
for day in days:
|
||
install = events[day]["install"]
|
||
event1 = events[day]["event1"]
|
||
event2 = events[day]["event2"]
|
||
conversions = event1 + event2
|
||
daily.append({
|
||
"date": f"{day[:4]}-{day[4:6]}-{day[6:]}",
|
||
"install": install,
|
||
"event1": event1,
|
||
"event2": event2,
|
||
"e1e2": conversions,
|
||
"e1e2_rate": round(conversions / install * 100, 2) if install else None,
|
||
"midas_spend": round(spend[day], 2),
|
||
"payable_spend": round(conversions * 3, 2),
|
||
})
|
||
|
||
totals = {
|
||
"install": sum(x["install"] for x in daily),
|
||
"event1": sum(x["event1"] for x in daily),
|
||
"event2": sum(x["event2"] for x in daily),
|
||
"e1e2": sum(x["e1e2"] for x in daily),
|
||
"midas_spend": round(sum(x["midas_spend"] for x in daily), 2),
|
||
"payable_spend": round(sum(x["payable_spend"] for x in daily), 2),
|
||
}
|
||
totals["e1e2_rate"] = round(totals["e1e2"] / totals["install"] * 100, 2) if totals["install"] else None
|
||
|
||
payload = {
|
||
"meta": {
|
||
"package": package,
|
||
"geo": geo,
|
||
"generated_at": datetime.now(timezone.utc).isoformat(timespec="seconds"),
|
||
"date_start": daily[0]["date"] if daily else None,
|
||
"date_end": daily[-1]["date"] if daily else None,
|
||
"currency": "USD",
|
||
"install_definition": "af_app_opened repostback",
|
||
"event_definition": "event1/event2 repostback",
|
||
"rate_definition": "(event1 + event2) / Install",
|
||
"payable_definition": "(event1 + event2) × $3",
|
||
},
|
||
"totals": totals,
|
||
"daily": daily,
|
||
}
|
||
|
||
tmp = root / "data.json.tmp"
|
||
tmp.write_text(json.dumps(payload, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
|
||
tmp.replace(root / "data.json")
|
||
PY
|