The following Google Apps Script can be used to surface top content i.e. most viewed pages in a Google Site. The script uses Google Analytic APIs. You will need to setup a Google Analytic for your Google Site. Here is the gadget in action: You will need to change the following two items:
function doGet(e) { // Style Sheet for Links and Title var _links = { "color": "#F26522", "font": "14px Verdana", "line-height": "24px" }; var _title = { "color": "#579BA5", "font": "18px Verdana" }; //End of Style Sheet // The following two variables need to be modified var id = "ga:42714"; // Analytics ID goes here var statsInterval = 1; // Interval of the stats in number of days // End var app = UiApp.createApplication(); var vPanel = app.createVerticalPanel(); app.add(vPanel); var appTitle = app.createLabel("Top Content"); applyCSS(appTitle, _title); vPanel.add(appTitle); var baseURL = "https://sites.google.com"; var today = new Date(); var month = today.getMonth(); month++; month = (month < 10) ? ("0" + month) : month; var date = today.getDate(); date = (date < 10) ? ("0" + date) : date; var endDate = today.getYear() + "-" + month + "-" + date ; var thirtyDaysAgo = new Date(); thirtyDaysAgo.setDate(today.getDate()-statsInterval); var month = thirtyDaysAgo.getMonth(); month++; month = (month < 10) ? ("0" + month) : month; var date = thirtyDaysAgo.getDate(); date = (date < 10) ? ("0" + date) : date; var startDate = (thirtyDaysAgo.getYear() + "-" + month + "-" + date); var oAuthConfig = UrlFetchApp.addOAuthService("Analytics"); oAuthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken"); oAuthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope=https://www.google.com/analytics/feeds/"); oAuthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken"); oAuthConfig.setConsumerKey("anonymous"); oAuthConfig.setConsumerSecret("anonymous"); var options = {"oAuthServiceName": "Analytics", "oAuthUseToken":"always" }; var response = UrlFetchApp.fetch("https://www.google.com/analytics/feeds/data?ids=" + id + "&dimensions=ga:pageTitle,ga:pagePath&metrics=ga:visitors&segment=gaid::-1&sort=-ga:visitors&start-date=" + startDate+ "&end-date=" + endDate + "&max-results=7", options); var shareDoc = Xml.parse(response.getContentText(), false); var root = shareDoc.getElement(); // feed element var entryList = ""; var content = ""; var shareHTML = ""; var entries = root.getElements("entry"); for (var i=0; i<entries.length; i++) { var e = entries[i]; var title = e.getElement("title").getText(); var id = e.getElement("id").getText(); var updateDate = e.getElement("updated").getText(); var dimensions = e.getElements("http://schemas.google.com/analytics/2009", "dimension"); var title = dimensions[0].getAttribute("value").getValue(); var titleSplitLocation = title.lastIndexOf("-"); if (titleSplitLocation>1) title = title.substr(0, titleSplitLocation); var link = baseURL + dimensions[1].getAttribute("value").getValue(); var numOfVisits = e.getElement("http://schemas.google.com/analytics/2009", "metric").getAttribute("value").getValue(); var updateDate = e.getElement("updated").getText(); var link = app.createAnchor(title + "(" + numOfVisits + ")", link); applyCSS(link, _links); vPanel.add(link); } return app; }function applyCSS(element, style){ for (var key in style){ element.setStyleAttribute(key, style[key]); } } |