# The Trade Desk Export Integration

Treasure Dataからジョブの結果をThe Trade Deskに直接書き込むことができます。The Trade Deskでは、各DSPターゲティングセグメントに広告を配信できます。

## 前提条件

- [TD Toolbelt](https://toolbelt.treasuredata.com/)を含むTreasure Dataの基本知識
- The Trade Deskのアカウント
- The Trade DeskへのTreasure アカウントアクセスの承認


## 制限事項

The Trade Desk Export Integrationは個人識別情報(PII)データをサポートしていません。

## インテグレーション概要

このインテグレーションは2つのパートで構成されています:

1. **The Trade Desk DMPとTreasure Data CDP間のCookie同期** — これはThe Trade Desk DMP IDとTreasure DataのIDであるtd_global_id間のマッピングを作成するために必要です。
2. **Treasure Data CPDからThe Trade Desk DMPへのデータエクスポート** — Cookie同期時に作成されたマッピングに基づいて、Treasure DataのIDは、自身のIDをキーとしたセグメントをThe Trade Desk DMPをキーとしたセグメントに変換します。その後、Treasure Dataの出力データコネクタを使用して、変換されたセグメントデータをThe Trade Deskアカウントにエクスポートできます。


**The Trade DeskとTreasure Data間のCookie同期**

![](/assets/trade-desk-integration.39f65762f4f5a83713aa583e6ca314a1e694e6f42e666d2d4d5e263438f457cb.7f5c9654.png)

**Treasure DataからThe Trade Deskへのデータ転送**

![](/assets/ttddataxfer.139600ac80280f4591a0d02797e91707aa19cd5d20a96abb3aeb3eaa74ec9be9.7f5c9654.png)

## Cookie同期タグの実装

まず、前のセクションで説明し、[JavaScriptSDKを使用したクロスドメイントラッキング](https://docs.treasuredata.com/smart/project-product-documentation/enabling-cross-domain-tracking-with-javascript-sdk)に記載されているように、Treasure DataのJavaScriptタグtd_global_idを設定する必要があります。

TTD Syncタグは各リージョンごとに提供されます。アクセスすると、`https://in.treasuredata.com/postback/v3/event/ttd/sync?td_format=pixel&td_write_key=%%TTD_PUID%%&td_global_id=td_global_id&td_ip=td_ip&ttd_id=%%TTD_TDID%%&td_ua=td_ua`にリダイレクトされます。これは`ttd`(DB)`sync`(テーブル)に保存されます。お客様はタグを実装する前にこれらを作成する必要があります。TD Write APIKEYはURLエンコードする必要があります。/を%2Fに変更してください。

- USリージョン: https://match.adsrvr.org/track/cmf/generic?ttd_pid=01qhnrr&ttd_tpi=1&ttd_puid={TD Write API KEYを貼り付けてください}
- EU01リージョン: https://match.adsrvr.org/track/cmf/generic?ttd_pid=996ppa1&ttd_tpi=1&ttd_puid={TD Write API KEYを貼り付けてください}
- Tokyoリージョン: https://match.adsrvr.org/track/cmf/generic?ttd_pid=6nh6mgj&ttd_tpi=1&ttd_puid={TD Write API KEYを貼り付けてください}


The Trade Desk DMPのタグがすでにインストールされているWebサイトに、以下のようなコードを追加します。以下のコードはEU01リージョンの例です。要件に基づいてコードを更新してください。


```html
<script type="text/javascript">
!function(t,e){if(void 0===e[t]){e[t]=function(){e[t].clients.push(this),this._init=[Array.prototype.slice.call(arguments)]},e[t].clients=[];for(var r=["addRecord","blockEvents","fetchServerCookie","fetchGlobalID","fetchUserSegments","resetUUID","ready","setSignedMode","setAnonymousMode","set","trackEvent","trackPageview","trackClicks","unblockEvents"],s=0;s<r.length;s++){var c=r[s];e[t].prototype[c]=function(t){return function(){return this["_"+t]=this["_"+t]||[],this["_"+t].push(Array.prototype.slice.call(arguments)),this}}(c)}var n=document.createElement("script");n.type="text/javascript",n.async=!0,n.src=("https:"===document.location.protocol?"https:":"http:")+"//cdn.treasuredata.com/sdk/4.0/td.min.js";var o=document.getElementsByTagName("script")[0];o.parentNode.insertBefore(n,o)}}("Treasure",this);

function createIframe(iframeSrc) {
  var gdpr = false;
  var gdpr_consent = '';

  // The parameter value provides information about vendor consent.
  // iframe not necessary but a recommendation given by The Trade Desk
  var iframe = document.createElement('iframe');
  iframe.src = iframeSrc;
  iframe.height = 1;
  iframe.width = 1;
  iframe.style.border = 'none';
  document.body.appendChild(iframe);

  function loadPixel(pixelSrc) {
    var pixel = document.createElement('img');
    pixel.src = pixelSrc;
    pixel.width = 1;
    pixel.height = 1;
    pixel.style.display = 'none';
    iframe.contentWindow.document.body.appendChild(pixel);
  }

  // new TTD URL
  var ttd_url = 'https://match.adsrvr.org/track/cmf/generic?';
  var params = [
    'ttd_pid=996ppa1',
    'ttd_tpi=1',
    'ttd_puid=' + 'your td api key',
    `gdpr=${gdpr ? 1 : 0}`,
    `gdpr_consent=${gdpr ? gdpr_consent : ''}`,
    `domain=${(window != window.parent) ? (new URL(document.referrer).hostname) : window.location.host}`
  ];
  loadPixel(ttd_url + params.join('&'));
}

var iframeSrc = 'about:blank';
var td = new Treasure({
  host: 'eu01.in.treasuredata.com',
  writeKey: 'your td api key',
  database: '{{name}}_ttd', // db where you will store pageviews
  startInSignedMode: true,
  trackCrossDomain: true,
  cdpHost: 'cdp-eu01.in.treasuredata.com'
});
var tbl = 'sync';
td.setSignedMode();
td.set('$global', 'td_global_id', 'td_global_id');
td.trackPageview(tbl, createIframe(iframeSrc));
</script>
```

## Treasure コンソールを使用した接続の作成

Treasure コンソールを使用して接続を構成できます。

### The Trade Deskでの作業: Secret Keyの取得

**Your Advertiser > Preferences**に移動し、スクロールダウンして**Secret Key**を取得してください。

![](/assets/ttd_advertiser_pref.9cd11bf875f9a1d64b3e98413e570d333b0e0721c545c31e7c2d34c2bf56c183.7f5c9654.png)

### Treasure Dataでの作業: 新しい接続の作成

1. Treasure コンソールから、**Integration Hub**を選択します。
2. **Catalog**を選択します。
3. **The Trade Desk**出力コネクタを検索して選択します。
![](/assets/tradedeskoutput.9a703415a16009d454fe7e47c9a8d7b03d10d4ee5b4a1585afa37dd8f19ca476.7f5c9654.png)
4. **Create Authentication**を選択します。
New Authenticationダイアログが開きます。
![](/assets/tradedeskauthdialog2.2e6573bbefd15353cc941478944c0d93b9dd499e682694eb51110489ae0cccde.7f5c9654.png)
5. The Trade Deskから取得したSecret Keyを入力し、**Continue**を選択します。
6. 接続の名前を入力します。
![](/assets/tradedesknewauthb.1c83a3cb0dcb352f1b606312d0876c7ef00c89eb9f6f015a6d45062a4b51db64.7f5c9654.png)
7. **Done**を選択します。


The Trade Desk出力コネクタが作成されます。

### データ接続でのエクスポート結果の構成

このステップでは、クエリを作成または再利用します。クエリでは、データ接続を構成します。場合によっては、クエリでカラムマッピングを定義する必要があります。

### パラメータを指定した接続の構成

1. Treasure コンソールにアクセスし、**Data Workbench > Queries**に移動します。
2. データのエクスポートに使用するクエリを見つけて選択します。
3. クエリエディタの上部にある**Export Results**を選択します。
Choose Integrationダイアログが表示されます。
4. 検索ボックスにThe Trade Desk接続名を入力してフィルタリングし、接続を選択します。
5. パラメータを指定します。


**パラメータ**:

- **Advertiser ID**(必須): あなたのAdvertiser IDです。
- **Select host name**(必須): キャンペーンのターゲットユーザーの場所に最も近いホスト名を使用してください。デフォルト: TOK(Tokyo)
- **Recency targeting**(オプション): ターゲットオーディエンスの対象となるユーザーにタグが付けられるUTCタイムスタンプです。このフィールドを省略すると、The Trade Deskがデータを処理するとすぐにユーザーがセグメントに追加されたとみなされます。または、ユーザーがデータセグメントの対象となる日時を指定できます。UTCでタイムスタンプを指定してください。形式は yyyy-MM-dd HH:mm です。MMは月(分ではない)を表し、HHは24時間(12時間ではない)を表します。デフォルト: 空白
- **Data remains active in a duration of**(必須): Recency targetingを基準として、ユーザーがアクティブなまま保持される期間を指定します。期間が経過すると、データはターゲティングに使用されなくなります。Time-to-live(TTL)を分単位で指定してください。時間は259200分(180日)を超えることはできません。推奨される最小時間は2880分(48時間)です。
- **Max records per batch request**(オプション): 各バッチでアップロードするレコードの最大数です。デフォルト: 5000
- **Sleep Per API request in Milliseconds**(オプション): システムが各リクエストでスリープする時間をミリ秒単位で指定します。デフォルト: 2000(2秒に相当)
- **Retry Limit**(オプション): システムが諦めるまでのリトライ回数です。デフォルト: 7


### サンプル構成は以下の通りです:

#### The Trade Deskにデータを送信するクエリの例

Treasure Dataから、以下のクエリを実行し、The Trade Desk接続に結果を出力します。

**TDIDのクエリ例**


```sql
SELECT id AS tdid,
name AS dataname
FROM your_table;
```

**DAIDのクエリ例**


```sql
SELECT id AS daid,
name AS dataname
FROM your_table;
```

**UID2のクエリ例**


```sql
SELECT id AS uid2,
name AS dataname
FROM your_table;
```

**RampIDのクエリ例**


```sql
SELECT id AS rampid,
name AS dataname
FROM your_table;
```

### (オプション) Query Export ジョブをスケジュールする

Scheduled Jobs と Result Export を使用して、指定したターゲット宛先に出力結果を定期的に書き込むことができます。

Treasure Data のスケジューラー機能は、高可用性を実現するために定期的なクエリ実行をサポートしています。

2 つの仕様が競合するスケジュール仕様を提供する場合、より頻繁に実行するよう要求する仕様が優先され、もう一方のスケジュール仕様は無視されます。

例えば、cron スケジュールが `'0 0 1 * 1'` の場合、「月の日」の仕様と「週の曜日」が矛盾します。前者の仕様は毎月 1 日の午前 0 時 (00:00) に実行することを要求し、後者の仕様は毎週月曜日の午前 0 時 (00:00) に実行することを要求するためです。後者の仕様が優先されます。

#### Treasure コンソール を使用してジョブをスケジュールする

1. **Data Workbench > Queries** に移動します
2. 新しいクエリを作成するか、既存のクエリを選択します。
3. **Schedule** の横にある None を選択します。
![](/assets/image2021-1-15_17-28-51.f1b242f6ecc7666a0097fdf37edd1682786ec11ef80eff68c66f091bc405c371.0f87d8d4.png)
4. ドロップダウンで、次のスケジュールオプションのいずれかを選択します:
![](/assets/image2021-1-15_17-29-47.45289a1c99256f125f4d887e501e204ed61f02223fde0927af5f425a89ace0c0.0f87d8d4.png)
| ドロップダウン値 | 説明 |
|  --- | --- |
| Custom cron... | [Custom cron... の詳細](#custom-cron-details)を参照してください。 |
| @daily (midnight) | 指定されたタイムゾーンで 1 日 1 回午前 0 時 (00:00 am) に実行します。 |
| @hourly (:00) | 毎時 00 分に実行します。 |
| None | スケジュールなし。 |


#### Custom cron... の詳細

![](/assets/image2021-1-15_17-30-23.0f94a8aa5f75ea03e3fec0c25b0640cd59ee48d1804a83701e5f2372deae466c.0f87d8d4.png)

| **Cron 値** | **説明** |
|  --- | --- |
| `0 * * * *` | 1 時間に 1 回実行します。 |
| `0 0 * * *` | 1 日 1 回午前 0 時に実行します。 |
| `0 0 1 * *` | 毎月 1 日の午前 0 時に 1 回実行します。 |
| "" | スケジュールされた実行時刻のないジョブを作成します。 |



```
 *    *    *    *    *
 -    -    -    -    -
 |    |    |    |    |
 |    |    |    |    +----- day of week (0 - 6) (Sunday=0)
 |    |    |    +---------- month (1 - 12)
 |    |    +--------------- day of month (1 - 31)
 |    +-------------------- hour (0 - 23)
 +------------------------- min (0 - 59)
```

次の名前付きエントリを使用できます:

- Day of Week: sun, mon, tue, wed, thu, fri, sat.
- Month: jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec.


各フィールド間には単一のスペースが必要です。各フィールドの値は、次のもので構成できます:

div
| フィールド値  | 例  | 例の説明  |
|  --- | --- | --- |
| 各フィールドに対して上記で表示された制限内の単一の値。 |  |  |
| フィールドに基づく制限がないことを示すワイルドカード
`'*'`。 | `'0 0 1 * *'` | 毎月 1 日の午前 0 時 (00:00) に実行するようにスケジュールを設定します。 |
| 範囲 `'2-5'`
フィールドの許可される値の範囲を示します。 | `'0 0 1-10 * *'` | 毎月 1 日から 10 日までの午前 0 時 (00:00) に実行するようにスケジュールを設定します。 |
| カンマ区切りの値のリスト `'2,3,4,5'`
フィールドの許可される値のリストを示します。 | `0 0 1,11,21 * *'` | 毎月 1 日、11 日、21 日の午前 0 時 (00:00) に実行するようにスケジュールを設定します。 |
| 周期性インジケータ `'*/5'`
フィールドの有効な値の範囲に基づいて、
スケジュールが実行を許可される頻度を表現します。 | `'30 */2 1 * *'` | 毎月 1 日、00:30 から 2 時間ごとに実行するようにスケジュールを設定します。
`'0 0 */5 * *'` は、毎月 5 日から 5 日ごとに午前 0 時 (00:00) に実行するようにスケジュールを設定します。 |
| `'*'`
ワイルドカードを除く上記の
いずれかのカンマ区切りリストもサポートされています
`'2,*/5,8-10'` | `'0 0 5,*/10,25 * *'` | 毎月 5 日、10 日、20 日、25 日の午前 0 時 (00:00) に実行するようにスケジュールを設定します。 |


1. (オプション) Delay execution を有効にすることで、クエリの開始時刻を遅延させることができます。


### クエリを実行する

クエリに名前を付けて保存して実行するか、単にクエリを実行します。クエリが正常に完了すると、クエリ結果は指定された宛先に自動的にエクスポートされます。

設定エラーにより継続的に失敗するスケジュールジョブは、複数回通知された後、システム側で無効化される場合があります。

(オプション) Delay execution を有効にすることで、クエリの開始時刻を遅延させることができます。

## Audience Studio で Segment をアクティベートする

Audience Studio で activation を作成することで、segment データをターゲットプラットフォームに送信することもできます。

1. **Audience Studio** に移動します。
2. parent segment を選択します。
3. ターゲット segment を開き、右クリックして、**Create Activation** を選択します。
4. **Details** パネルで、Activation 名を入力し、前述の Configuration Parameters のセクションに従って activation を設定します。
5. **Output Mapping** パネルで activation 出力をカスタマイズします。


![](/assets/ouput.b2c7f1d909c4f98ed10f5300df858a4b19f71a3b0834df952f5fb24018a5ea78.8ebdf569.png)

- Attribute Columns
  - **Export All Columns** を選択すると、変更を加えずにすべての列をエクスポートできます。
  - **+ Add Columns** を選択して、エクスポート用の特定の列を追加します。Output Column Name には、Source 列名と同じ名前があらかじめ入力されます。Output Column Name を更新できます。**+ Add Columns** を選択し続けて、activation 出力用の新しい列を追加します。
- String Builder
  - **+ Add string** を選択して、エクスポート用の文字列を作成します。次の値から選択します:
    - String: 任意の値を選択します。テキストを使用してカスタム値を作成します。
    - Timestamp: エクスポートの日時。
    - Segment Id: segment ID 番号。
    - Segment Name: segment 名。
    - Audience Id: parent segment 番号。


1. **Schedule** を設定します。


![](/assets/snippet-output-connector-on-audience-studio-2024-08-28.a99525173709da1eb537f839019fa7876ffae95045154c8f2941b030022f792c.8ebdf569.png)

- スケジュールを定義する値を選択し、オプションでメール通知を含めます。


1. **Create** を選択します。


batch journey の activation を作成する必要がある場合は、[Creating a Batch Journey Activation](/products/customer-data-platform/journey-orchestration/batch/creating-a-batch-journey-activation) を参照してください。

## ワークフローでのエクスポート結果の構成(オプション)

Treasure ワークフロー内で、このデータコネクターを使用してデータを出力するように指定できます。


```yaml
timezone: UTC

_export:
  td:
    database: sample_datasets

+td-result-into-target:
  td>: queries/sample.sql
  result_connection: your_connections_name
  result_settings:
    secret_key: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    advertiser_id: xxxxxx
    data_activation_duration: 43200
    ....
```

データをエクスポートするには、[Workflowでデータコネクターを使用する方法の情報](https://docs.treasuredata.com/smart/project-product-documentation/using-integrations-to-import-data-with-td-workflows)を確認してください。

## TD ToolbeltとThe Trade Desk接続の使用

CLIからTD Toolbeltを使用して、The Trade Deskにエクスポートできます。

TDIDの例


```bash
td query \
  --result '{
    "type": "thetradedesk",
    "secret_key": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    "advertiser_id": xxxxxx,
    "data_activation_duration": 43200
  }' \
  -d sample_datasets \
  "SELECT tdid, dataname FROM (
     VALUES
       ('d46a3975-c267-404e-89ae-0a9ff0cdce5f', 'demo-segment'),
       ('244d97ec-c258-4359-ae80-c67bf92bd294', 'demo-segment'),
       ('f749cd45-c6e2-4a82-87fe-afb7d2cd1188', 'demo-segment')
   ) AS tbl (tdid, dataname)" \
  -T presto
```

DAIDの例


```bash
$ td query --result '{"type":"thetradedesk","secret_key":"xxxxxxxxxxxxxxxxxxxxxxxxxxxxx","advertiser_id":xxxxxx,"data_activation_duration":43200}' -d sample_datasets "SELECT daid, dataname FROM (
 VALUES ('d46a3975-c267-404e-89ae-0a9ff0cdce5f','demo-segment'),
 ('244d97ec-c258-4359-ae80-c67bf92bd294','demo-segment'),
 ('f749cd45-c6e2-4a82-87fe-afb7d2cd1188','demo-segment')
) tbl (daid, dataname)" -T presto
```

DAIDの例


```bash
td query \
  --result '{
    "type": "thetradedesk",
    "secret_key": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    "advertiser_id": xxxxxx,
    "data_activation_duration": 43200
  }' \
  -d sample_datasets \
  "SELECT daid, dataname FROM (
     VALUES
       ('d46a3975-c267-404e-89ae-0a9ff0cdce5f', 'demo-segment'),
       ('244d97ec-c258-4359-ae80-c67bf92bd294', 'demo-segment'),
       ('f749cd45-c6e2-4a82-87fe-afb7d2cd1188', 'demo-segment')
   ) AS tbl (daid, dataname)" \
  -T presto
```

RampIDの例


```bash
td query \
  --result '{
    "type": "thetradedesk",
    "secret_key": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    "advertiser_id": xxxxxx,
    "data_activation_duration": 43200
  }' \
  -d sample_datasets \
  "SELECT rampid, dataname FROM (
     VALUES
       ('XY1005wXyWPB1SgpMUKIpzA0I3UaLEz-2lg0wFAr1PWK7FMhs', 'demo-segment'),
       ('XY1005wXyWPB1SgpMUKIpzA0I3UaLEz-2lg0wFAr1PWK7FMhs', 'demo-segment'),
       ('XY1005wXyWPB1SgpMUKIpzA0I3UaLEz-2lg0wFAr1PWK7FMhs', 'demo-segment')
   ) AS tbl (rampid, dataname)" \
  -T presto
```