# Trying to convert a session query into a custom metric for the first time

**URL:** <https://community.dynatrace.com/t/trying-to-convert-a-session-query-into-a-custom-metric-for-the-first-time/52237>\
**Category:** Open Q&A\
**Tags:** dynatrace-managed, usql, dynatrace-api\
**Created:** [June 25, 2025, 12:05pm UTC](https://community.dynatrace.com/t/trying-to-convert-a-session-query-into-a-custom-metric-for-the-first-time/52237 "2025-06-25T12:05:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![runatyr](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/runatyr/32/11396_2.png) [@runatyr](https://community.dynatrace.com/u/runatyr)\
**Post date:** [June 25, 2025, 12:05pm UTC](https://community.dynatrace.com/t/trying-to-convert-a-session-query-into-a-custom-metric-for-the-first-time/52237/1 "2025-06-25T12:05:40Z")

</div>

Hello:

I am trying to convert a session query into custom metric.

At the end of the day. I want to be able to show in a bar char the amount of user session every 15 minutes.

I need to change this into an API call since we will be using the API to export the data into another system.

In the graph below (in Dynatrace) I have this being done for the last 12 hours. However, when I click on “Create Custom Metric”

**I get the following errors/alerts that it can not convert.**

**How can the query above be converted so that it is custom metric friendly?**

See the error and images below.

This is the query and alerts associated when changing to a custom metric.

--\>

SELECT DISTINCT DATETIME(starttime, ‘E/dd--HH:mm’, ‘15m’), COUNT (\*) FROM usersession WHERE startTime \>= $NOW - DURATION(“12h”) AND endTime \<= $NOW and browserFamily !=“Synthetic monitor”

--\>

 ![Screenshot 2025-06-25 080249.png](https://global.discourse-cdn.com/dynatrace/original/3X/5/f/5f355d4d16b575e8223df764b830c19faa80c9de.png)

 ![Screenshot 2025-06-25 080321.png](https://global.discourse-cdn.com/dynatrace/original/1X/9dc3c81eddb7c51f8da44ba7643d8fe468e72e78.png)

---

<div class="post-metadata">

**Author:** ![radek\_jasinski](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/radek_jasinski/32/10432_2.png) [@radek\_jasinski](https://community.dynatrace.com/u/radek_jasinski)\
**Post date:** [June 25, 2025, 12:34pm UTC](https://community.dynatrace.com/t/trying-to-convert-a-session-query-into-a-custom-metric-for-the-first-time/52237/2 "2025-06-25T12:34:28Z")

</div>

Hi,

If you want to count the number of user sessions every 15 minutes and expose this data via the API, you can’t directly convert a USQL query using DATETIME and DISTINCT into a custom metric. Dynatrace does not support this when creating custom metrics. Instead, you should use the Settings API and the builtin:custom-metrics schema to define your own COUNTER-type metric, which will automatically count closed user sessions. You can add a filter, for example, to exclude synthetic sessions (browserFamily != “Synthetic monitor”). Once the metric is created, the data will be collected automatically and will be available through the Metrics API. There, you can apply time aggregation, such as rollup(sum,15m), to get the number of sessions in 15-minute intervals.

> **[Create USQL custom metrics for web applications in RUM Classic](https://docs.dynatrace.com/docs/observe/digital-experience/rum-classic/web-applications/additional-configuration/custom-metrics-from-user-sessions)**
>
> Every time a user session is closed, Dynatrace can extract metrics and store them as time series. Learn how to set up and use USQL custom metrics for web applications.

Radek

---

<div class="post-metadata">

**Author:** ![runatyr](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/runatyr/32/11396_2.png) [@runatyr](https://community.dynatrace.com/u/runatyr)\
**Post date:** [June 25, 2025, 3:16pm UTC](https://community.dynatrace.com/t/trying-to-convert-a-session-query-into-a-custom-metric-for-the-first-time/52237/3 "2025-06-25T15:16:43Z")

</div>

I was able to make the metric under the settings--\>user session custom metrics--\> Add item

(See below)

Would this accomplish the same thing as the “Settings” API method?

I should be able to go in an query this from metrics now…correct?

 ![Screenshot 2025-06-25 111619.png](https://global.discourse-cdn.com/dynatrace/original/3X/1/9/1986dea01b535dc3c5c686684ad566db23335352.png)
