# How many seconds user was watching video?

**URL:** <https://community.wowza.com/t/how-many-seconds-user-was-watching-video/43236>\
**Category:** Wowza Streaming Engine\
**Created:** [June 19, 2014, 11:57pm UTC](https://community.wowza.com/t/how-many-seconds-user-was-watching-video/43236 "2014-06-19T23:57:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![qwe\_qwe1](https://avatars.discourse-cdn.com/v4/letter/q/bcef8e/32.png) [@qwe\_qwe1](https://community.wowza.com/u/qwe_qwe1)\
**Post date:** [June 19, 2014, 11:57pm UTC](https://community.wowza.com/t/how-many-seconds-user-was-watching-video/43236/1 "2014-06-19T23:57:44Z")

</div>

Hello,

I’d like to implement my own statistics sytem. I’m using [this code](https://www.wowza.com/docs/module-to-get-http-and-rtp-performance-statistics) to count how many seconds user was watching video but my tests are different then reality. For example i’m watching video and shutdown player in 10 sec. but logs show me then I was watching video 15 sec. I tested this module in RTMP, RTSP na HLS streaming but always result are bad. How can I solve this problem? Maybe someone has better method to do this?

Example below:

```auto
public void onStreamDestroy(IMediaStream stream)
	{
		getLogger().info("onStreamDestroy() - invoked");
		
		IOPerformanceCounter perf = stream.getMediaIOPerformance();
		
		Long outbytes = perf.getMessagesOutBytes();
		getLogger().info("Outbytes: " + outbytes);
		
		Double outrate = perf.getMessagesOutBytesRate();
		getLogger().info("OutRate: " + outrate);
		
		Double seconds = outbytes / outrate;
		getLogger().info("Seconds: " + seconds);	
		
	}

```

After 10 sec logs show:

```auto
INFO server comment - Outbytes: 2526989
INFO server comment - OutRate: 80634.0
INFO server comment - Seconds: 31.339000917727013

```

It’s 21 sec. mistake.

---

<div class="post-metadata">

**Author:** ![Matt\_Young](https://sea2.discourse-cdn.com/flex002/user_avatar/community.wowza.com/matt_young/32/389_2.png) [@Matt\_Young](https://community.wowza.com/u/Matt_Young)\
**Post date:** [June 20, 2014, 7:05am UTC](https://community.wowza.com/t/how-many-seconds-user-was-watching-video/43236/2 "2014-06-20T07:05:44Z")

</div>

If you are looking to obtain information on a given session/client connection, you might review our [serverinfo HTTP Provider](https://www.wowza.com/docs/how-to-get-detailed-server-info-with-an-http-provider) source code as a starting point. There are even elapsed time and time running related functions that could be of use on a per connection basis.

---

<div class="post-metadata">

**Author:** ![Matt\_Young](https://sea2.discourse-cdn.com/flex002/user_avatar/community.wowza.com/matt_young/32/389_2.png) [@Matt\_Young](https://community.wowza.com/u/Matt_Young)\
**Post date:** [July 8, 2014, 3:39am UTC](https://community.wowza.com/t/how-many-seconds-user-was-watching-video/43236/3 "2014-07-08T03:39:37Z")

</div>

You might try eliminating the global variable for iClient and utilizing the following:

```auto
stream.getClient().getElapsedTime().getTimeSeconds()

```

Alternatively, it may prove more precise to check this within the onDisconnect event as follows:

```auto
 public void onDisconnect(IClient client) {
  getLogger().info("onDisconnect: " + client.getElapsedTime().getTimeSeconds());
 }

```

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/community.wowza.com/system/32/447_2.png) [@system](https://community.wowza.com/u/system)\
**Post date:** [June 22, 2014, 12:53pm UTC](https://community.wowza.com/t/how-many-seconds-user-was-watching-video/43236/4 "2014-06-22T12:53:59Z")

</div>

Hi!

Wowza Wrench has a feature that allows you to measure the elapsed time per user. It also uses client.getElapsedTime().getTimeSeconds() under the hood. When I was investigating time differences, I found that maybe connections are administered periodically in Wowza and that’s why there is a varying difference between the time you see and the time the client object reports. There are some idle\* settings in VHost.xml which might have effect on this. Would be good to get some official explanation on the inaccuracy we both saw.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/community.wowza.com/system/32/447_2.png) [@system](https://community.wowza.com/u/system)\
**Post date:** [July 8, 2014, 9:48am UTC](https://community.wowza.com/t/how-many-seconds-user-was-watching-video/43236/5 "2014-07-08T09:48:21Z")

</div>

Hi,

Can you explain why can may it be more precise withing onDisconnect…? And why we are seeing difference between the time we measure and what the client object reports…?

---

<div class="post-metadata">

**Author:** ![qwe\_qwe1](https://avatars.discourse-cdn.com/v4/letter/q/bcef8e/32.png) [@qwe\_qwe1](https://community.wowza.com/u/qwe_qwe1)\
**Post date:** [June 20, 2014, 4:40am UTC](https://community.wowza.com/t/how-many-seconds-user-was-watching-video/43236/6 "2014-06-20T04:40:26Z")

</div>

I tried use [ElapsedTimer](https://www.wowza.com/resources/serverapi/com/wowza/util/ElapsedTimer.html):

```auto
public void onStreamDestroy(IMediaStream stream) {
		getLogger().info("Seconds: " + iClient.getElapsedTime().getTimeSeconds());
}

```

but result is also wrong.
