Skip to main content

Media customers that are interested in tracking the amount of time users spend on their products would be interested in instrumenting a property that measures the duration between two events. With this property, users can then measure various items such as Average Property Value, Median Property Value, or use the duration values in complex formulas in our Event Segmentation: Formulas chart. 

 

To track duration, you would need to capture the timestamp of a Start Event and timestamp of an End Event, calculate the difference between the two timestamps, and then pass in the duration as an event property for the End Event. In our demo account this End Event is called Played Song, and here is an example chart: https://analytics.amplitude.com/demo/chart/new/snewb09 

 

As an example for Page View events, let's say you have a Start Event = 'Viewed Home Page' and an End Event = 'Clicked Signup'. When the user fires the 'Viewed Home Page' event, you can create a variable in your code called first_timestamp and pass in the current timestamp. When the user is going to fire the 'Clicked Signup' event, you would calculate the difference between the current timestamp and first_timestamp and pass that value into the 'duration' event property. You would then send the 'Clicked Signup' event with the 'duration' event property. This way, you can segment and filter on the value of the 'duration' event property for the 'Clicked Signup' event to see how long it took users to click that button after viewing your home page.

 

We also recommend normalizing the 'duration' event property to a numerical value like seconds so that you can perform functions such as property average in the UI.

@daniel.balan if there are multiple exit points from the home page besides sign up (say Viewed Help Site), then it might make more sense to associate duration with the “Viewed Home Page” event, instead of “Clicked Signup”. Otherwise it would be difficult to figure out time spent on home page, by adding up durations sent in all the exits events from the home page.

I am wondering if there is a way to “update” duration in the Viewed Home Page event after it has been logged. May be takings event id and using some api to update duration in it, either from the client itself or server.

Or may be another way.


@dadude  hey! any update to the duration property would have to be done before the event is fired and reaches Amplitude. Once the event reaches Amplitude, the event cannot be updated, since historical data cannot be tampered with.


Thanks for the response @daniel.balan . For now we have added an additional generic event which contains duration and the screen for which duration is sent. Its of course going to double our screen view count.

I started learning and using amplitude about a month back. And it’s totally awesome, after the dark ages I spent using ga and firebase. But this one little thing has proven to be quite an irritant. Tracking time spent on screen is a pretty basic requirement for measuring “engagement”.


@Amplitude Admin

Would this be possible using the amplitude event timestamps of the 2 different timestamps we would derive the “duration” from? 
So for example START_EVENT has a timestamp we send to Amplitude (or if we don’t have a timestamp it is derived by your server)
And END_EVENT has a similar timestamp derived from the the time the event arrived at the server.

So how can I derive the time between the two events (duration) without manually creating a UNIX timestamp custom event property. Is this possible?


Hi @n8descript 😃 Thanks for this question. I’ve looped in @ning.chang, who will take a closer look. In the meantime, I ran a search in the community and found these threads that might help. Please keep us posted as you make progress! 

 


hi @n8descript, you can check out the derived properties function to calculate this! https://help.amplitude.com/hc/en-us/articles/360058731292-Derived-properties-Add-new-event-and-user-properties-retroactively#date-time-functions this feature is available for customers with the govern add-on! 


If you can’t derive it from existing amplitude events (I want to track video watch duration, there’s not necessarily a nice end event) then you can track it yourself. I understand that events in amplitude are immutable, so updating duration on an amplitude event is not possible. So we’ll have to update it ourselves before we send the final duration.

ping duration to my server then send a server event to amplitude with the total duration

Something like below should work:

every x seconds, update my server with the latest duration
POST myserver.com/events
{
  userId: abc123,
  mediaId: abc123,
  currentDuration: 15
}

Model on server, in database etc
{
  userId: abc123,
  mediaId: abc123,
  duration: 150,
  lastEventAt: 2020-01-01T00:00:00Z
}

cron job every x minutes to look back and send presumably finished durations via amplitude server events
track(“media_consumed_duration”, {
  userId: abc123,
  mediaId: abc123,
  duration: 150
}, {
  // use the lastEventAt for the event time because we’re actually sending it a few minutes later
  time: 2020-01-01T00:00:00Z
})


@Compilations Dev nice one!!


How then do you track the end time-stamp when the user sends the app to the background?

It’s possible to handle it with AppStates and EventListeners but it’s kind of a mess and not working properly.


Reply