javascript - Get total values in google apps script -
javascript - Get total values in google apps script -
i'm trying utilize "totalsforallresults" method in google apps script force out total values each metrics. printed out result of each metrics each dimension spreadsheet. , i'd see total values @ end of row. tried many times cat't work. i'll happy if give me advice.
the problem lastly paragraph. code worked analytics info "totalsforallresults" doesn't work.
var results = channelreportpc(firstprofile); outputtospreadsheet(results); function channelreportpc(firstprofile) { var profileid = firstprofile.getid(); var tableid = 'ga:' + profileid; var startdate = electivestartdate('sd'); var enddate = electiveenddate('ed'); var optargs = { 'dimensions': 'ga:medium', // comma separated list of dimensions. 'sort': '-ga:sessions', 'segment': 'sessions::condition::ga:devicecategory==desktop,ga:devicecategory==tablet', // process desktop or tablet traffic. 'start-index': '1', 'max-results': '20' }; // create request api. var results = analytics.data.ga.get( tableid, // table id (format ga:xxxxxx). startdate, // start-date (format yyyy-mm-dd). enddate, // end-date (format yyyy-mm-dd). 'ga:sessions,ga:percentnewsessions,ga:bouncerate,ga:pageviewspersession,ga:avgsessionduration,ga:transactions,ga:transactionrevenue,ga:transactionspersession', // comma seperated list of metrics. optargs ); var sheet = spreadsheetapp.getactivespreadsheet().insertsheet("media"); } function outputtospreadsheet(results) { sheet = spreadsheetapp.getactivesheet() // print headers. var headernames = []; (var = 0, header; header = results.getcolumnheaders()[i]; ++i) { headernames.push(header.getname()); } sheet.getrange(1, 2, 1, headernames.length) .setvalues([headernames]) .setbackground('#eeeeee'); // print rows of data. sheet.getrange(2, 2, results.getrows().length, headernames.length) .setvalues(results.getrows()); sheet.getrange(getrows().length+1, 2, 1, headernames.length) .setvalues(results.totalsforallresults); }
i believe results.totalforallresults
contains array of values. if is, seek convert array of array. seek follow these steps code,
info = []; data.push(results.totalsforallresults); sheet.getrange(getrows().length+1, 2, 1, headernames.length).setvalues(data);
javascript google-apps-script google-analytics-api
Comments
Post a Comment