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 요청을 해야 합니다.

GET 요청은 일반적으로 기본적으로 캐시되지만, 몇 가지 예외가 있습니다(높은 카디널리티의 사용자 속성을 포함하는 URL, :no_cache, 또는 1MB보다 큰 응답 본문 등). 동일한 GET 요청이 둘 이상의 필드에 나타나면 Braze는 엔드포인트를 다시 호출하는 대신 캐시된 응답을 재사용합니다. 캐시 동작에 대한 자세한 내용은 응답 캐싱을 참조하세요.

HTTP POST를 통한 연결된 콘텐츠 호출은 기본적으로 캐시되지 않습니다. 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을 반환하면 사용자는 다음 푸시 알림을 받게 됩니다.

메시지가 "It's raining! Grab an umbrella!"인 푸시 알림

기본적으로 연결된 콘텐츠는 GET HTTP 요청에 Content-Type 헤더를 application/json으로, Accept: */*로 설정합니다. 다른 콘텐츠-유형이 필요한 경우 태그에 :content_type your/content-type을 추가하여 명시적으로 지정하세요. 그러면 Braze는 Content-Type 헤더와 Accept 헤더를 모두 지정한 유형으로 설정합니다.

1
{% connected_content https://api.sunrise-sunset.org/v2?lat=40.7128&lng=-74.0060&date=today :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와 같이 form-urlencoded 본문을 제공하면, Braze는 전송 전에 자동으로 본문을 JSON 인코딩합니다.

연결된 콘텐츠는 기본적으로 POST 호출도 캐시하지 않습니다. 이 동작은 연결된 콘텐츠 POST 호출에 :cache_max_age를 추가하여 업데이트할 수 있습니다.

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 본문을 제공하려면 공백이 없는 경우 인라인으로 작성할 수 있습니다. 본문에 공백이 있는 경우 assign 또는 capture 문을 사용해야 합니다. 즉, 다음 세 가지 모두 허용됩니다:

인라인: 공백 불허
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
%}
assign 문의 본문: 공백 허용
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_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!