我能够获取代码示例以将数据从 Google Analytics 提取到 Google 电子表格(https://developers.google.com/analytics/solutions/articles/reporting-apps-script)但是,我知道我想要的配置文件,我不需要通过获取“第一个配置文件”的开销。当我尝试对这样的 ID 进行硬编码时,它无法正常工作:
function getReportDataForProfile() {
var profileId = '12345678';
var tableId = 'ga:12345678';
var startDate = getLastNdays(14); // 2 weeks (a fortnight) ago.
var endDate = getLastNdays(0); // Today.
var optArgs = {
'dimensions': 'ga:keyword', // Comma separated list of dimensions.
'sort': '-ga:visits,ga:keyword', // Sort by visits descending, then keyword.
'segment': 'dynamic::ga:isMobile==Yes', // Process only mobile traffic.
'filters': 'ga:source==google', // Display only google traffic.
'start-index': '1',
'max-results': '250' // Display the first 250 results.
};
// Make a request to the API.
var results = Analytics.Data.Ga.get(
tableId, // Table id (format ga:xxxxxx).
startDate, // Start-date (format yyyy-MM-dd).
endDate, // End-date (format yyyy-MM-dd).
'ga:visits,ga:pageviews', // Comma seperated list of metrics.
optArgs);
if (results.getRows()) {
return results;
} else {
throw new Error('No profiles found');
}
}