# Google Ads Remarketing Export Integration CLI

## 'td' コマンドのインストール

最新の[TD Toolbelt](https://toolbelt.treasuredata.com/)をインストールします。

## Google OAuth2 認証情報の作成

OAuth認証情報(`client_id`と`client_secret`)を作成するには、以下の手順を実行します。

1. Google API Console Credentialsを開きます:[https://console.developers.google.com/apis/credentials](https://console.developers.google.com/apis/credentials)
2. プロジェクトドロップダウン(左上隅)から、**New Project**をクリックし、プロジェクト名を入力し、必要に応じて提供されたProject IDを編集します。**Create**をクリックします。


![](/assets/image-20191015-160905.caa8b1cce14fbbb9c01caa3b70963985095e89e53e2c9836292726eb4f5a5da7.381d5938.png)

1. Credentialsページの「Create credentials」タブで、**OAuth client ID**を選択します。
2. Consent画面でプロダクト名を設定するよう求められる場合があります。その場合は**Configure consent screen**を選択し、要求された情報を入力して**Save**をクリックし、Credentials画面に戻ります。
3. Application Typeに「Web Application」を選択します。OAuth認証情報の名前を入力します。「Authorized redirect URIs」にGoogle OAuth playground URLを必ず追加してください。



```
https://developers.google.com/oauthplayground
```

![](/assets/image-20191015-160917.c527c0b2f63ed9eab48d2decc1395086c4920fc2292223e959db3a620f2a2fd0.381d5938.png)

1. **Create**を選択します。
2. client IDとclient secretをクリップボードにコピーします。コネクタージョブを設定する際にこの情報が必要になります。


![](/assets/image-20191015-160925.b9931ce15fa920878c8811651e735d917c808063fa76ca0f24488604939e513b.381d5938.png)

詳細については、[Googleの認証情報に関する情報](https://developers.google.com/adwords/api/docs/guides/authentication#webapp)を参照してください。

## Adsアカウントの認証をOAuth認証情報に紐付ける

Google OAuth Playgroundを使用して、OAuth認証情報から`refresh_token`を生成できます。このプロセスは、OAuthアプリがAdsアカウントにアクセスすることを認証するためのものです。

1. [https://developers.google.com/oauthplayground](https://developers.google.com/oauthplayground)にアクセスします。
2. 右上隅の**Settings**を選択し、**Use your own OAuth credentials**にチェックを入れます。
3. 前のセクションの「OAuth Client ID」と「OAuth Client Secret」を入力します。
4. 必要なスコープ(OAuth2権限)を以下のように入力します。



```
https://www.googleapis.com/auth/adwords https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email
```

![](/assets/image-20191015-160940.bc3fc4fddf91450a594060edc259a955458f58daee8f51806d515370c76a8b87.381d5938.png)

1. **Authorize APIs**を選択し、Google Adsアカウントにログインします。
2. OAuth2 Playgroundにリダイレクトされたら、**Exchange authorization codes for tokens**を選択します。


![](/assets/image-20191015-160952.f30746ee731818a9d52d272f96540aaf935a9cd5296e0d9dcfad3ec212819438.381d5938.png)

## クエリコマンドの準備

`td query`コマンドの`-r` / `--result`オプションを使用して、Google Ads Remarketingの結果出力先を追加します。


```
$ td query -d mydb 'SELECT email FROM app_users' -r '{"type":"google_adwords","client_id":"773#######################################j33.apps.googleusercontent.com","client_secret":"A######################N","developer_token":"9####################Q","refresh_token":"1/CG#######################################KI","client_customer_id":"111-222-3333","name":"abc_app_user_list","description":"User list from mobile app abc","app_id":"abc","mode":"append","membership_lifespan":10000,"batch_size":100000,"maximum_retries":5,"initial_retry_interval_millis":500,"maximum_retry_interval_millis":300000}'
```

パラメータ:

- `client_id`: (必須) Google OAuth2認証情報
- `client_secret`: (必須) Google OAuth2認証情報
- `refresh_token`: (必須) Google OAuth2認証情報
- `developer_token`: (必須) Google Ads Developer Token
- `client_customer_id`: (必須) Google Ads Customer ID。形式: `nnn-nnn-nnnn` (`n`は数字)
- `name`: (必須) ユーザーリストの名前
- `description`: (オプション) ユーザーリストの説明。デフォルト: *空*
- `app_id`: モバイルアプリケーションID(`mobile_id`エクスポートの場合のみ必須)
- `mode`: (オプション) アップロードモード。デフォルト: append。サポートされる値:
  - `append`
  - `replace`
  - `remove`
- `membership_lifespan`: (オプション) ユーザーの連絡先情報がユーザーリストに保持される日数。デフォルト: `10000`
- `batch_size`: (オプション) 各バッチでアップロードされるレコード数。デフォルト: `100000`
- `maximum_retries`: (オプション) システムが諦めるまでのリトライ回数。デフォルト: `5`
- `initial_retry_interval_millis`: (オプション) 初期リトライ待機時間(ミリ秒単位)。デフォルト: `500`(0.5秒に相当)
- `maximum_retry_interval_millis`: (オプション) 最大リトライ待機時間(ミリ秒単位)。デフォルト: `300000`(5分に相当)