wowvxw
Last Updated: February 25, 2016
·
1.31K
· mohangk

Evaluating CDN perforamance for your users using Google Analytics

We needed to compare the performance of 2 different CDNs. To get a sense of the performance of the different CDNs based on our users location, we embedded some simple javascript on our webpage and used Google Analytics event tracking to monitor how the CDNs actually performed for our end users.

The code that we embedded on our website is as follows.

function dl(url) {
  var imageAddr = url + "?n=" + Math.random();
  var startTime, endTime;
  var download = new Image();

  download.onload = function () {
    endTime = (new Date()).getTime();
    var duration = (endTime - startTime);
    _gaq.push(['all._trackEvent', 'Speedtest', url, ' '+duration, duration]);
    //console.log(url+" >"+ duration);
  }
  startTime = (new Date()).getTime();
  download.src = imageAddr;
}

if(Math.random() > 0.66) {
  dl('http://cdn1.test.com/1x1.png');
  dl('http://cdn2.test.com/1x1.png');
}

We track how long it takes to download a one pixel image from each CDN and then send the information to Google Analytics for tracking purposes. Google Analytics can track the location the event is coming from and this is useful to know how the CDN performs for different locations. The numbers are not perfect but we believe good enough for a comparative analysis. More information on GA tracking - https://developers.google.com/analytics/devguides/collection/gajs/eventTrackerGuide