

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 範例 1：資料擴充
<a name="monetization-functions-examples-enrichment"></a>

## 案例
<a name="monetization-functions-examples-enrichment-scenario"></a>

串流服務想要在工作階段開始時從外部服務 （在此案例中為 LiveRamp) 擷取受眾身分資料，並將其存放在播放器參數中，以便稍後在工作階段中以廣告為目標。身分信封是由身分解析服務提供的加密識別符，可讓廣告系統跨裝置和平台識別檢視器，而不會公開個人資訊。

## Configuration
<a name="monetization-functions-examples-enrichment-config"></a>

**擷取 LiveRamp 信封 (HTTP\_REQUEST)：**

```
{
    "FunctionId": "fetchLiveRamp",
    "FunctionType": "HTTP_REQUEST",
    "HttpRequestConfiguration": {
        "Runtime": "JSONATA",
        "MethodType": "GET",
        "Url": "{%'https://api.example-identity.com/v2/envelope?pid=12345&iv=' & player_params.hashedId%}",
        "Headers": {
            "Authorization": "{%'Bearer ramp-token-789'%}"
        },
        "RequestTimeoutMilliseconds": 2000,
        "Output": {
            "player_params.envelope_id": "{%response.body.envelopes[0].value%}"
        }
    }
}
```

**注意**  
`Bearer ramp-token-789` 使用您自己的 API 登入資料取代 。請勿將敏感字符直接存放在生產中的函數組態中。考慮使用字符輪換策略。

`RequestTimeoutMilliseconds` 根據外部服務的預期回應時間來設定 。較低的值可減少延遲，但會增加逾時錯誤的機會。

## 函數映射
<a name="monetization-functions-examples-enrichment-mapping"></a>

```
{
    "FunctionMapping": {
        "PRE_SESSION_INITIALIZATION": "fetchLiveRamp"
    }
}
```

## 函數執行時會發生什麼情況
<a name="monetization-functions-examples-enrichment-runtime"></a>

1. 檢視器會啟動播放工作階段。

1. MediaTailor 會執行`PRE_SESSION_INITIALIZATION`生命週期掛鉤並執行 `fetchLiveRamp`。

1. 函數會使用 建置請求 URL，`player_params.hashedId`並呼叫 LiveRamp API。在 URL 中， `pid` 是合作夥伴 ID，而 `iv`是雜湊檢視器識別符。

1. 輸出表達式會從回應陣列 (`response.body.envelopes[0].value`) 中的第一個信封擷取值，並將其寫入 `player_params.envelope_id`。

**提示**  
若要明確處理錯誤，請在存取回應資料`response.statusCode`之前檢查： `{%response.statusCode = 200 ? response.body.envelopes[0].value : ''%}`

如需詳細資訊，請參閱 [HTTP\_REQUEST](monetization-functions-types-http-request.md)、[PRE\_SESSION\_INITIALIZATION](monetization-functions-hooks-pre-session.md)、[故障診斷和監控](monetization-functions-troubleshooting.md) 和 [限制](monetization-functions-limits.md)。