# Fastlane Spaceship 數(shù)據(jù)采集 Demo
以下是通過 **Fastlane Spaceship** 自動獲取 App Store Connect 銷售數(shù)據(jù)的完整指南:
---
## 環(huán)境準(zhǔn)備
### 1. 安裝 Fastlane
```bash
# 需要 Ruby 2.5 或更高版本
gem install fastlane -NV
腳本實現(xiàn)
2. 創(chuàng)建 appstore_data_demo.rb
require 'spaceship'
require 'date'
require 'json'
# 配置憑證(建議使用環(huán)境變量)
APPLE_ID = ENV['APPSTORE_ID'] || "your_apple_id@example.com"
PASSWORD = ENV['APPSTORE_PWD'] || "your_app_specific_password"
def login
Spaceship::Tunes.login(APPLE_ID, PASSWORD)
Spaceship::Tunes.select_team if Spaceship::Tunes.client.teams.count > 1
end
def fetch_sales_data(date)
start_date = Date.parse(date)
end_date = start_date
all_apps = Spaceship::Tunes::Application.all
app = all_apps.first
analytics_data = Spaceship::Tunes.client.get_analytics_data(
app_id: app.apple_id,
start_date: start_date.strftime("%Y-%m-%d"),
end_date: end_date.strftime("%Y-%m-%d"),
measures: ['installs', 'sales', 'refunds', 'iap'],
frequency: 'DAILY'
)
JSON.parse(analytics_data.body)['results'] || []
end
begin
login
target_date = (Date.today - 3).strftime("%Y-%m-%d") # 數(shù)據(jù)延遲 2 天
sales_data = fetch_sales_data(target_date)
if sales_data.empty?
puts "?? No data for #{target_date}"
else
File.write("sales_#{target_date}.json", JSON.pretty_generate(sales_data))
puts "? Saved #{sales_data.size} records to sales_#{target_date}.json"
end
rescue => e
puts "? Error: #{e.message}"
end
運行說明
3. 執(zhí)行腳本
# 直接運行
ruby appstore_data_demo.rb
# 使用環(huán)境變量(推薦)
APPSTORE_ID="your_id" APPSTORE_PWD="your_pwd" ruby appstore_data_demo.rb
數(shù)據(jù)處理示例
4. 解析 JSON 輸出
import json
with open('sales_2023-10-01.json') as f:
data = json.load(f)
for item in data:
print(f"Date: {item['date']}")
print(f"App: {item['appName']}")
print(f"Revenue: ${item['developerProceeds']:.2f}")
print(f"Refunds: {item['refunds']}\n")
關(guān)鍵配置項
| 參數(shù) | 說明 |
|---|---|
measures |
指標(biāo)類型:installs/sales/refunds |
frequency |
數(shù)據(jù)粒度:DAILY/HOURLY |
start_date |
數(shù)據(jù)開始日期(YYYY-MM-DD) |
app.apple_id |
目標(biāo)應(yīng)用的唯一標(biāo)識符 |
注意事項
?? 權(quán)限要求
需在 App Store Connect 中分配 Sales 或 Analytics 權(quán)限?? 數(shù)據(jù)延遲
當(dāng)日(T)數(shù)據(jù)需在 T+3 日獲取(例如 10/1 數(shù)據(jù) 10/4 可用)?? 接口穩(wěn)定性
此腳本依賴蘋果未公開 API,重大更新后可能失效