> ## Documentation Index
> Fetch the complete documentation index at: https://devdocs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 統計資料快速入門

> PlayFab 統計資料快速入門，說明如何使用 SDK 定義統計資料、寫入玩家值和讀取結果，以啟動進度追蹤。

# 統計資料快速入門

在本指南中，我們將了解如何為統計資料服務設定開發環境。我們也將學習如何從我們的網站 [Game Manager](https://developer.playfab.com) 建立快速排行榜。

## 先決條件

我們需要 PlayFab 帳戶才能使用 PlayFab 統計資料服務。如需建立帳戶的指示，請參閱[驗證](/zh-TW/services/playfab/identity/dev-identity/authentication/aad-authentication)。

## 建立統計資料

我們將選取左側功能表上的 Progression。

<img src="https://mintcdn.com/microsoft-4404708b/N3T1ucKV7zIMBudj/images/playfab/player-progression/statistics/game-manager-main-menu.png?fit=max&auto=format&n=N3T1ucKV7zIMBudj&q=85&s=f8afb627f579da6ba1f9677d3296a3ee" alt="PlayFab 統計資料主功能表" width="1280" height="900" data-path="images/playfab/player-progression/statistics/game-manager-main-menu.png" />

我們現在從上方功能表選取 Statistics 索引標籤。

<img src="https://mintcdn.com/microsoft-4404708b/N3T1ucKV7zIMBudj/images/playfab/player-progression/statistics/statistic-menu.png?fit=max&auto=format&n=N3T1ucKV7zIMBudj&q=85&s=78393c583a8ff1afaf920b9bde60eaa7" alt="PlayFab 統計資料新增統計資料" width="1280" height="900" data-path="images/playfab/player-progression/statistics/statistic-menu.png" />

之後，我們將前往 "New Statistic" 按鈕並建立統計資料定義。

<img src="https://mintcdn.com/microsoft-4404708b/N3T1ucKV7zIMBudj/images/playfab/player-progression/statistics/new-statistic.png?fit=max&auto=format&n=N3T1ucKV7zIMBudj&q=85&s=16b828e676d1bede541bcb9331dbe133" alt="PlayFab 統計資料使用方式" width="1239" height="888" data-path="images/playfab/player-progression/statistics/new-statistic.png" />

在這裡，我們可以設定統計資料的各個層面。請在此深入了解可用的參數以及如何建立統計資料：

* [建立基本排行榜](/zh-TW/services/playfab/player-progression/statistics/create-basic-statistics)。
* [API 參考](/zh-TW/services/playfab/player-progression/statistics/api-reference)

最終結果應如下所示。

<img src="https://mintcdn.com/microsoft-4404708b/N3T1ucKV7zIMBudj/images/playfab/player-progression/statistics/stat-result.png?fit=max&auto=format&n=N3T1ucKV7zIMBudj&q=85&s=1c145637328915af6a2a81ef56ecccd8" alt="PlayFab 統計資料最終結果" width="1872" height="822" data-path="images/playfab/player-progression/statistics/stat-result.png" />

## 設定環境

在這裡，我們將學習如何設定開發人員環境以使用 C# SDK，但這裡介紹的概念也適用於其他 SDK 或純 HTTP 要求。

## 設定 TitleId 和 DeveloperSecretKey

以遊戲實體身分進行驗證時需要 DeveloperSecretKey。

```C# theme={null}
PlayFabSettings.staticSettings.TitleId = ""; // Change this value to your own titleId from PlayFab Game Manager
PlayFabSettings.staticSettings.DeveloperSecretKey = ""; // Change this to your title's secret key from Game Manager
```

## 建立遊戲並取得秘密金鑰

* 登入 [https://developer.playfab.com/](https://developer.playfab.com/)
* 建立遊戲
  * 在設定下的 API Features 區段中找到遊戲識別碼。
* 產生秘密金鑰：
  * 遊戲檢視左上角遊戲名稱旁的齒輪
  * Title Settings
  * Secret Keys
  * New secret key

以下是在 Game Manager 中找到 Secret Key 區段時 UI 的樣子。

<img src="https://mintcdn.com/microsoft-4404708b/N3T1ucKV7zIMBudj/images/playfab/player-progression/statistics/secret-keys.png?fit=max&auto=format&n=N3T1ucKV7zIMBudj&q=85&s=864e20d34e00d5eb96b9a392c7cc8e0b" alt="PlayFab 統計資料金鑰" width="2541" height="1152" data-path="images/playfab/player-progression/statistics/secret-keys.png" />

## 以遊戲身分登入

對於寫入作業，我們建議從您的遊戲伺服器使用 TitleEntity，此方法會傳回我們所有要求都會使用的 `AuthenticationContext`。

```C# theme={null}
public static async Task<PlayFabAuthenticationContext> LoginAsTitleEntity()
{
    GetEntityTokenRequest request = new GetEntityTokenRequest()
    {
        Entity = new PlayFab.AuthenticationModels.EntityKey()
        {
            Id = PlayFabSettings.staticSettings.TitleId,
            Type = "title",
        },                
    };

    PlayFabResult<GetEntityTokenResponse> entityTokenResult = await PlayFabAuthenticationAPI.GetEntityTokenAsync(request);

    PlayFabAuthenticationContext authContext = new PlayFabAuthenticationContext
    {
        EntityToken = entityTokenResult.Result.EntityToken
    };
    
    return authContext;
}
```

## 以玩家身分登入 (建立玩家)

以玩家身分登入，直接對 PlayFab API 進行讀取作業。此方法會傳回我們所有要求都會使用的 `AuthenticationContext`。

```C# theme={null}
private static async Task<PlayFabAuthenticationContext> LoginAsPlayer(string customId = "GettingStartedGuide")
{
    LoginWithCustomIDRequest request = new LoginWithCustomIDRequest { CustomId = customId, CreateAccount = true };

    PlayFabResult<LoginResult> loginResult = await PlayFabClientAPI.LoginWithCustomIDAsync(request);

    return loginResult.Result.AuthenticationContext;
}

```

## 設定實體的顯示名稱屬性

為了讓 `DisplayName` 屬性可作為 Get Statistics API 回應的一部分，我們需要為我們建立的每個實體執行下列程式碼。此程序會將實體對應至遊戲的自訂顯示名稱。

```C# theme={null}
private static async Task UpdateEntityDisplayName(PlayFabAuthenticationContext context, string customId)
{
    SetDisplayNameRequest request = new SetDisplayNameRequest()
    {
        AuthenticationContext = context,
        DisplayName = customId,
        Entity = new PlayFab.ProfilesModels.EntityKey()
        {
            Id = context.EntityId,
            Type = context.EntityType,
        },
    };

    PlayFabResult<SetDisplayNameResponse> updateNameResult = await PlayFabProfilesAPI.SetDisplayNameAsync(request);

}
```

## 另請參閱

* [建立基本統計資料](/zh-TW/services/playfab/player-progression/statistics/create-basic-statistics)。
* [統計資料的進階運用](/zh-TW/services/playfab/player-progression/statistics/doing-more-statistics)。
* [季節性統計資料](/zh-TW/services/playfab/player-progression/statistics/seasonal-statistics)。
* [將內容資料新增至統計資料](/zh-TW/services/playfab/player-progression/statistics/metadata-statistics)。
* [交易式寫入](/zh-TW/services/playfab/player-progression/statistics/transactional-writes)
* [API 參考](/zh-TW/services/playfab/player-progression/statistics/api-reference)。
* [限制](/zh-TW/services/playfab/player-progression/statistics/limits-statistics)。
* [配額](/zh-TW/services/playfab/player-progression/statistics/quota-statistics)
* [統計資料計量](/zh-TW/services/playfab/pricing/meters/statistics-meters)。


## Related topics

- [PlayFab 統計資料概觀](/zh-TW/services/playfab/player-progression/statistics/index.md)
- [PlayFab 玩家進度文件](/zh-TW/services/playfab/player-progression/index.md)
- [建立基本統計資料](/zh-TW/services/playfab/player-progression/statistics/create-basic-statistics.md)
- [遊戲資料快速入門](/zh-TW/services/playfab/live-service-management/game-configuration/titledata/quickstart.md)
- [玩家資料快速入門](/zh-TW/services/playfab/player-progression/player-data/quickstart.md)
