gpt4 book ai didi

google-apps-script - 如何使用Google Apps脚本从Google Analytics Management API请求非抽样报告?

转载 作者:行者123 更新时间:2023-12-03 15:50:00 26 4
gpt4 key购买 nike

我做了什么:


我有Google Analytics Premium
我已按照以下说明为OAuth2授权用于Apps脚本:https://github.com/googlesamples/apps-script-oauth2
我已在高级Google服务和开发者控制台上启用了Google Analytics API和Drive API。
我正在尝试按照以下说明请求非抽样报告:https://developers.google.com/analytics/devguides/config/mgmt/v3/mgmtReference/management/unsampledReports/insert#request


我的问题:

我已在Google Apps脚本编辑器上编写了以下代码,以请求Google Analytics(分析)API的非抽样报告。据我所知,如果它能正常工作,应该会触发Google Analytics(分析)界面上的未采样请求。但是,我在界面的“待处理”或“已完成”部分中看不到它。当我运行代码时,什么也没发生。我什至没有看到任何错误。您能帮我做错什么吗?先感谢您。

这是我的代码:



function insertView()
var request = gapi.client.analytics.management.unsampledReports.insert(
{
'accountId': 'XXXXXX',
'webPropertyId': 'UA-XXXXXX-XX',
'profileId': 'XXXXXXXX',
'resource': {
'title': 'A test Report',
'start-date': '2016-03-31',
'end-date': '2016-04-04',
'metrics': 'ga:itemRevenue',
'dimensions': 'ga:date'

}
});
return request;
}
}

function outputToSpreadsheetNext(request) {

var sheetId = '1RSkx8n-YRMq7Cnco-mvC83bJPKSnsb3QPx3BItAWmN8';
var sheetPrevious= SpreadsheetApp.openById(sheetId).getSheets()[0];

var headerNamesPrevious = []
for (var i = 0, header; header = request.getColumnHeaders()[i]; ++i) {
headerNamesPrevious.push(header.getName());
}

sheetPrevious.getRange(1, 1, 1, headerNamesPrevious.length)
.setValues([headerNamesPrevious]);

// Print the rows of data.
sheetPrevious.getRange(2, 1,request.getRows().length,headerNamesPrevious.length)
.setValues(request.getRows());

}
}





我已经在此处撰写了有关如何操作的说明: http://sophearychiv.com/how-to-pull-and-automate-unsampled-reports-from-google-analytics-into-google-spreadsheet/

最佳答案

这是您可能想尝试的工作版本。

使用说明


创建一个新的Google Spreadsheet。
将以下内容复制到新脚本中
转到资源>高级Google服务
启用Google Analytics(分析)API,将其切换为开启
单击高级Google服务对话框中的Google Developer Console链接
在Cloud API Manager中找到并启用Analytics API


现在,您可以运行函数insertReport(),这将使用API​​插入未抽样报告。请记住,就像我在the previous question中告诉您的那样,这些过程可能需要几个小时才能完成。

稍后运行updateAllReports()函数,它应该尝试获取报告的更新状态。

另外,如果状态为完成,则将为您提供指向Google云端硬盘上文件的链接,并将数据从CSV导入到第二张工作表中。

var LOG_SHEET_NAME = 'Unsampled Report Logs';
var ss = SpreadsheetApp.getActive();
var ui = SpreadsheetApp.getUi();

function insertReport() {
var resource = {
'title': 'A test Report',
'start-date': '2016-03-31',
'end-date': '2016-04-04',
'metrics': 'ga:itemRevenue',
'dimensions': 'ga:date'

};
var accountId = 'XXXXXXXX';
var webPropertyId = 'UA-XXXXXXXX-1';
var profileId = 'YYYYYYYY';

try {
var request = Analytics.Management.UnsampledReports.insert(resource, accountId, webPropertyId, profileId);
} catch (error) {
ui.alert('Error Performing Unsampled Report Query', error.message, ui.ButtonSet.OK);
return;
}

var sheet = ss.getSheetByName(LOG_SHEET_NAME);

if (!sheet) {
sheet = ss.insertSheet(LOG_SHEET_NAME);
sheet.appendRow(['User', 'Account', 'Web Property', 'View', 'Title', 'Inserted Time', 'Updated Time', 'Status', 'Id', 'File']);
sheet.getRange(1, 1, 1, 10).setFontWeight('bold');
}
sheet.appendRow([
Session.getEffectiveUser().getEmail(),
request.accountId,
request.webPropertyId,
request.profileId,
request.title,
request.created,
request.updated,
request.status,
request.id
]);

}

// Scans LOG_SHEET_NAME and tries to update any report that is PENDING
function updateAllReports() {
var sheet = ss.getSheetByName(LOG_SHEET_NAME);
var lastRow = sheet.getLastRow();

var dataRange = sheet.getRange(2,1, lastRow, 10);

var data = dataRange.getValues();

for (var i=0; i<data.length; i++) {
// If data is PENDING let's try to update it's status. Hopefully it's complete now
// but it may take up to 24h to process an Unsampled Reprot
if (data[i][0] == Session.getEffectiveUser().getEmail() && data[i][7] == 'PENDING') {
try {
var request = Analytics.Management.UnsampledReports.get(data[i][1], data[i][2], data[i][3], data[i][8]);
} catch (error) {
ui.alert('Error Performing Unsampled Report Query', error.message, ui.ButtonSet.OK);
return;
}

data[i] = [
Session.getEffectiveUser().getEmail(),
request.accountId,
request.webPropertyId,
request.profileId,
request.title,
request.created,
request.updated,
request.status,
request.id,
request.status == 'COMPLETED' ? DriveApp.getFileById(request.driveDownloadDetails.documentId).getUrl() : ''
];


// If data is Complete let's import it into a new sheet
if (request.status == 'COMPLETED') {
importReportFromDrive(request.title, request.driveDownloadDetails.documentId);
}
}
}

// Write only once to the spreadsheet this is faster
dataRange.setValues(data);

}

function importReportFromDrive(title, fileId) {
var file = DriveApp.getFileById(fileId);
var csvString = file.getBlob().getDataAsString();
var data = Utilities.parseCsv(csvString);

// Find a suitable name for the new sheet
var i=1;
var sheetName = title;
while (ss.getSheetByName(sheetName)) {
sheetName = title + ' ('+ i++ +')';
}

var sheet = ss.insertSheet(sheetName);
var range = sheet.getRange(1, 1, data.length, data[0].length);
range.setValues(data);
}


PS:我为Google Analytics(分析)支持工作,正如“ Zig Mandel”在评论中所说,可以随时与Google Analytics(分析)高级支持联系,我们很乐意提供帮助。我们非常友好。

关于google-apps-script - 如何使用Google Apps脚本从Google Analytics Management API请求非抽样报告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36489572/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com