将计划的,未抽样的报告从Google Analytics(分析)导入到Google Sheets

时间:2019-03-14 10:55:44

标签: google-apps-script import google-sheets google-analytics google-analytics-api

我在Google Analytics(分析)(360)中有一份计划外的非抽样报告,我试图使用应用程序脚本自动将其导入Google表格。

我可以使用文件ID提取特定的非抽样报告,但是对于计划报告,每个新报告都有唯一的ID。每天是否可以导入计划的非抽样报告的最新版本?

function getUnsampledReport() {

  var details = {
    'accountId': '6204137',
    'webPropertyId': 'UA-6204137-5',
    'profileId': '179885391',
    'unsampledReportId': '9OeClnELSMmq0dhUs3g49Q'
  }

  var request = Analytics.Management.UnsampledReports.get(
    details.accountId,
    details.webPropertyId,
    details.profileId,
    details.unsampledReportId
  );

  var unsampledReportId = request.driveDownloadDetails.documentId;
  var file = DriveApp.getFileById(unsampledReportId);
  var csvData = Utilities.parseCsv(file.getBlob().getDataAsString());
  var sheet = SpreadsheetApp.getActiveSheet();
  sheet.getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData);

}

0 个答案:

没有答案