我能够获取代码示例,将Google Analytics中的数据导入Google电子表格(https://developers.google.com/analytics/solutions/articles/reporting-apps-script)但是,我知道我想要的个人资料的ID,我不需要通过获得“第一个档案”的开销。当我尝试对这样的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');
}
}
答案 0 :(得分:0)
个人资料ID必须以ga:。
作为前缀所以在你的情况下:
var profileId = '12345678'; should be:
var profileId = 'ga:12345678';
https://developers.google.com/analytics/devguides/reporting/core/v3/reference#ids