This one is deprecated since the demise of Gadgets on sites.
As described in Database abstraction of site results, each site page has some associated analytics data stored in a database which is updated through a GAS trigger. The page gadget which displays that data is a JavaScript app, which uses the same technique as JSON API for data abstraction classes to serve analytics data to pages.
You can take a copy of this web service here

The code

/** intended to be run in a google sites page
 * this looks up popularity data generated from analytics
 * and adds it to your page using htmlservice & html5 canvas
 **/
function doGet(e) {
  e =  e || {parameter:{}};
  var params = e.parameter;
  
  // now we shoud have found the page on the site
  var result = {data:null , error:null};
  var d = siteStats(params.page ,params.sitecode) ;
  if (!d) { 
    result.error = 'no data';
  }
  else {
    result.data = d;
  }

  
  var s = JSON.stringify(result) ;
  var mime = ContentService.MimeType.JSON;
  if (params.callback) {
    s = params.callback + "(" + s + ")";
    mime = ContentService.MimeType.JAVASCRIPT;
  }
    
  return ContentService.createTextOutput(s).setMimeType(mime);

}

/**
 * get the stats associated with this page
 * @param {string} url the url of the page
 * @param {string} siteCode the code to look up the stats
 * @return {object} the data for this page
 **/
function siteStats (url,siteCode) {

  // find the data for this url in the analytics summary
  return cSiteStats.siteStats (url,siteCode);

}
function showMyScriptAppResource(s) {
  try {
    return ScriptApp.getResource(s);
  }
  catch (err) {
    throw err + " getting script " + s;
  }
}

See more like this in Database abstraction with google apps script and Displaying analytics data on site pages