# User Session\\Action API Get User count for application

**URL:** <https://community.dynatrace.com/t/user-session-action-api-get-user-count-for-application/20518>\
**Category:** Real User Monitoring\
**Tags:** user-actions, user-sessions\
**Created:** [May 1, 2019, 4:20pm UTC](https://community.dynatrace.com/t/user-session-action-api-get-user-count-for-application/20518 "2019-05-01T16:20:04Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![robert\_auten](https://avatars.discourse-cdn.com/v4/letter/r/ccd318/32.png) [@robert\_auten](https://community.dynatrace.com/u/robert_auten)\
**Post date:** [May 1, 2019, 4:20pm UTC](https://community.dynatrace.com/t/user-session-action-api-get-user-count-for-application/20518/1 "2019-05-01T16:20:04Z")

</div>

I am trying to use the API to get real user counts per application. I have tried the following but it isn’t matching what the console is showing. Can someone shed some light on this? It seems I am getting a fraction of the userids and some of the ones that are anon it is returning those as ‘null’

select usersession.userid FROM usersession where usersession.userType = ‘REAL\_USER’ AND useraction.application = ‘My Awesome Application’ GROUP BY usersession.userid

---

<div class="post-metadata">

**Author:** ![robert\_auten](https://avatars.discourse-cdn.com/v4/letter/r/ccd318/32.png) [@robert\_auten](https://community.dynatrace.com/u/robert_auten)\
**Post date:** [May 1, 2019, 4:36pm UTC](https://community.dynatrace.com/t/user-session-action-api-get-user-count-for-application/20518/2 "2019-05-01T16:36:12Z")

</div>

It looks like the API is capping at 50 rows.

---

<div class="post-metadata">

**Author:** ![robert\_auten](https://avatars.discourse-cdn.com/v4/letter/r/ccd318/32.png) [@robert\_auten](https://community.dynatrace.com/u/robert_auten)\
**Post date:** [May 1, 2019, 6:59pm UTC](https://community.dynatrace.com/t/user-session-action-api-get-user-count-for-application/20518/3 "2019-05-01T18:59:52Z")

</div>

Yep I found where you can set this: The framework always applies an upper limit to prevent system overload. If `LIMIT` is not used, 50 results are returned by default.

---

<div class="post-metadata">

**Author:** ![joshua\_dtrace](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/joshua_dtrace/32/20014_2.png) [@joshua\_dtrace](https://community.dynatrace.com/u/joshua_dtrace)\
**Post date:** [May 1, 2019, 7:34pm UTC](https://community.dynatrace.com/t/user-session-action-api-get-user-count-for-application/20518/4 "2019-05-01T19:34:25Z")

</div>

I believe from my testing the maximum rows that can be returned is 5000. So, adding a “LIMIT 5000” to the end of the USQL request would be required..

---

<div class="post-metadata">

**Author:** ![robert\_auten](https://avatars.discourse-cdn.com/v4/letter/r/ccd318/32.png) [@robert\_auten](https://community.dynatrace.com/u/robert_auten)\
**Post date:** [May 1, 2019, 7:38pm UTC](https://community.dynatrace.com/t/user-session-action-api-get-user-count-for-application/20518/5 "2019-05-01T19:38:41Z")

</div>

Yes I was able to get it to return the data although if I tell it to give me the count of userids in the query, the number doesn’t match the user count from the User session console. I am thinking I don’t have a filter or it is ignoring the timeframe filter because each time I run the query the number changes.

Query I am trying:

startTimestamp=1556733966561&endTimestamp=1556737361768&query=select count(userid) FROM usersession where usersession.userType = ‘REAL\_USER’ AND useraction.application = ‘My Awesome App’ LIMIT 5000

---

<div class="post-metadata">

**Author:** ![joshua\_dtrace](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/joshua_dtrace/32/20014_2.png) [@joshua\_dtrace](https://community.dynatrace.com/u/joshua_dtrace)\
**Post date:** [May 1, 2019, 7:40pm UTC](https://community.dynatrace.com/t/user-session-action-api-get-user-count-for-application/20518/6 "2019-05-01T19:40:59Z")

</div>

Hey Robert!

The USQL and User Sessions API only work for completed/finished sessions, whereas the in-UI User Sessions page will also show “Live”/in-progress sessions.

If your timestamp is recent enough, this could be the reason why you are seeing differences.

---

<div class="post-metadata">

**Author:** ![robert\_auten](https://avatars.discourse-cdn.com/v4/letter/r/ccd318/32.png) [@robert\_auten](https://community.dynatrace.com/u/robert_auten)\
**Post date:** [May 1, 2019, 8:04pm UTC](https://community.dynatrace.com/t/user-session-action-api-get-user-count-for-application/20518/7 "2019-05-01T20:04:52Z")

</div>

Yes, I set the startime to be 0 so that it will give me the last 2 hours and I changed the UI to show me the last 2 hours? I am trying to get the unique users that have access my website in the last hour. The same data that shows up when I click on the User Sessions in the UI. You are saying though that the API will only give me the completed correct? I am just trying to get a count of how many users have been to my website in the last 2 hours. So this would be the count of users that have completed sessions and current live users.

---

<div class="post-metadata">

**Author:** ![joshua\_dtrace](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/joshua_dtrace/32/20014_2.png) [@joshua\_dtrace](https://community.dynatrace.com/u/joshua_dtrace)\
**Post date:** [May 1, 2019, 8:34pm UTC](https://community.dynatrace.com/t/user-session-action-api-get-user-count-for-application/20518/8 "2019-05-01T20:34:00Z")

</div>

Live sessions are most commonly seen during the last 2 hours, so that is correct that the USQL results will be different by only having the completed sessions displayed.

---

<div class="post-metadata">

**Author:** ![robert\_auten](https://avatars.discourse-cdn.com/v4/letter/r/ccd318/32.png) [@robert\_auten](https://community.dynatrace.com/u/robert_auten)\
**Post date:** [May 1, 2019, 8:43pm UTC](https://community.dynatrace.com/t/user-session-action-api-get-user-count-for-application/20518/9 "2019-05-01T20:43:24Z")

</div>

Is there any way in the API to get live session data? If I am only counting completed sessions, I am not really getting the true count of users that have been or are still on the website in the last 2 hours. The UI gives me this information.

---

<div class="post-metadata">

**Author:** ![joshua\_dtrace](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/joshua_dtrace/32/20014_2.png) [@joshua\_dtrace](https://community.dynatrace.com/u/joshua_dtrace)\
**Post date:** [May 1, 2019, 9:00pm UTC](https://community.dynatrace.com/t/user-session-action-api-get-user-count-for-application/20518/10 "2019-05-01T21:00:31Z")

</div>

Not using the USQL API, no. There is an upcoming improvement in the next quarter or so to include a “user count” for Web Applications via the Timeseries API. This might help with your use-case.

---

<div class="post-metadata">

**Author:** ![robert\_auten](https://avatars.discourse-cdn.com/v4/letter/r/ccd318/32.png) [@robert\_auten](https://community.dynatrace.com/u/robert_auten)\
**Post date:** [May 1, 2019, 9:20pm UTC](https://community.dynatrace.com/t/user-session-action-api-get-user-count-for-application/20518/11 "2019-05-01T21:20:11Z")

</div>

Thank you for that information! What I probably will end up doing is just checking the numbers once a day at a time when there shouldn’t be much traffic on the website. That at least will give me a good ball park of users that used the system during the day.

---

<div class="post-metadata">

**Author:** ![joshua\_dtrace](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/joshua_dtrace/32/20014_2.png) [@joshua\_dtrace](https://community.dynatrace.com/u/joshua_dtrace)\
**Post date:** [May 1, 2019, 9:21pm UTC](https://community.dynatrace.com/t/user-session-action-api-get-user-count-for-application/20518/12 "2019-05-01T21:21:38Z")

</div>

You’re very welcome, Robert!  
And sounds good!

---

<div class="post-metadata">

**Author:** ![Montalvo](https://avatars.discourse-cdn.com/v4/letter/m/e95f7d/32.png) [@Montalvo](https://community.dynatrace.com/u/Montalvo)\
**Post date:** [September 30, 2022, 3:51pm UTC](https://community.dynatrace.com/t/user-session-action-api-get-user-count-for-application/20518/13 "2022-09-30T15:51:12Z")

</div>

Greetings Joshua,

I’m currently trying to get the same exact data as Robert, I was checking the Timeseries API but I couldn’t understand how this can be possible using it.

I was also reading and find out that there’s an “extrapolationLevel” field that according to the documentation, it means:

> The number is the denominator of a fraction and indicates the amount of actual data. The value&nbsp;1&nbsp;means that the result contains only the actual data. The value&nbsp;4&nbsp;means that result is calculated using 1/4 of the actual data.

How can I change this value in order to be set to 1 since I’ve tried passing it on the params request but it seems like it is not mutable.

Here’s an example of my results:

Request:

```python
import requests

headers = {
    'accept': 'application/json',
    'Authorization': 'Api-Token dt0c01.xxxxxx-xxxxxxx',
}

params = {
    'query': 'SELECT COUNT(DISTINCT ip) AS \'distinct_ip\' FROM usersession WHERE (userType=\'REAL_USER\' AND (useraction.application=\'sandals.com\' OR userevent.application=\'sandals.com\' OR usererror.application=\'sandals.com\')) LIMIT 1000',
    'startTimestamp': '3600000',
    'addDeepLinkFields': 'false',
    'explain': 'false',
}

response = requests.get('https://hudxxxxx.live.dynatrace.com/api/v1/userSessionQueryLanguage/table', params=params, headers=headers)

print(response.json())

```

Response:

```python
{
  "extrapolationLevel": 4,
  "columnNames": [
    "distinct_ip"
  ],
  "values": [
    [
      1489990
    ]
  ]
}

```

According to my Session Query results, the “values” should be around 500-600ish

Can you please help me!

Regards,
