Skip to content

コネクテッドコンテンツのローカル変数

このページでは、ローカルのコネクテッドコンテンツ変数の概要と、それらの使用方法と保存方法について説明します。

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を返した場合、ユーザーはこのプッシュを受け取ります。

!プッシュ通知で「雨が降っている!Grab an umbrella!”

By default, Connected Content will set a Content-Type header on a GET HTTP request that it makes to application/json with Accept: */*. If you require another content type, specify it explicitly by adding :content_type your/content-type to the tag. Braze will then set both the Content-Type and Accept header to the type you specify.

1
{% connected_content http://numbersapi.com/random/trivia :content_type application/json %}

HTTP POST

By default, Connected Content makes an HTTP GET request to the specified URL. To make a POST request instead, specify :method post.

You can optionally provide a POST body by specifying :body followed by either a query string of the format key1=value1&key2=value2&... or a reference to captured values. Content-Type defaults to application/x-www-form-urlencoded. If you specify :content_type application/json and provide a form-urlencoded body such as key1=value1&key2=value2, Braze will automatically JSON-encode the body before sending.

Connected Content also does not cache POST calls by default. You can update this behavior by adding :cache_max_age to the Connected Content POST call.

1
{% connected_content https://example.com/api/endpoint :method post :body key1=value1&key2=value2 %}
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 %}
New Stuff!