
2014年1月5日: シェルスクリプトの内容を更新しました。
iPhoneアプリのダウンロードレポートをSQLで管理したい
iTunes ConnectのiPhone/iPadアプリダウンロードレポートは保存期間が短い上に、得られる情報が少ない。 Google Analyticsくらいまで細かい分析結果が得られると嬉しいのだけど・・・。 とりあえず、iPhoneアプリのダウンロード数の日次レポートを自動取得するように設定してみた。
iTunes Connectレポートの自動取得方法
Appleからレポート取得用ライブラリ(Java)が提供されています。 このライブラリを使えば、itunes connectの”Sales and Trends”にあるデータを全て取得できるようです。 ドキュメントは下記参照↓
iTunes Connect Sales and Trends Guide
Javaが必要、大きな流れとしては、こんな感じでしょうか。 サーバーの基本的な設定は済んであるものとしています。
- サーバー(VPS)にJAVAをインストール
- Autoingestion.classをダウンロード
- レポート取得用のコード作成 (シェルスクリプト)
- CRON設定
ちなみに使用しているサーバーはさくらのVPSでCentOS6 x86_64です。
1. サーバー(VPS)にJAVAをインストール
JDKをダウンロード
http://www.oracle.com/technetwork/java/javase/downloads/index.html 「Java SE 6 Update 33」リンクからjdk-6u33-linux-x64-rpm.binをインストール
ダウンロードしたJDK-RPMファイルをサーバーへ移動(wgetできないのでMac経由)
JDKをインストール
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
[]# ./jdk-6u33-linux-x64-rpm.bin Unpacking... Checksumming... Extracting... UnZipSFX 5.50 of 17 February 2002, by Info-ZIP (Zip-Bugs@lists.wku.edu). inflating: jdk-6u33-linux-amd64.rpm inflating: sun-javadb-common-10.6.2-1.1.i386.rpm inflating: sun-javadb-core-10.6.2-1.1.i386.rpm inflating: sun-javadb-client-10.6.2-1.1.i386.rpm inflating: sun-javadb-demo-10.6.2-1.1.i386.rpm inflating: sun-javadb-docs-10.6.2-1.1.i386.rpm inflating: sun-javadb-javadoc-10.6.2-1.1.i386.rpm 準備中... ########################################### [100%] 1:jdk ########################################### [100%] Unpacking JAR files... rt.jar... jsse.jar... charsets.jar... tools.jar... localedata.jar... plugin.jar... javaws.jar... deploy.jar... Installing JavaDB 準備中... ########################################### [100%] 1:sun-javadb-common ########################################### [ 17%] 2:sun-javadb-core ########################################### [ 33%] 3:sun-javadb-client ########################################### [ 50%] 4:sun-javadb-demo ########################################### [ 67%] 5:sun-javadb-docs ########################################### [ 83%] 6:sun-javadb-javadoc ########################################### [100%] Java(TM) SE Development Kit 6 successfully installed. Product Registration is FREE and includes many benefits: * Notification of new versions, patches, and updates * Special offers on Oracle products, services and training * Access to early releases and documentation Product and system data will be collected. If your configuration supports a browser, the JDK Product Registration form will be presented. If you do not register, none of this information will be saved. You may also register your JDK later by opening the register.html file (located in the JDK installation directory) in a browser. For more information on what data Registration collects and how it is managed and used, see: http://java.sun.com/javase/registration/JDKRegistrationPrivacy.html Press Enter to continue..... Done. |
環境変数を設定
1 2 3 4 5 6 |
[]# vi /etc/profile // 下記を追加 export JAVA_HOME=/usr/java/default export PATH=$PATH:$JAVA_HOME/bin export CLASSPATH=.:$JAVA_HOME/jre/lib:$JAVA_HOME/lib:$JAVA_HOME/lib/tools.jar []# source /etc/profile |
2. Autoingestion.classをダウンロード
下記URLからダウンロードしておきます。
1 |
[]# wget http://www.apple.com/itunesnews/docs/Autoingestion.class.zip |
3. レポート取得用のコード作成 (シェル)
下記のようなコマンドで取得できるみたいです。 取得形式はTSVでした。
java Autoingestion <username> <password> <vendorid> <report_type> <date_type> <report_subtype> <date_yyyymmdd>
Variable | Value | Notes |
---|---|---|
username ユーザー名 |
Your user name | The user name you use to log into iTunes Connect |
password パスワード |
Your password | The password you use to log into iTunes Connect |
vendorid ベンダーID |
8####### (your unique | Vendor ID for the entity which you want to download the report |
report_type レポートタイプ |
Sales | This is the report type you want to download. Currently only Sales Reports are available. |
date_type データタイプ |
Daily or Weekly | Selecting Weekly will provide you the Weekly version of the report. Selecting Daily will provide you |
report_subtype レポートタイプ |
Summary or Opt-In | This is the parameter for the Sales Reports. |
date 日付 |
YYYYMMDD | This is the date of report you are requesting. If the value for Date parameter is not provided, you will get the latest report available. |
シェルスクリプトを書いてみました。流れとしてはこんな感じ。
- iTunesConnectからデイリーレポートをダウンロード
- ダウンロードしたZipファイル解凍 & データを少し加工(インポートできる形に)
- Sqlite3にインポート
- 全データをTSV出力 (ついでに)
ディレクトリ構成は以下の通り。
1 2 3 4 5 |
./myitunesconnect ∟ getDailyReport.sh ∟ report.sqlite ∟ Autoingestion ∟ Autoingestion.class |
前準備として、SQlite3のテーブルを作成しておきます。
1 2 3 4 |
# テーブルを作成 sqlite3 $SQLITE_FILE "CREATE TABLE logs ('Provider' text,'Provider Country' text,'SKU' text, 'Developer' text,'Title' text,'Version' text, 'Product Type Identifier' text, 'Units' integer,'Developer Proceeds' integer,'Begin Date' date,'End Date' date,'Customer Currency' text,'Country Code' text,'Currency of Proceeds' text,'Apple Identifier' text,'Customer Price' text,'Promo Code' text,'Parent Identifier' text,'Subscription' text,'Period' integer, 'Category' text, UNIQUE('SKU','Title','Version','Product Type Identifier','Units','Developer Proceeds','Begin Date','Customer Currency','Country Code','Currency of Proceeds','Apple Identifier','Customer Price','Promo Code','Parent Identifier','Subscription','Period', 'Category'));" # テーブルを削除する場合 # # sqlite3 $SQLITE_FILE "DROP TABLE logs;" |
getDailyReport.sh
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
#!/bin/sh echo "*** Start get itunes connect logs and import sqlite. ***" # このファイルのパスを取得 ROOT_DIR=`echo $(cd $(dirname $0);pwd)` # Autoingestion.classがあるディレクトリ AUTO_INGES_PATH=$ROOT_DIR/Autoingestion # iTunes ConnectのID VENDOR_ID={あなたのiTunes ConnectのID} # iTunes ConnectのユーザーID USER={あなたのiTunes ConnectのユーザーID} # iTunes Connectのパスワード PASS={あなたのiTunes Connectのパスワード} # 取得したいデータ・タイプ TYPE="Sales Daily Summary" # 取得したいデータの日付 DATE=`date --date="5 days ago" +"%Y%m%d"` # 取得したZipファイル名 FILENAME_COMPRESSED="S_D_"$VENDOR_ID"_"$DATE".txt.gz" # Zipファイル解凍後のテキストファイル FILENAME="S_D_"$VENDOR_ID"_"$DATE".txt" # SQLITEのパス SQLITE_FILE=$ROOT_DIR/report.sqlite echo "* root dir = $ROOT_DIR" echo "* sqlite file = $SQLITE_FILE" echo "* download file = $AUTO_INGES_PATH/$FILENAME_COMPRESSED" # パス移動 cd $AUTO_INGES_PATH # 本日のログ・ファイルが存在しているか確認 if [ ! -e $AUTO_INGES_PATH/$FILENAME ]; then echo "** Import report data to sqlite. **" # レポートを取得する java Autoingestion $USER $PASS $VENDOR_ID $TYPE $DATE # レポートを取得できたか確認 echo $AUTO_INGES_PATH/$FILENAME_COMPRESSED if [ ! -e $AUTO_INGES_PATH/$FILENAME_COMPRESSED ]; then echo "** Failure download log file" exit 1 fi # レポートを解凍する gunzip $FILENAME_COMPRESSED # 1行目を削除する sed -i -e "1d" $FILENAME # 日付データを入れ替える(SQLiteにインポートするため) sed -i -e "s/\([0-9][0-9][0-9][0-9]\)\/\([0-9][0-9]\)\/\([0-9][0-9]\)/\3\/\1\/\2/g" $FILENAME # Sqlite3にインポートする sqlite3 -separator $'\t' $SQLITE_FILE ".import $AUTO_INGES_PATH/$FILENAME logs" else echo "** Report file already exists. **" exit 1 fi # CSV出力する場合 CSV_FILE=$ROOT_DIR"/all_logs.csv" sqlite3 $SQLITE_FILE "select * from logs ORDER BY `End Date` desc;" |sed -e "s/|/\t/g" > $CSV_FILE # SQLite内のデータを表示 sqlite3 $SQLITE_FILE "select * from logs;" |
※ デイリーレポートでは、過去30日までしか取れないみたいです。30日以降を指定すると下記のエラーメッセージが返ってきます。
1 |
Daily reports are available only for past 30 days, please enter a date within past 30 days. |
4. CRON設定
一日一回動かします。(パスは自分の環境に合わせ変更)
1 2 |
[]# crontab -e 00 06 * * * /bin/sh /home/username/myitunesconnect/getDailyReport.sh |
レポートの形式について
気になるダウンロードできるレポートの形式ですが、上でも書きましたがTSV形式で、項目は下記の通りです。(「Category」は途中で追加されました。)
Provider | APPLE |
---|---|
Provider Country | US |
SKU | {あなたのアプリのSKU} |
Developer | {あなたのDeveloper名} |
Title | {あなたのアプリタイトル} |
Version | 2.01 |
Product Type Identifier | 1T |
Units | 13 |
Developer Proceeds | 0 |
Begin Date | 7/12/12 |
End Date | 7/12/12 |
Customer Currency | JPY |
Country Code | JP |
Currency of Proceeds | JPY |
Apple Identifier | {アプリのApple Identifier} |
Customer Price | 0 |
Promo Code | |
Parent Identifier | |
Subscription | |
Period | |
Category |
上記で「Product Type Identifier」という項目だけがよくわからなかったのですが、 それもドキュメントに下記のように書いてありました。 何のアプリなのか、新規なのか更新なのかということみたいです。
Product Type | Identifier Type | Description |
---|---|---|
1 | Free or Paid Apps | iPhone and iPod Touch,iOS | 7 | Updates | iPhone and iPod Touch,iOS |
IA1 | In Apps | Purchase, iOS |
IA9 | In Apps | Subscription, iOS |
IAY | In Apps | Auto-Renewable Subscription, iOS |
IAC | In Apps | Free Subscription, iOS |
1F | Free or Paid Apps | Universal, iOS |
7F | Updates | Universal, iOS |
1T | Free or Paid Apps | iPad, iOS |
7T | Updates | iPad,iOS |
F1 | Free or Paid Apps | Mac OS |
F7 | Updates | Mac OS |
FI1 | In Apps | Mac OS |
1E | Paid Apps | Custom iPhone and iPod Touch, iOS |
1EP | Paid Apps | Custom iPad, iOS |
1EU | Paid Apps | Custom Universal, iOS |