Google Analytics Reports (Referrer ranking)
Accesses to Google Analytics and get Ranking report (PV / Referrer / Domain). it is possible to specify duration and filter criteria, so you can get regular reports without having to sign in to Google Analytics.
2017 © Questetra, Inc. (MIT License)
Configs
  • A: Set OAuth2 Config Name (at [OAuth 2.0 Setting]) *
  • B: Set ViewID of Group Analytics *
  • C: Select DATE DATA for Log Start Date *
  • D: Select DATE DATA for Log End Date *
  • E: Set Number of Ranking List (e.g. 20) *
  • F: Set Ranking Filter for List (e.g. ga:pagePath=~/2016/|/2017/)
  • G: Select STRING DATA for Ranking List (update)
  • X: Select STRING DATA for Access Log (update)
Script
//// == 工程コンフィグの参照 / Config Retrieving ==
var oauth2  = configs.get( "conf_OAuth2" ) + "";
var viewId  = configs.get( "conf_ViewId" ) + "";
var numOfRanking  = configs.get( "conf_NumOfRanking" ) - 0;
var rankingFilter = configs.get( "conf_RankingFilter" ) + "";

var dataIdC = configs.get( "conf_DataIdC" ); // (returns key)
var dataIdD = configs.get( "conf_DataIdD" ); // (returns key)
var dataIdG = configs.get( "conf_DataIdG" ); // (returns key)
var dataIdX = configs.get( "conf_DataIdX" ); // (returns key)

//// == ワークフローデータの参照 / Data Retrieving ==
var startDate = data.get( dataIdC ) + ""; // (YYYY-MM-DD)
var endDate   = data.get( dataIdD ) + ""; // (YYYY-MM-DD)

//// == 演算 / Calculating ==
var requestObj = {};
  requestObj.reportRequests = [];
  requestObj.reportRequests[0] = {};
  requestObj.reportRequests[0].viewId = viewId;
  requestObj.reportRequests[0].dateRanges = [];
  requestObj.reportRequests[0].dateRanges[0] = {};
  requestObj.reportRequests[0].dateRanges[0].startDate = startDate;
  requestObj.reportRequests[0].dateRanges[0].endDate = endDate;
  requestObj.reportRequests[0].dimensions = [];
  requestObj.reportRequests[0].dimensions[0] = {};
  requestObj.reportRequests[0].dimensions[0].name = "ga:fullReferrer";
  requestObj.reportRequests[0].metrics = [];
  requestObj.reportRequests[0].metrics[0] = {};
  requestObj.reportRequests[0].metrics[0].expression = "ga:pageviews";
  requestObj.reportRequests[0].metrics[1] = {};
  requestObj.reportRequests[0].metrics[1].expression = "ga:sessions";
  if( rankingFilter !== "" ){
    requestObj.reportRequests[0].filtersExpression = rankingFilter;
  }
  requestObj.reportRequests[0].orderBys = [];
  requestObj.reportRequests[0].orderBys[0] = {};
  requestObj.reportRequests[0].orderBys[0].sortOrder = "DESCENDING";
  requestObj.reportRequests[0].orderBys[0].fieldName = "ga:pageviews";
  requestObj.reportRequests[0].orderBys[1] = {};
  requestObj.reportRequests[0].orderBys[1].sortOrder = "DESCENDING";
  requestObj.reportRequests[0].orderBys[1].fieldName = "ga:sessions";
  requestObj.reportRequests[0].pageSize = numOfRanking;

var accessLog = "";
var token = httpClient.getOAuth2Token( oauth2 );
var uri = "https://analyticsreporting.googleapis.com/v4/reports:batchGet";
var response = httpClient.begin()
  .bearer( token )
  .body( JSON.stringify( requestObj ), "application/json" )
  .post( uri );
accessLog += "---POST request--- " + response.getStatusCode() + "\n";
accessLog += response.getResponseAsString() + "\n";

var rankingStr = "";
var statusCode = response.getStatusCode() + "";
if( statusCode == "200" ){
  var reportObj = JSON.parse( response.getResponseAsString() );
  rankingStr += reportObj.reports[0].columnHeader.metricHeader.metricHeaderEntries[0].name + "\t";
  rankingStr += reportObj.reports[0].columnHeader.metricHeader.metricHeaderEntries[1].name + "\t";
  rankingStr += reportObj.reports[0].columnHeader.dimensions[0];
  rankingStr += "\n";

  for( var i = 0; i < reportObj.reports[0].data.rows.length; i++ ){
    rankingStr += reportObj.reports[0].data.rows[i].metrics[0].values[0] + "\t";
    rankingStr += reportObj.reports[0].data.rows[i].metrics[0].values[1] + "\t";
    rankingStr += reportObj.reports[0].data.rows[i].dimensions[0];
    rankingStr += "\n";
  }
}

//// == ワークフローデータへの代入 / Data Updating ==
retVal.put( dataIdG, rankingStr );
if( dataIdX !== "" ){
  retVal.put( dataIdX, accessLog );
}

Download

Capture

Notes

  • You need to Enable [Google Analytics Reporting API] in the Google Developers Console beforehand and obtain the Client ID and Client secret

See also

%d bloggers like this: