Android ゲームのイベント
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
このガイドでは、Google Play Games サービスが提供するイベント API を使用して、ゲーム分析のためにプレーヤーのゲームプレイ データを収集する方法について説明します。API は com.google.android.gms.games.event
パッケージと com.google.android.gms.games
パッケージに含まれています。
始める前に
イベントゲームのコンセプトをまだ確認されていない場合は、確認することをおすすめします。
イベント API を使用してコーディングを開始する前に:
イベント クライアントを取得する
イベント API の使用を開始するには、まずゲームで EventsClient
オブジェクトを取得する必要があります。そのためには、PlayGames.getEventsClient()
メソッドを呼び出してアクティビティを渡します。
イベントを送信する
コードをゲームに追加して、ゲームに関連するイベントが発生したときに Play ゲームサービスに通知することができます。
イベントの更新を送信するには、eventId
値と 0 以上の整数 incrementAmount
を指定して EventsClient.increment()
を呼び出します。
eventId
は、Google Play Console で初めてイベントを定義するときに Play ゲームサービスによって生成され、ゲーム内でこのイベントを一意に識別するために使用されます。
incrementAmount
入力を使用すると、ゲーム固有の目標の達成に向けた、プレーヤーの定量的な進行状況を指定できます。たとえば、ゲームでトラッキングするイベントが「目の飛び出たモンスター 500 体を倒す」である場合、incrementAmount
の値はプレーヤーが 1 回の戦闘で倒したモンスターの数となります。
増分を 1 としてイベントを送信する方法の例を次に示します。
public void submitEvent(String eventId) {
PlayGames.getEventsClient(this)
.increment(eventId, 1);
}
イベントを取得する
EventsClient.load()
を呼び出すことで、ゲーム用に Google のサーバーに保存されているすべてのイベントデータを取得できます。このメソッド呼び出しでは、Play ゲームサービスがユーザーのデバイス上にローカルにキャッシュされたデータを消去するかどうかを示すブール値を渡します。
Google Play Console で定義した特定のイベントのデータを取得するには、EventsClient.loadByIds()
を呼び出して、入力パラメータにイベント ID の配列を渡します。
次のスニペットは、ゲームのすべてのイベントのリストについて Play ゲームサービスにクエリを行う方法を示しています。
public void loadEvents() {
PlayGames.getEventsClient(this)
.load(true)
.addOnCompleteListener(new OnCompleteListener<AnnotatedData<EventBuffer>>() {
@Override
public void onComplete(@NonNull Task<AnnotatedData<EventBuffer>> task) {
if (task.isSuccessful()) {
// Process all the events.
for (Event event : task.getResult().get()) {
Log.d(TAG, "loaded event " + event.getName());
}
} else {
// Handle Error
Exception exception = task.getException();
int statusCode = CommonStatusCodes.DEVELOPER_ERROR;
if (exception instanceof ApiException) {
ApiException apiException = (ApiException) exception;
statusCode = apiException.getStatusCode();
}
showError(statusCode);
}
}
});
}
このページのコンテンツやコードサンプルは、コンテンツ ライセンスに記載のライセンスに従います。Java および OpenJDK は Oracle および関連会社の商標または登録商標です。
最終更新日 2025-07-26 UTC。
[null,null,["最終更新日 2025-07-26 UTC。"],[],[],null,["# Events for Android games\n\n| **Note:** This guide is for the Play Games Services v2 SDK. For information on the previous version of this SDK, see the [Play Games Services v1\n| documentation](/games/pgs/v1/android/events).\n\nThis guide shows you how to collect player gameplay data for game analytics\nusing the events APIs provided by Google Play Games Services. The APIs can be found in the\n[`com.google.android.gms.games.event`](https://developers.google.com/android/reference/com/google/android/gms/games/event/package-summary)\nand [`com.google.android.gms.games`](https://developers.google.com/android/reference/com/google/android/gms/games/package-summary)\npackages.\n\nBefore you begin\n----------------\n\nIf you haven't already done so, you might find it helpful to review the [events\ngame concepts](/games/pgs/events).\n\nBefore you start to code using the events APIs:\n\n- Define the events for your game in the\n [Google Play Console](https://play.google.com/apps/publish/).\n\n- Follow the [sign-in checklist recommendations](/games/pgs/quality#sign-in).\n\nGet the events client\n---------------------\n\nTo start using the events APIs, your game must first obtain an\n[`EventsClient`](https://developers.google.com/android/reference/com/google/android/gms/games/EventsClient)\nobject. You can do this by calling the [`PlayGames.getEventsClient()`](https://developers.google.com/android/reference/com/google/android/gms/games/PlayGames#public-static-eventsclient-geteventsclient-activity-activity)\nmethod and passing in the activity.\n| **Note:** The [`EventsClient`](https://developers.google.com/android/reference/com/google/android/gms/games/EventsClient) class makes use of the Google Play services [`Task`](https://developers.google.com/android/reference/com/google/android/gms/tasks/Task) class to return results asynchronously. To learn more about using tasks to manage threaded work, see the [Tasks API developer\n| guide](https://developers.google.com/android/guides/tasks).\n\nSubmit events\n-------------\n\nYou can add code in your game to notify Play Games Services whenever an event of\ninterest to your game occurs.\n\nTo send an event update, call\n[`EventsClient.increment()`](https://developers.google.com/android/reference/com/google/android/gms/games/EventsClient#increment)\nwith the `eventId` value and an integer `incrementAmount` that is equal to or\ngreater than 0.\n\n- The `eventId` is generated by Play Games Services when you first define the\n event in the Google Play Console and is used to uniquely identify this\n event in your game.\n\n- You can use the `incrementAmount` input to specify the player's quantitative\n progress towards completing some game-specific goal. For example, if the\n event your game wants to track is *'Defeat 500 bug-eyed monsters'* , the\n `incrementAmount` value can be the number of monsters that the player killed\n in a single battle.\n\nHere's an example of how to submit an event with an increment amount of 1: \n\n```text\npublic void submitEvent(String eventId) {\n PlayGames.getEventsClient(this)\n .increment(eventId, 1);\n}\n```\n\nRetrieve events\n---------------\n\nYou can retrieve all events data stored in Google's servers for your game, by\ncalling\n[`EventsClient.load()`](https://developers.google.com/android/reference/com/google/android/gms/games/EventsClient#load(boolean)).\nIn the method call, pass in a boolean value to indicate if Play Games Services\nshould clear the locally cached data on the user's device.\n\nTo retrieve data for specific events that you defined in the\nGoogle Play Console, call\n[`EventsClient.loadByIds()`](https://developers.google.com/android/reference/com/google/android/gms/games/EventsClient#loadByIds)\nand pass in an array of event IDs in the input parameters.\n\nThe following snippet shows how you can query Play Games Services for the list of\nall events for your game: \n\n```gdscript\npublic void loadEvents() {\n PlayGames.getEventsClient(this)\n .load(true)\n .addOnCompleteListener(new OnCompleteListener\u003cAnnotatedData\u003cEventBuffer\u003e\u003e() {\n @Override\n public void onComplete(@NonNull Task\u003cAnnotatedData\u003cEventBuffer\u003e\u003e task) {\n if (task.isSuccessful()) {\n // Process all the events.\n for (Event event : task.getResult().get()) {\n Log.d(TAG, \"loaded event \" + event.getName());\n }\n } else {\n // Handle Error\n Exception exception = task.getException();\n int statusCode = CommonStatusCodes.DEVELOPER_ERROR;\n if (exception instanceof ApiException) {\n ApiException apiException = (ApiException) exception;\n statusCode = apiException.getStatusCode();\n }\n showError(statusCode);\n }\n }\n });\n}\n```"]]