> ## 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.

# First glance for event-based Stats

> First-look walkthrough for configuring an XBOX Live event and stat in Partner Center, then adding code to send events and update stats.

For titles that are configured on Partner Center by using event-based Stats, this topic shows you how to configure an event and a stat on Partner Center, and then add code to your title to send events and update stats.

## Sign in to Partner Center

The following examples use Partner Center to configure XBOX game experiences.

After your company has an agreement to create products for XBOX, configure your content in Partner Center. If your company isn't yet set up with Partner Center access and you're developing a game, contact your Microsoft account manager. If you're developing an app, contact XBOX App Partner Requests ([xboxapps@microsoft.com](mailto:xboxapps@microsoft.com)). If you're working with the ID\@XBOX program, contact ID Setup ([idsetup@microsoft.com](mailto:idsetup@microsoft.com)).

The ID\@XBOX program enables qualified game developers of all sizes to unleash their creativity by self-publishing digital games on XBOX One (or later). This gives studios the tools and support that they need to help maximize their success. To learn more, go to [https://www.xbox.com/Developers](https://www.xbox.com/Developers).

After you have access to Partner Center, you'll need Microsoft to create a product group for your game. Your Microsoft account manager can assist with this process.

After Microsoft has created a product group for your game, use Partner Center to configure XBOX services for your game. The XBOX services configuration for your game is also referred to as its *service configuration*.

Use the following steps to go to the location in Partner Center where you can add the events, stats, achievements, and leaderboards for your game.

1. Sign in to your account on [Partner Center](https://developer.microsoft.com/dashboard/windows/overview).

2. Select the product group that contains or will contain your game.

3. On the **Product Group** page, either select your game or select **Add New Product** to create a new entry.

4. If you're creating a new product, enter a name for your product.

5. For resource access, select **Exclusive Resource Access (ERA)**.

6. Choose whether you need to produce physical media for the game or if it will only be a digital download.

7. Select **Create Product**.

8. At the bottom of the left pane, expand the sandbox that your product was created in.

   Service configurations are defined at the sandbox level. If you export your product to another sandbox, you also need to export your service configuration.

9. On the sandbox submenu, select **Service Configuration**. Use this page to configure XBOX services for your title in the current sandbox.

## Identify and define the events for your scenarios

In Partner Center, before we can define the rules that implement the player experiences we want to achieve, we need to identify and define the events that will drive those experiences.

In the event-based model, achievements and leaderboards are based on player stats. As a result, we can look at the stats that we'll define to see what events our title will need to send.

To power the experiences that we'll define in this example, we'll use an event that we send at the end of each lap in a race.

Let's call this event `LapCompleted`. We'll use this event to drive our customized experiences. To power those experiences, we'll also define the following fields as part of our `LapCompleted` event.

* **LapTime**: A field that indicates the time in minutes to complete a lap.

* **CarModel**: A string field that indicates the model of the car that's used to complete the lap.

* **WeatherCondition**: A string field that identifies the weather condition of the track when the lap was completed.

* **TrackId**: The ID of the track that the lap was completed on.

We'll now define this event in Partner Center.

1. In Partner Center, on the left pane, select **Test Sandbox** > **Service Configuration** > **Events & Stat Rules**.

2. Select **New** to create a new event. The **Add New Event** dialog appears, shown as follows.

   <img src="https://mintcdn.com/microsoft-4404708b/gzrdvT5kpqAXKEQt/images/gdk/services/AddNewEvent-XDP.png?fit=max&auto=format&n=gzrdvT5kpqAXKEQt&q=85&s=d620d2ea30f73bfb53fcebfe7356f526" alt="Screenshot of the Add New Event dialog in Partner Center." width="350" height="560" data-path="images/gdk/services/AddNewEvent-XDP.png" />

3. Select **Base** in **Base Event**.

4. Enter *LapCompleted* in **Event Name**.

5. Optionally, enter a description for the event in **Description**.

6. Select **Add Event Field**.

7. Enter *LapTime* in **Field Name**, and then specify the **Type** as **Double**, shown as follows.

   <img src="https://mintcdn.com/microsoft-4404708b/gzrdvT5kpqAXKEQt/images/gdk/services/LapTimeField-XDP.png?fit=max&auto=format&n=gzrdvT5kpqAXKEQt&q=85&s=1ed0a8c599be8bd3551ce963df1945dd" alt="Screenshot of the Add New Event Field dialog in Partner Center." width="352" height="439" data-path="images/gdk/services/LapTimeField-XDP.png" />

8. Select **Add Event Field**.

9. Repeat the process to add the following event fields.

   * *CarModel*: **UnicodeString**

   * *WeatherCondition*: **UnicodeString**

   * *TrackId*: **Int32**

   Your event should look something like the following screenshot.

   <img src="https://mintcdn.com/microsoft-4404708b/gzrdvT5kpqAXKEQt/images/gdk/services/LapCompletedEvent-XDP.png?fit=max&auto=format&n=gzrdvT5kpqAXKEQt&q=85&s=c30cfa0f508b2a7126abce03dccfda91" alt="Screenshot of the newly defined LapCompleted event in Partner Center." width="347" height="644" data-path="images/gdk/services/LapCompletedEvent-XDP.png" />

10. Select **Save Event** to save the event in your sandbox.

## Design stat rules

Now that we've defined the player experiences, we can start creating the stat rules that implement those experiences. Because we're using `LapTime` for all three scenarios (featured stats, achievements, and leaderboards), we'll configure it first.

This stat will show up as a featured stat (also known as a hero stat) and will also be used for a global leaderboard with additional metadata.

The game fires in-game events when certain actions happen in the game and are considered to be the raw data for creating stats. At least one stat should be created or updated by every event.
(For more information about generating multiple stats in a single event, see [Event-based Stats overview](/services/xbox-services/player-data/stats-leaderboards/event-based/live-stats-eb-overview).)

1. On the **Events & Stat Rules** page, select the **LapCompleted** event from the list of events.

2. Select **New** (to the right of **Stat Rules**) to create a new stat rule. It will be based on the selected event. The **Add Stat Rule** dialog appears, shown as follows.

   <img src="https://mintcdn.com/microsoft-4404708b/gzrdvT5kpqAXKEQt/images/gdk/services/AddNewStatRule-XDP.png?fit=max&auto=format&n=gzrdvT5kpqAXKEQt&q=85&s=1f3c0e2cdfb3a9df22550c6a07a80d44" alt="Screenshot of the Add Stat Rule dialog in Partner Center." width="473" height="474" data-path="images/gdk/services/AddNewStatRule-XDP.png" />

3. Enter *FastestLap* in **Stat Rule Name**.

4. Because we want other apps and titles to access stats that are created by this rule, select the **Open read access to any title** checkbox.

   Stat rules have logic that defines how the event creates or updates the stat. Because we want to track the fastest lap, we only want to update the stat value if the new lap time is lower than the existing fastest lap time.

5. To do this, select **Min** in **Operator** and **LapTime** in **Parameter**, shown as follows.

   <img src="https://mintcdn.com/microsoft-4404708b/gzrdvT5kpqAXKEQt/images/gdk/services/MinLapTime-XDP.png?fit=max&auto=format&n=gzrdvT5kpqAXKEQt&q=85&s=93ff9ebb88b0843595386481e4ae455c" alt="Screenshot of the Operator and Parameter selections for a stat rule in Partner Center." width="465" height="470" data-path="images/gdk/services/MinLapTime-XDP.png" />

Finally, we need to ensure that the event contains all the extra data (known as *stat context* or *metadata*) that we need for this specific stat: the player's car, track, and weather conditions.

6. To add the field to the metadata, select **CarModel** and then select **Add Event Field**.

7. Repeat this for the **WeatherCondition** and **TrackId** fields, shown as follows.

   <img src="https://mintcdn.com/microsoft-4404708b/gzrdvT5kpqAXKEQt/images/gdk/services/FastestLapStatRule-XDP.png?fit=max&auto=format&n=gzrdvT5kpqAXKEQt&q=85&s=9d517a1980e04cff73ff42bbc058a5b9" alt="Screenshot of the FastestLap stat rule with metadata in Partner Center." width="462" height="537" data-path="images/gdk/services/FastestLapStatRule-XDP.png" />

8. Select **Add Stat Rule** to create the stat rule.

## Configure featured stats, leaderboards, and achievements

### Featured stats

Now that the stat rule is complete, we want the stat to appear on the GameHub Achievement tab in GameHub. As a result, we must configure a featured stat (known as a hero stat in Partner Center).

1. In Partner Center, on the left pane, select **Test Sandbox** > **Service Configuration** > **Hero Stats**.

2. Select **New** to add a new featured/hero stat.

3. Enter *Fastest Lap* in **Hero Stat Display Name**.

4. Select **ShortTimespan** in **Display Format**. Select **Seconds** in **Display Units**.

5. Select **Best** in **Display Semantic**. This indicates to companion apps that the value represents a best score for the stat.

6. Select **Select an Instance**, and then select **FastestLap** in **Stat Instance**. This indicates that the featured stat is based on the `FastestLap` stat value.

7. Enter *1* in **Ordinal**. This indicates that this featured stat should be displayed first when listing the featured stats.

8. Select **Ascending** in **Sort Order**. This indicates that lower values are better than higher values, shown as follows.

   <img src="https://mintcdn.com/microsoft-4404708b/gzrdvT5kpqAXKEQt/images/gdk/services/FastestLapHeroStat-XDP.png?fit=max&auto=format&n=gzrdvT5kpqAXKEQt&q=85&s=10ef730c295cc621a9489e24dab801f1" alt="Screenshot of the Fastest Lap hero stat in Partner Center." width="462" height="422" data-path="images/gdk/services/FastestLapHeroStat-XDP.png" />

9. Select **Add New Hero Stat** to create the featured/hero stat.

### Global leaderboard

Using the same lap time stat, we can configure a leaderboard so that every player can see how their lap time is ranked against all other players across the world.

1. In Partner Center, on the left pane, select **Test Sandbox** > **Service Configuration** > **Leaderboards**.

2. Select **New** to create a new global leaderboard.

3. Enter *FastestLap* in **Leaderboard ID**.

4. Select **Select an Instance**, select the **FastestLap** stat rule that we created earlier, and then select **Add Stat Instance**.

5. Select **Ascending** in **Sort**, shown as follows. This selection indicates that lower times are better.

   <img src="https://mintcdn.com/microsoft-4404708b/gzrdvT5kpqAXKEQt/images/gdk/services/FastestLapLeaderboard-XDP.png?fit=max&auto=format&n=gzrdvT5kpqAXKEQt&q=85&s=397f88a12dc095812ec3f83a5da1c3ec" alt="Screenshot of the Fastest Lap leaderboard in Partner Center." width="462" height="321" data-path="images/gdk/services/FastestLapLeaderboard-XDP.png" />

6. Select **Add New Leaderboard** to create a new global leaderboard.

As player stats get updated, the leaderboard is also updated to reflect any change in the rankings.

If there are existing player stats when you create the leaderboard, it can take several hours to update the new leaderboard with these stats.

### Event-based Achievements

We recommend that you use title-managed Achievements. However, for titles that are still using event-based Achievements, you can use events to power the unlocking of achievements on the service, as follows.

**To use events to unlock event-based Achievements**

1. In Partner Center, on the left pane, select **Test Sandbox** > **Service Configuration** > **Achievements & Challenges**.

2. Select **New** to add a new achievement.

3. Enter *Under 60 seconds* in **Achievement Name**.

4. Enter *Completed a lap in under 60 seconds* in **Description**. This description is displayed to players when the achievement is unlocked.

5. Enter *Complete a lap in under 60 seconds* in **Not Achieved Description**. This description is displayed to players when the achievement is still locked.

6. Upload a 1920 × 1080 pixel .png file to serve as the **Achievement Icon**, shown as follows.

   <img src="https://mintcdn.com/microsoft-4404708b/gzrdvT5kpqAXKEQt/images/gdk/services/Under60Achievement-XDP.png?fit=max&auto=format&n=gzrdvT5kpqAXKEQt&q=85&s=a5b278e08b2cb2ff9659f51e309cbcf6" alt="Screenshot of the Add New Achievement dialog in Partner Center." width="338" height="595" data-path="images/gdk/services/Under60Achievement-XDP.png" />

7. Select **Add Achievement** to create the achievement.

8. Ensure that the **Under 60 seconds** achievement is selected in the list of achievements. Select **New** to the right of **Rewards** to give the player a reward for unlocking the achievement.

9. Give the player 20 gamerscore points for unlocking this achievement. In **Reward Type**, select **Gamerscore**.

10. Enter *20* in **Value**, shown as follows.

    <img src="https://mintcdn.com/microsoft-4404708b/gzrdvT5kpqAXKEQt/images/gdk/services/Under60AchievementReward-XDP.png?fit=max&auto=format&n=gzrdvT5kpqAXKEQt&q=85&s=470435fd47f489a6e962741d921d6be8" alt="Screenshot of the Add New Award dialog in Partner Center." width="462" height="250" data-path="images/gdk/services/Under60AchievementReward-XDP.png" />

11. Select **Add Reward**.

12. Ensure that the **Under 60 seconds** achievement is still selected in the list of achievements. Select **New** to the right of **Rules** to define the conditions that unlock the achievement.

13. Enter *Under 60 seconds rule* in **Rule Name**. This value isn't seen by the player, so you can call it whatever you want.

14. Select **Min** in **Action**. This indicates that the achievement is unlocked when the selected stat instance is less than or equal to the threshold value.

15. Enter *60* in **Threshold**.

16. Select **Select an Instance**, select the **FastestLap** stat rule, and then select **Add Stat Instance**.

17. Select **Add Rule** to create the rule for the achievement, shown as follows.

    <img src="https://mintcdn.com/microsoft-4404708b/gzrdvT5kpqAXKEQt/images/gdk/services/Under60AchievementRule-XDP.png?fit=max&auto=format&n=gzrdvT5kpqAXKEQt&q=85&s=09ee171845326525664f12f8e2c8d58d" alt="Screenshot of the Add New Rule dialog for an achievement in Partner Center." width="462" height="338" data-path="images/gdk/services/Under60AchievementRule-XDP.png" />

### Add code to send the event from your game

Now that we've defined the `LapCompleted` event that XBOX services will use to update our player scenarios, we need to add code to the game to send the event when a player completes a racing lap in the game.

To write an event, call the [XblEventsWriteInGameEvent](/reference/live/xsapi-c/events_c/functions/xbleventswriteingameevent) function with the event name of `LapCompleted`.

**Flat C API**

```cpp theme={null}
    HRESULT hr = XblEventsWriteInGameEvent(
        xboxLiveContext,
        "LapCompleted",
        R"({"DifficultyLevelId":100, "GameplayModeId":"Adventure"})",
        R"({"LocationX":1,"LocationY":1})"
    );
```

## See also

[Events overview](/services/xbox-services/player-data/stats-leaderboards/event-based/events/live-game-events)


## Related topics

- [Event-based Stats and leaderboards portal config](/services/xbox-services/player-data/stats-leaderboards/event-based/config/live-statslb-eb-config-nav.md)
- [Config](/services/xbox-services/player-data/stats-leaderboards/event-based/config/index.md)
- [Portal configuration of event-based Featured Stats](/services/xbox-services/player-data/stats-leaderboards/featured-stats/config/live-featuredstats-eb-portal.md)
- [Event-based Stats and Leaderboards](/services/xbox-services/player-data/stats-leaderboards/event-based/live-statslb-eb-nav.md)
- [Event-based Stats overview](/services/xbox-services/player-data/stats-leaderboards/event-based/live-stats-eb-overview.md)
