Google Analytics Report

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.

    A. Set OAuth2 Config Name (at [OAuth 2.0 Setting])Single-line inputRequired
    B. Set ViewID of Group AnalyticsSingle-line inputRequired
    C. Select DATE DATA for Log Start DateData selectDateRequired
    D. Select DATE DATA for Log End DateData selectDateRequired
    E. Set Nuber of Ranking List (e.g. 20)Single-line inputRequired
    F. Set Ranking Filter for List (e.g. ga:pagePath=~/2016/|/2017/)Single-line input
    G. Select STRING DATA for Ranking List (update)Data selectString(multiple line)
    X. Select STRING DATA for Access Log (update)Data selectString(multiple line)
    -variable namevariable label
    Aconf_OAuth2Set OAuth2 Config Name (at [OAuth 2.0 Setting])
    Bconf_ViewIdSet ViewID of Group Analytics
    Cconf_DataIdCSelect DATE DATA for Log Start Date
    Dconf_DataIdDSelect DATE DATA for Log End Date
    Econf_NumOfRankingSet Nuber of Ranking List (e.g. 20)
    Fconf_RankingFilterSet Ranking Filter for List (e.g. ga:pagePath=~/2016/|/2017/)
    Gconf_DataIdGSelect STRING DATA for Ranking List (update)
    Xconf_DataIdXSelect STRING DATA for Access Log (update)
    // Google Analytics reports via Analytics Reporting API v4 (ver. 20170724)
    // (c) 2017, Questetra, Inc. (the MIT License)
    
    //// == 工程コンフィグの参照 / 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: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;
    
    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 += reportObj.reports[0].columnHeader.dimensions[1] + "\t";
      rankingStr += reportObj.reports[0].columnHeader.dimensions[2];
      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 += reportObj.reports[0].data.rows[i].dimensions[1] + "\t";
        rankingStr += reportObj.reports[0].data.rows[i].dimensions[2];
        rankingStr += "\n";
      }
    }
    
    //// == ワークフローデータへの代入 / Data Updating ==
    retVal.put( dataIdG, rankingStr );
    if( dataIdX !== "" ){
      retVal.put( dataIdX, accessLog );
    }
    

    • B’: View ID of Google Analytics entered in the Data Item selected at config B
    • C’: Start date of access log stored in the Data Item selected at config C
    • D’: End date of access log stored in the Data Item selected at config D
    • E’: Number of items of Ranking entered in the Data Item selected at config E
    • F’: Ranking-filter entered in the Data Item selected at config F
    • Retrieved Ranking list is stored in the data item selected in config G
    • The communication logs are stored in the data item selected in config X
    • You need to Enable [Google Analytics Reporting API] at Google Developers Console beforehand and obtain “Client ID” and “Client secret”