How to calculate the avg and total duration each page has been visited by the users

I have a datatype called Page view.

Two fields called “current page” and “from page”

When the user clicks the button on each page, the page names are saved to the “Page view” using a custom state.

How do I calculate the average duration each page is visited and the total duration each page is visited?

I thought it would be possible with the formula below but It’s not letting me put this formula in.
I have a repeating group with each page’s name.
I tried:
do a search for page views
Constraint: current page = parent groups page name
each items creation date - do a search for page views
Constraint: from page = parent groups page name, each items creation date

This is what Google Analytics is for. Don’t build this into your app.

Whats the issue with building it into my app? I figured it would be easier than learning google analytics

The reason i thought it would be easier is that I’m creating a mobile app so its all built out on one page and I’m using custom states to navigate through the app rather than URLs or URL parameters.