While redesigning Classical KUSC, one of our primary objectives was to improve how we measure radio listening online. Precisely, how do we measure listening behavior across our website, iPhone, Android, and Apple TV app using a single system such as Google Analytics. As of now, the answer is through Google Tag Manager.

I love Google Analytics. I've been tracking page views and events since 2005, and it has never failed me. In the past seven years or so, many competitors have entered the analytics space, including Flurry, Fabric Answers, Mixpanel, etc. but you can pretty much do anything using GA. What's also interesting is the evolution of Google Tag Manager. What I specifically like about GTM is that you can use that as your tool to listen for pageviews and events. Still, then you can route the data to Google Analytics or even Facebook analytics, so you only really need one library to rule them all.

Measuring listening behavior using

I recently created this script to help track how long a user is listening. This is particularly relevant in online radio because the behavior of most listeners is to visit the site, look for and select the play button, let the music begin , then forget and move on. This form of passive listening is very common in radio but Google Analytics wasn't designed for non-interactive listening. In fact, GA was intended to track the length of time it takes to move from one page to the next. Therefore, this script helps remedy that problem.

function() {
  var timerE = isNaN({{timer-event-number}}) ? 0 : {{timer-event-number}};
  var timerI = isNaN({{timer-interval}}) ? 0 : {{timer-interval}};
  
  var elapsed = timerE * timerI / 1000;
  var min = Math.floor(elapsed/60);
  var sec = elapsed % 60;
  return min + 'm ' + sec + 's';
}
//The new Way
function() {
  //1: This will prevent "NaN" showing up in GA reports
  var timerE = isNaN({{timer-event-number}}) ? 0 : {{timer-event-number}};
  var timerI = isNaN({{timer-interval}}) ? 0 : {{timer-interval}};
  //2: Format the data in HH:MM:SS.mmm format
  function addZ(n) {
    return (n<10? '0':'') + n;
  }
  //3: Calculate the elapsed time
  var s = timerI * timerE;
  //4: Crunch
  var ms = s % 1000;
  s = (s - ms) / 1000;
  var secs = s % 60;
  s = (s - secs) / 60;
  var mins = s % 60;
  var hrs = (s - mins) / 60;

  //return addZ(hrs) + ':' + addZ(mins) + ':' + addZ(secs) + '.' + ms;
  return addZ(hrs) + ':' + addZ(mins) + ':' + addZ(secs);
}

Source from Stackoverflow