The Analytics service allows you to use the Google Analytics Management API and Reporting APIs in Apps Script. These APIs gives Analytics users the ability to get information about the structure of their account and run reports on how it is performing.
Reference
For detailed information on this service, see the reference documentation for the various Google Analytics APIs:
- Core Reporting API
- Management API
- Metadata API
- Multi-Channel Funnels Reporting API
- Real Time Reporting API
Like all advanced services in Apps Script, the Analytics service uses the same objects, methods, and parameters as the public API.
Sample code
The sample code below demonstrates how to use a few features of the Analytics service.
List account structure
The sample code below, which uses version 3 of the Management API, lists all of the Google Analytics accounts, web proprties, and profiles that the current user can access.
function listAccounts() {
var accounts = Analytics.Management.Accounts.list();
if (accounts.items && accounts.items.length) {
for (var i = 0; i < accounts.items.length; i++) {
var account = accounts.items[i];
Logger.log('Account: name "%s", id "%s".', account.name, account.id);
// List web properties in the account.
listWebProperties(account.id);
}
} else {
Logger.log('No accounts found.');
}
}
function listWebProperties(accountId) {
var webProperties = Analytics.Management.Webproperties.list(accountId);
if (webProperties.items && webProperties.items.length) {
for (var i = 0; i < webProperties.items.length; i++) {
var webProperty = webProperties.items[i];
Logger.log('\tWeb Property: name "%s", id "%s".', webProperty.name,
webProperty.id);
// List profiles in the web property.
listProfiles(accountId, webProperty.id);
}
} else {
Logger.log('\tNo web properties found.');
}
}
function listProfiles(accountId, webPropertyId) {
var profiles = Analytics.Management.Profiles.list(accountId,
webPropertyId);
if (profiles.items && profiles.items.length) {
for (var i = 0; i < profiles.items.length; i++) {
var profile = profiles.items[i];
Logger.log('\t\tProfile: name "%s", id "%s".', profile.name,
profile.id);
}
} else {
Logger.log('\t\tNo web properties found.');
}
}
Run a report
The sample code below, which uses version 3 of the Core Reporting API, runs a report to retrieve the top 25 keywords and traffic sources and stores the results in a new spreadsheet.
function runReport(profileId) {
var today = new Date();
var oneWeekAgo = new Date(today.getTime() - 7 * 24 * 60 * 60 * 1000);
var startDate = Utilities.formatDate(oneWeekAgo, Session.getTimeZone(),
'yyyy-MM-dd');
var endDate = Utilities.formatDate(today, Session.getTimeZone(),
'yyyy-MM-dd');
var tableId = 'ga:' + profileId;
var metric = 'ga:visits';
var options = {
'dimensions': 'ga:source,ga:keyword',
'sort': '-ga:visits,ga:source',
'filters': 'ga:medium==organic',
'max-results': 25
};
var report = Analytics.Data.Ga.get(tableId, startDate, endDate, metric,
options);
if (report.rows) {
var spreadsheet = SpreadsheetApp.create('Google Analytics Report');
var sheet = spreadsheet.getActiveSheet();
// Append the headers.
var headers = report.columnHeaders.map(function(columnHeader) {
return columnHeader.name;
});
sheet.appendRow(headers);
// Append the results.
sheet.getRange(2, 1, report.rows.length, headers.length)
.setValues(report.rows);
Logger.log('Report spreadsheet created: %s',
spreadsheet.getUrl());
} else {
Logger.log('No rows returned.');
}
}