I'm not entirely sure if you always want to track from a specific page, but if you are looking for averages you shouldn't need to worry about individual page timings.
If you are looking for the average page timings across the site, then you just need the earliest time the session was active, the last time, and the number of pages visited over that time period. Dividing the time difference by page count gives the average. You can then call summarize
again to get the overall average.
Note- I'm subtracting 1 from the page count because I don't have the time between the last page was opened and when they left.
Code:
let userTiming = requests | summarize PageViews=count()-1, StartTime=min(timestamp), EndTime=max(timestamp) by session_Id;
userTiming
| extend AverageTime=datetime_diff('millisecond', EndTime, StartTime)/PageViews
//| summarize avg(AverageTime)
If you are looking for the time spent on a specific page based on the user's next pageview, then your initial idea of joining the table to itself wasn't bad, but we can execute it a bit differently to avoid the < join.
- Get a table that only contains the views for a given page.
- Join that back onto the requests table based on the session Id alone.
- Filter that down so we only have requests that came after the ones to our specific page.
- Use summarize to only get the minimum timestamp that comes after our first pageview.
- Get the average difference by session Id
- Just as before, pairing the
avg
functions with .summarize
can give you the overall average.
Code:
let startTime = requests | where name == "Function2" | project session_Id, startingTimestamp=timestamp;
let userTiming = requests
| join kind=inner (startTime) on $left.session_Id == $right.session_Id
| where timestamp > startingTimestamp
| summarize nextTimestamp=min(timestamp) by startingTimestamp, session_Id;
userTiming
| summarize AverageTime=avg(datetime_diff('millisecond', nextTimestamp, startingTimestamp)) by session_Id
//| summarize avg(AverageTime)