コネクテッドコンテンツのローカル変数
このページでは、ローカルのコネクテッドコンテンツ変数の概要と、それらの使用方法と保存方法について説明します。
Brazeは、connected_content
タグ内に指定されたエンドポイントに送信時に標準のGETリクエストを行います。エンドポイントがJSONを返す場合、それは自動的に解析され、connected
という変数に格納されます。エンドポイントがテキストを返す場合、それはconnected_content
タグの代わりにメッセージに直接挿入されます。
応答を変数に保存したい場合は、JSONオブジェクトを返すことをお勧めします。そして、コネクテッドコンテンツの応答でタグをテキストに置き換える場合は、応答が有効な JSON (json.org が定義) ではないことを確認してください。
URLの後に:save your_variable_name
を指定して、データを別のものとして保存することもできます。例えば、次のconnected_content
タグは、localweather
というローカル変数に応答を保存します(複数のconnected_content
JSON変数を保存できます):
1
{% connected_content https://www.metaweather.com/api/location/2459115/ :save localweather %}
Metaweatherは、地域の天気を返すために「Where-on-Earth ID」を使用する無料の天気APIです。テストと学習の目的でのみこのコードを使用してください。
格納された変数は、
connected_content
リクエストを含むフィールド内でのみアクセスできます。例えば、メッセージとタイトルフィールドの両方でlocalweather
変数を使用したい場合は、両方のフィールド内でconnected_content
リクエストを行う必要があります。リクエストが同一である場合、Brazeは送信先サーバーへの2回目のリクエストを行うのではなく、キャッシュされた結果を使用します。ただし、HTTP POST 経由で行われたコネクテッドコンテンツの呼び出しははデフォルトでキャッシュされず、送信先サーバーに 2 回目のリクエストを行います。POST呼び出しにキャッシュを追加したい場合は、cache_max_age
オプションを参照してください。
JSON解析
コネクテッドコンテンツは、:save
が指定されると、JSON 形式の結果をローカル変数に解釈します。例えば、天気関連のコネクテッドコンテンツエンドポイントは、次のJSONオブジェクトを返します。これを:save localweather
を指定してローカル変数localweather
に格納します。
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
{
"consolidated_weather": [
{
"id": 5.8143475362693e+15,
"weather_state_name": "Clear",
"weather_state_abbr": "c",
"wind_direction_compass": "WSW",
"created": "2017-06-12T14:14:46.268110Z",
"applicable_date": "2017-06-12",
"min_temp": 22.511666666667,
"max_temp": 31.963333333333,
"the_temp": 27.803333333333,
"wind_speed": 6.8884690250312,
"wind_direction": 251.62921994166,
"air_pressure": 1021.335,
"humidity": 50,
"visibility": 14.945530601288,
"predictability": 68
},
.
.
.
"title": "New York",
"location_type": "City",
"woeid": 2459115,
"latt_long": "40.71455,-74.007118",
"timezone": "US\/Eastern"
}
雨が降っているかどうかをテストするには、{{localweather.consolidated_weather[0].weather_state_name}}
を参照してください。これをこのオブジェクトで使用すると、Clear
が返されます。もし結果の場所の名前でもパーソナライズしたい場合は、{{localweather.title}}
はNew York
を返します。
次の画像は、設定が正しく行われている場合にダッシュボードで表示される構文のハイライトの種類を示しています。また、例のconnected_content
リクエストを活用する方法も示しています!
1
2
3
4
5
6
7
8
9
10
{% connected_content https://www.metaweather.com/api/location/search/?query={{custom_attribute.${customCity}}} :save locationjson %}
{% connected_content https://www.metaweather.com/api/location/{{locationjson[0].woeid}}/ :save localweather %}
{% if {{localweather.consolidated_weather[0].weather_state_name}} == 'Rain' %}
It's raining! Grab an umbrella!
{% elsif {{localweather.consolidated_weather[0].weather_state_name}} == 'Clouds' %}
No sunscreen needed :)
{% else %}
Enjoy the weather!
{% endif %}
APIが{{localweather.consolidated_weather[0].weather_state_name}}
でRain
を返した場合、ユーザーはこのプッシュを受け取ります。
コネクテッドコンテンツはデフォルトで、作成する GET HTTP リクエストの Content-Type
ヘッダーを、Accept: */*
を持つ application/json
に設定します。別のコンテンツタイプが必要な場合は、タグに:content_type your/content-type
を追加して明示的に指定してください。Brazeは、指定したタイプにContent-TypeおよびAcceptヘッダーの両方を設定します。
1
{% connected_content http://numbersapi.com/random/trivia :content_type application/json %}
HTTP POST
デフォルトでは、コネクテッドコンテンツは指定されたURLにHTTP GETリクエストを送信します。代わりにPOSTリクエストを行うには、:method post
を指定します。
指定された:body
の後にkey1=value1&key2=value2&...
形式のクエリ文字列またはキャプチャされた値への参照を指定することで、オプションでPOSTボディを提供できます。Content-Typeのデフォルトはapplication/x-www-form-urlencoded
です。:content_type application/json
を指定し、key1=value1&key2=value2
のようなフォームURLエンコードされた本文を提供すると、Brazeは送信前に自動的に本文をJSONエンコードします。
また、接続されたコンテンツは、デフォルトではPOST 呼び出しをキャッシュしません。この動作を更新するには、:cache_max_age
をConnected Content POST コールに追加します。
デフォルトのコンテンツタイプ
1
{% connected_content https://example.com/api/endpoint :method post :body key1=value1&key2=value2 %}
Application/JSON Content-Type
1
{% connected_content https://example.com/api/endpoint :method post :body key1=value1&key2=value2 :content_type application/json %}
JSONボディの提供
独自のJSONボディを提供したい場合、スペースがない場合はインラインで記述できます。体にスペースがある場合は、割り当てまたはキャプチャのステートメントを使用する必要があります。つまり、以下の 3 つのいずれも使用できます。
インライン: スペースがない場合
1
{% connected_content https://example.com/api/endpoint :method post :body {"foo":"bar","baz":"{{1|plus:1}}"} :content_type application/json %}
capture ステートメント内の本文: スペースがある場合
1
2
3
4
{% capture postbody %}
{"foo": "bar", "baz": "{{ 1 | plus: 1 }}"}
{% endcapture %}
{% connected_content https://example.com/api/endpoint :method post :body {{postbody}} :content_type application/json %}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{% capture postbody %}
{
"ids":[ca_57832,ca_75869],"include":{"attributes":{"withKey":["daily_deals"]}}
}
{% endcapture %}
{% connected_content
https://example.com/api/endpoint
:method post
:headers {
"Content-Type": "application/json"
}
:body {{postbody}}
:save result
%}
代入文の本文:スペースが許可されている
1
2
{% assign postbody = '{"foo":"bar", "baz": "2"}' %}
{% connected_content https://example.com/api/endpoint :method post :body {{postbody}} :content_type application/json %}
HTTPステータスコード
コネクテッドコンテンツの呼び出しから HTTP ステータスを使用するには、まず HTTP ステータスをローカル変数として保存してから、__http_status_code__
キーを使用します。以下に例を示します。
1
2
3
4
{% connected_content https://example.com/api/endpoint :save result %}
{% if result.__http_status_code__ != 200 %}
{% abort_message('Connected Content returned a non-200 status code') %}
{% endif %}
このキーは、有効な JSON オブジェクトと 2XX
応答をエンドポイントが返す場合にのみ、コネクテッドコンテンツオブジェクトに自動的に追加されます。エンドポイントが配列や他の型を返す場合、そのキーは応答に自動的に設定されることはありません。