How to format Average Session Duration?

827 views
Skip to first unread message

Becky Hillard

unread,
Sep 23, 2015, 9:19:45 AM9/23/15
to Google Analytics Reporting API

Hi 


I have set up Google sheets to pull through data from the analytics API. I am struggling to format some of the cells it pulls through e.g.


Average session duration isn't showing correctly and I can't work out how to format it.


The average duration on one page should be 00:03:01, but it showing as 181.0740741 - does anyone have any ideas?


Thank you!

Matt

unread,
Sep 24, 2015, 11:42:29 PM9/24/15
to Google Analytics Reporting API
There is a great answer on stackoverflow.

String.prototype.toHHMMSS = function () {
    var sec_num = parseInt(this, 10); // don't forget the second param
    var hours   = Math.floor(sec_num / 3600);
    var minutes = Math.floor((sec_num - (hours * 3600)) / 60);
    var seconds = sec_num - (hours * 3600) - (minutes * 60);

    if (hours   < 10) {hours   = "0"+hours;}
    if (minutes < 10) {minutes = "0"+minutes;}
    if (seconds < 10) {seconds = "0"+seconds;}
    var time    = hours+':'+minutes+':'+seconds;
    return time;
}

You can use it now like:

alert("5678".toHHMMSS());

Matt
Reply all
Reply to author
Forward
0 new messages