So I've been experimenting with the Google Analytics API, however, I con't figure out how they 'calculate' the sessionDuration
. On the dashboard I got the value 5m 58s
, which I want to get via their API. So I execute the following API request:
https://www.googleapis.com/analytics/v3/data/ga?ids=ga%3A133001501&start-date=2019-01-01&end-date=2019-01-31&metrics=ga%3AsessionDuration&access_token=ya29.GlufBlMhXovMyujgGRTYaUd-1afB99IGjEX5EhoAM9HlSPqqYoAEWaEAKKJI7RvdGNwALRODkHtaYqXfHv8cSHfa7Uevzbf2J5TplU6ZWPVlBix9p1U_jUcj8va4
(Link to the report, in case you can't see the output anymore.)
That returns something like this: "96821.0"
But what is this exactly and how can I convert this to x minutes y seconds?
I thought, those are seconds, so I can use this type of code:
<?php
$init = 96821;
$minutes = floor(($init / 60) % 60);
$seconds = $init % 60;
echo "$minutes:$seconds";
?>
But that gives me 53:41
, and the analytics panel says 5m 58s
...
What is the solution here? I can't find it anywhere online...