> ## 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 コミュニティ統計を作成し、グローバル目標、イベント、共有マイルストーンを実現します。

コミュニティ統計は、エンゲージメントを高め、プレイヤーがゲームに深く関わるよう促す強力なツールです。ゲームタイトルレベルで統計を集計することで、開発者はプレイヤー間のコラボレーションを促す共通の目標や課題を作成できます。このアプローチは、個々のプレイヤーの行動をコミュニティ全体の目標への貢献に変え、プレイヤーが共に集団的なマイルストーンを達成しようとすることで、持続的なエンゲージメントを促進します。プレイヤーは自分の個々の貢献がより大きなコミュニティにとって重要だと感じ、ゲームや他のプレイヤーとのつながりが強まります。

たとえば、全プレイヤーが 1 週間以内に合計 1,000 万エリミネーションを達成すると特別なゲーム内イベントがアンロックされる、グローバルなボスバトルを実装することを想像してください。プレイヤーは自分の貢献ごとにコミュニティのプログレスバーが進むのを見ることで、目標達成を助ける動機を得ます。この動きは、特にリアルタイムの進捗更新やマイルストーン報酬と組み合わさると、投資と参加の強力なフィードバックループを生み出します。

PlayFab では、次の 2 つの統計を定義することでコミュニティ統計を簡単に管理できます: 1 つはプレイヤーレベルの統計 (集計元)、もう 1 つはタイトルまたはグループレベルの統計 (集計先) です。プレイヤー統計への更新は、自動的に集計先の統計に反映されます。現在のコミュニティスコアを取得するには、集計先エンティティの統計を読み取ります。

PlayFab は 2 種類のコミュニティ統計をサポートします:

* **Title-level**: すべてのプレイヤーの更新をタイトル全体の 1 つの統計に集計します。プレイヤーベース全体で共有するグローバル目標に最適です。
* **Group-level**: プレイヤーの更新をグループエンティティ統計に集計します。共有目標に向けてプレイヤーの一部が取り組む、クランチャレンジ、ギルド目標、チームベースの目標に最適です。

## タイトルレベルのコミュニティ統計

### 集計元となる統計定義の作成

まず、集計元として機能するプレイヤー用の統計定義を作成します:

```C# theme={null}
// Use the PlayFabAuthenticationContext to call the API
PlayFabProgressionInstanceAPI statsAPI = new PlayFabProgressionInstanceAPI(context);
CreateStatisticDefinitionRequest statDefinitionRequest = new CreateStatisticDefinitionRequest()
{
    Name = "sourceStat",
    AuthenticationContext = context,
    EntityType = "title_player_account",
    VersionConfiguration = new VersionConfiguration()
    {
        MaxQueryableVersions = 1,
        ResetInterval = ResetInterval.Manual,
    },
    Columns = new List<StatisticColumn>()
    {
        new StatisticColumn()
        {
            Name = "Eliminations",
            AggregationMethod = StatisticAggregationMethod.Sum,
        },
    },
};

PlayFabResult<PlayFab.LeaderboardsModels.EmptyResponse> createStatDefResult = await statsAPI.CreateStatisticDefinitionAsync(statDefinitionRequest);
```

### 集計先となる統計定義の作成

次に、集計先として機能するタイトル用の統計定義を作成します。タイトル統計定義の集計元として、プレイヤー統計定義を指定します:

```C# theme={null}
// Use the PlayFabAuthenticationContext to call the API
PlayFabProgressionInstanceAPI statsAPI = new PlayFabProgressionInstanceAPI(context);
CreateStatisticDefinitionRequest statDefinitionRequest = new CreateStatisticDefinitionRequest()
{
    Name = "communityStat",
    AuthenticationContext = context,
    EntityType = "title",
    Columns = new List<StatisticColumn>()
    {
        new StatisticColumn()
        {
            Name = "Eliminations",
            AggregationMethod = StatisticAggregationMethod.Sum,
        },
    },
    AggregationSources = new string[] { "sourceStat" };
};

PlayFabResult<PlayFab.LeaderboardsModels.EmptyResponse> createStatDefResult = await statsAPI.CreateStatisticDefinitionAsync(statDefinitionRequest);
```

<Note>
  ***NOTE:*** 次のように設定してください
</Note>

`EntityType` を `title` に設定し、`AggregationSources` にプレイヤーレベルで定義した統計を参照させます。

コミュニティ統計を作成すると、プレイヤーレベルで行われた更新はタイトルレベルで集計され始めます。集計されるのは新しい更新のみです。集計元の統計がしばらく前から存在していても、システムはそれらの過去の値をタイトルレベルにバックフィルしません。

### タイトルコミュニティ統計の読み取り

タイトルレベルの統計の現在のスコアを取得するには、タイトルエンティティの統計を読み取ります:

```C# theme={null}
// Use the PlayFabAuthenticationContext to call the API
PlayFabProgressionInstanceAPI statsAPI = new PlayFabProgressionInstanceAPI(context);

GetStatisticsRequest request = new GetStatisticsRequest()
{
    AuthenticationContext = context,
    Entity = new PlayFab.LeaderboardsModels.EntityKey()
    {
        Id = "<title id in hex>",
        Type = "title"
    },
};

PlayFabResult<GetStatisticsResponse> result = await statsAPI.GetStatisticsAsync(request); 
```

## グループレベルのコミュニティ統計

グループレベルのコミュニティ統計を使うと、クランやギルドといった特定のグループ内でプレイヤーの行動を集計できます。この集計は、メンバーが共通の目標に向けて協力するクランチャレンジのようなシナリオで役立ちます。

### 集計先となる統計定義の作成

`EntityType` を `group` に設定した統計定義を作成します。集計元にはプレイヤー統計定義を指定します:

```C# theme={null}
// Use the PlayFabAuthenticationContext to call the API
PlayFabProgressionInstanceAPI statsAPI = new PlayFabProgressionInstanceAPI(context);
CreateStatisticDefinitionRequest statDefinitionRequest = new CreateStatisticDefinitionRequest()
{
    Name = "groupCommunityStat",
    AuthenticationContext = context,
    EntityType = "group",
    Columns = new List<StatisticColumn>()
    {
        new StatisticColumn()
        {
            Name = "Eliminations",
            AggregationMethod = StatisticAggregationMethod.Sum,
        },
    },
    AggregationSources = new string[] { "sourceStat" }
};

PlayFabResult<PlayFab.LeaderboardsModels.EmptyResponse> createStatDefResult = await statsAPI.CreateStatisticDefinitionAsync(statDefinitionRequest);
```

<Note>
  集計元の統計定義 (プレイヤーレベル) は、タイトルレベルのコミュニティ統計で使用したものと同じです。既存の集計元統計定義を再利用できます。
</Note>

### 集計元統計の更新

グループレベルの集計のためにプレイヤーの集計元統計を更新するときは、`StatisticUpdate` オブジェクトの `AggregationTargetEntityKeys` プロパティに対象のグループエンティティを指定します。このプロパティは、更新をどのグループエンティティに集計するかを PlayFab に伝えます。

```C# theme={null}
// Use the PlayFabAuthenticationContext to call the API
PlayFabProgressionInstanceAPI statsAPI = new PlayFabProgressionInstanceAPI(context);

UpdateStatisticsRequest request = new UpdateStatisticsRequest()
{
    AuthenticationContext = context,
    Entity = new PlayFab.LeaderboardsModels.EntityKey()
    {
        Id = "<player entity id>",
        Type = "title_player_account"
    },
    Statistics = new List<StatisticUpdate>()
    {
        new StatisticUpdate()
        {
            Name = "sourceStat",
            Scores = new List<string> { "5" },
            AggregationTargetEntityKeys = new List<PlayFab.LeaderboardsModels.EntityKey>()
            {
                new PlayFab.LeaderboardsModels.EntityKey()
                {
                    Id = "<group entity id>",
                    Type = "group"
                }
            }
        }
    }
};

PlayFabResult<UpdateStatisticsResponse> result = await statsAPI.UpdateStatisticsAsync(request);
```

<Info>
  タイトルレベルのコミュニティ統計では、タイトルが既定の集計対象であるため `AggregationTargetEntityKeys` は必須ではありません。グループレベルのコミュニティ統計では、グループエンティティキーを指定する必要があります。現時点で、`AggregationTargetEntityKeys` に指定できるエンティティキーは 1 つだけです。
</Info>

### グループコミュニティ統計の読み取り

グループコミュニティ統計の現在のスコアを取得するには、グループエンティティの統計を読み取ります:

```C# theme={null}
// Use the PlayFabAuthenticationContext to call the API
PlayFabProgressionInstanceAPI statsAPI = new PlayFabProgressionInstanceAPI(context);

GetStatisticsRequest request = new GetStatisticsRequest()
{
    AuthenticationContext = context,
    Entity = new PlayFab.LeaderboardsModels.EntityKey()
    {
        Id = "<group entity id>",
        Type = "group"
    },
};

PlayFabResult<GetStatisticsResponse> result = await statsAPI.GetStatisticsAsync(request);
```

## 制限事項

* コミュニティ統計に指定できる集計元は 1 つだけです。
* コミュニティ統計にはバージョン構成を指定できません。集計元統計に指定したバージョン構成がコミュニティ統計にも適用されます。
* コミュニティ統計の `EntityType` は `title` または `group` である必要があります。
* いずれの列にも `Last` 集計を使用できません。
* 集計元統計を更新する際に `AggregationTargetEntityKeys` に指定できるエンティティキーは 1 つだけです。

## 関連項目

* [基本的な統計の作成](/services/playfab/player-progression/statistics/create-basic-statistics)
* [統計でさらに多くを行う](/services/playfab/player-progression/statistics/doing-more-statistics)
* [シーズン統計](/services/playfab/player-progression/statistics/seasonal-statistics)
* [統計にコンテキストデータを追加する](/services/playfab/player-progression/statistics/metadata-statistics)
* [API リファレンス](/services/playfab/player-progression/statistics/api-reference)
* [制限](/services/playfab/player-progression/statistics/limits-statistics)
* [クォータ](/services/playfab/player-progression/statistics/quota-statistics)
* [統計メーター](/services/playfab/pricing/meters/statistics-meters)


## Related topics

- [PlayFab プレイヤー進行ドキュメント](/ja-jp/services/playfab/player-progression/index.md)
- [コミュニティ概要ページ](/ja-jp/services/playfab/community/what-is-community.md)
- [Godot & コミュニティエンジン 概要](/ja-jp/paths/community-engines/overview.md)
- [PlayFab とは?](/ja-jp/services/playfab/get-started/what-is-playfab.md)
- [タイトルからのレピュテーション フィードバックの送信](/ja-jp/services/xbox-services/community/reputation/concepts/live-sending-reputation-feedback.md)
