Google Analytics Reports (PV ranking)

Creates a ranking report (TSV) for the number of page views. It is also possible to aggregate it with a specified duration and filter criteria.

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 (click to open)

// Google Analytics reports via Analytics Reporting API v4 (ver. 20210312)
// (c) 2021, Questetra, Inc. (the MIT License)

//// == 工程コンフィグの参照 / Config Retrieving ==
const oauth2 = configs.get( "conf_OAuth2" );
const viewId = configs.get( "conf_ViewId" );
const numOfRanking = Number(configs.get( "conf_NumOfRanking" ));
const rankingFilter = configs.get( "conf_RankingFilter" );

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

//// == ワークフローデータの参照 / Data Retrieving ==
const startDate = String(engine.findDataByNumber(dataIdC)); // (YYYY-MM-DD)
const endDate = String(engine.findDataByNumber(dataIdD)); // (YYYY-MM-DD)

//// == 演算 / Calculating ==
const 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:hostname";
requestObj.reportRequests[0].dimensions[1] = {};
requestObj.reportRequests[0].dimensions[1].name = "ga:pagePath";
requestObj.reportRequests[0].dimensions[2] = {};
requestObj.reportRequests[0].dimensions[2].name = "ga:pageTitle";
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;

const token = httpClient.getOAuth2Token( oauth2 );
const uri = "https://analyticsreporting.googleapis.com/v4/reports:batchGet";
const response = httpClient.begin()
.bearer( token )
.body( JSON.stringify( requestObj ), "application/json" )
.post( uri );
const statusCode = response.getStatusCode() + "";

let accessLog = "";
accessLog += "---POST request--- " + statusCode + "\n";
accessLog += response.getResponseAsString() + "\n";

let rankingStr = "";
if( statusCode == "200" ){
const 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 += reportObj.reports[0].columnHeader.dimensions[1] + "\t";
rankingStr += reportObj.reports[0].columnHeader.dimensions[2];
rankingStr += "\n";

for( let 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 += reportObj.reports[0].data.rows[i].dimensions[1] + "\t";
rankingStr += reportObj.reports[0].data.rows[i].dimensions[2];
rankingStr += "\n";
}
}

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

Download

(C) Questetra, Inc. (MIT License)
https://support.questetra.com/addons/googleanalytics-reportspv/
The Addon-import feature is available with Professional or Enterprise edition.

Notes

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

Capture

See also

%d bloggers like this: