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

# Handling an event-based stat change

> Example XSAPI code for subscribing to XBOX Live event-based stat change notifications and cleaning up handlers using the Real-Time Activity service.

This topic provides example code for handling an event-based stat change.

**To get notifications about event-based stat changes**

1. [Add a Stat-changed handler](#ID4EM1)
2. [Subscribe to a specific stat change](#ID4EM2)

**To clean up**

1. [Remove a Stat-changed handler](#ID4EM3)
2. [Unsubscribe from a specific stat change](#ID4EM4)

<a id="ID4EM1" />

## Add a Stat-changed handler

Register an event handler for statistic change notifications by using the following example code.
The event handler receives an [XblStatisticChangeEventArgs](/reference/live/xsapi-c/user_statistics_c/structs/xblstatisticchangeeventargs) object.

#### Flat C API

```cpp theme={null}
void* context{ nullptr };
XblFunctionContext statisticChangedFunctionContext = XblUserStatisticsAddStatisticChangedHandler(
    xboxLiveContext,
    [](XblStatisticChangeEventArgs eventArgs, void* context) 
    {
        // Handle stat change. 
        LogToScreen("Statistic changed callback: stat changed (%s = %s)",
            eventArgs.latestStatistic.statisticName, 
            eventArgs.latestStatistic.value); 
    },
    context
);
```

For more information, see the following:

* [XblStatisticChangeEventArgs](/reference/live/xsapi-c/user_statistics_c/structs/xblstatisticchangeeventargs)
* [XblUserStatisticsAddStatisticChangedHandler](/reference/live/xsapi-c/user_statistics_c/functions/xbluserstatisticsaddstatisticchangedhandler)

<a id="ID4EM2" />

## Subscribe to a specific stat change

Subscribe to statistic update notifications via the `XblUserStatisticsSubscribeToStatisticChange` handler by using the following example code.

#### Flat C API

```cpp theme={null}
XblRealTimeActivitySubscriptionHandle subscriptionHandle{ nullptr };

HRESULT hr = XblUserStatisticsSubscribeToStatisticChange(
    xboxLiveContext,
    xboxUserId,
    scid,
    statisticName.c_str(),
    &subscriptionHandle
);
```

<a id="ID4EM3" />

## Remove a Stat-changed handler

Remove an event handler for statistic change notifications by using the following example code.

#### Flat C API

```cpp theme={null}
XblUserStatisticsRemoveStatisticChangedHandler(
        xboxLiveContext,
        statisticChangedFunctionContext
);
```

<a id="ID4EM4" />

## Unsubscribe from a specific stat change

Unsubscribe a previously created statistic change subscription by using the following example code.

#### Flat C API

```cpp theme={null}
hr = XblUserStatisticsUnsubscribeFromStatisticChange(
    xboxLiveContext,
    statisticChangeSubscriptionHandle
);
```


## Related topics

- [Event-based Stats & Leaderboards example code](/services/xbox-services/player-data/stats-leaderboards/event-based/how-to/live-statslb-eb-howto-nav.md)
- [Getting event-based stats](/services/xbox-services/player-data/stats-leaderboards/event-based/how-to/live-getting-eb-stat.md)
- [Event-based Stats and leaderboards portal config](/services/xbox-services/player-data/stats-leaderboards/event-based/config/live-statslb-eb-config-nav.md)
- [Portal configuration of event-based Featured Stats](/services/xbox-services/player-data/stats-leaderboards/featured-stats/config/live-featuredstats-eb-portal.md)
- [First glance for event-based Stats](/services/xbox-services/player-data/stats-leaderboards/event-based/config/live-stats-first-glance.md)
