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

# Writing an event to power an event-based stat

> Sample XblEventsWriteInGameEvent code that fires an XBOX services event so configured stat rules update event-based user stats on the service.

This topic provides example code that shows how to write an event.
On the service, this example code updates event-based stats based on the stat rules that you've defined in Partner Center.

The following example uses the flat C API. `LogToScreen` represents your app's logging function.

```cpp theme={null}
HRESULT hr = XblEventsWriteInGameEvent(
    xboxLiveContext,
    "PuzzleSolved",
    R"({"DifficultyLevelId":100, "GameplayModeId":"Adventure"})",
    R"({"LocationX":1,"LocationY":1})"
);
if (FAILED(hr))
{
    // Log the failure. Don't retry the call in a loop.
    LogToScreen("XblEventsWriteInGameEvent failed: 0x%08X", static_cast<unsigned int>(hr));
}
```

For more information, see
[XblEventsWriteInGameEvent](https://docs.xbox.com/reference/live/xsapi-c/events_c/functions/xbleventswriteingameevent.md).

## Troubleshooting

* **The call returns `E_INVALIDARG`.** *xboxLiveContext* or the event name is null.
* **The call returns `E_XBL_NOT_INITIALIZED`.** Call [XblInitialize](https://docs.xbox.com/reference/live/xsapi-c/xbox_live_global_c/functions/xblinitialize.md) before you write events.
* **The call returns `E_NOTIMPL`.** The XGameEvent feature of the Gaming Runtime isn't available. On a Windows PC, the installed Gaming Runtime Services must include the XGameEvent feature.
* **The call succeeds, but the stat doesn't change.** The event name and the names of the event fields must match the names in your title's service configuration. Names are case-insensitive. If the event name doesn't match an event in the service configuration, the service drops the event without notifying your game.
* **Field values are in the wrong place.** Put fields that have a finite set of numeric or string values, such as difficulty level or game mode, in the dimensions. Put scalar numeric metrics, such as score, time, or position, in the measurements.
* **Updates take time.** Events are written locally on the device, even when the device is offline, and are uploaded when the device is online. If the device is online, event-based stats typically update 5 to 20 seconds after the event is written.


## Related topics

- [Event-based Stats & Leaderboards example code (contents)](https://docs.xbox.com/services/xbox-services/player-data/stats-leaderboards/event-based/how-to/live-statslb-eb-howto-nav.md)
- [Leaderboards](https://docs.xbox.com/build/steam-porting-guide/features/steam-leaderboards.md)
- [Writing an event to power an event-based achievement](https://docs.xbox.com/services/xbox-services/player-data/achievements/event-based/how-to/live-writing-event-based-achievement.md)
- [First glance for event-based Stats](https://docs.xbox.com/services/xbox-services/player-data/stats-leaderboards/event-based/config/live-stats-first-glance.md)
- [Event-based Stats vs. title-managed Stats](https://docs.xbox.com/services/xbox-services/player-data/stats-leaderboards/live-stats-eb-vs-tm.md)
