Google Analytics
Stay organized with collections
Save and categorize content based on your preferences.
Get an Analytics account
function getAccountByName() {
var accountName = 'INSERT_ACCOUNT_NAME_HERE';
var accounts = Analytics.Management.Accounts.list();
for (var i = 0; i < accounts.items.length; i++) {
if (accountName == accounts.items[i].name) {
console.log('Account ID: %s, Name = %s', accounts.items[i].id,
accounts.items[i].name);
}
}
return;
}
Get a web property
function getWebPropertyById() {
var accountId = 'INSERT_ACCOUNT_ID_HERE';
var webPropertyId = 'INSERT_WEB_PROPERTY_ID_HERE';
var webProperty = Analytics.Management.Webproperties.get(
accountId, webPropertyId);
console.log('Web Property ID: %s, Name: %s', webProperty.id, webProperty.name);
}
List all profiles
function listAllProfiles() {
var accountId = 'INSERT_ACCOUNT_ID_HERE';
var webPropertyId = 'INSERT_WEB_PROPERTY_ID_HERE';
var profiles = Analytics.Management.Profiles.list(accountId, webPropertyId);
for (var i = 0; i < profiles.items.length; i++) {
console.log('Profile ID: %s, Name: %s', profiles.items[i].id,
profiles.items[i].name);
}
}
Get stats for an Analytics profile
function getStatsForProfileId() {
var profileId = 'INSERT_PROFILE_ID_HERE';
// Dates should be in yyyy-mm-dd format.
var startDate = 'INSERT_START_DATE_HERE';
var endDate = 'INSERT_END_DATE_HERE';
var results = Analytics.Data.Ga.get('ga:' + profileId, startDate,
endDate, 'ga:sessions');
console.log('Profile Name: %s', results.profileInfo.profileName);
console.log('Total Sessions: %s', results.rows[0][0]);
}
Run a multi-channel funnel report
function runMultiChannelFunnelReport() {
// See https://support.google.com/analytics/answer/1191180 to learn more about
// multi-channel funnel reports in Google Analytics.
var profileId = 'INSERT_PROFILE_ID_HERE';
// Dates should be in yyyy-mm-dd format.
var startDate = 'INSERT_START_DATE_HERE';
var endDate = 'INSERT_END_DATE_HERE';
var results = Analytics.Data.Mcf.get(
'ga:' + profileId,
startDate,
endDate,
'mcf:totalConversions', // List of all metrics to retrieve.
{
'dimensions': 'mcf:sourcePath',
'sort': '-mcf:totalConversions',
'max-results': 25
}
);
var headers = [];
for (var i = 0; i < results.columnHeaders.length; i++) {
headers.push(results.columnHeaders[i].name);
}
console.log(headers.join(','));
for (var i = 0; i < results.rows.length; i++) {
var rowData = [];
var row = results.rows[i];
for (var j = 0; j < row.length; j++) {
var cell = row[j];
var dataType = results.columnHeaders[j].dataType;
if (dataType == 'MCF_SEQUENCE') {
rowData.push(getStringFromMcfSequence(cell.conversionPathValue));
} else {
rowData.push(cell.primitiveValue);
}
}
console.log(rowData.join(','));
}
}
Filter stats for an Analytics profile
function filterStats() {
var profileId = 'INSERT_PROFILE_ID_HERE';
var results = Analytics.Data.Ga.get(
'ga:' + profileId,
'2014-01-01', // Start date in yyyy-mm-dd format.
'2014-01-15', // End date in yyyy-mm-dd format.
'ga:sessions', // List of all metrics to retrieve.
{
// Filter for Firefox browser users in the USA. See
// https://developers.google.com/analytics/devguides/reporting/core/v3/reference#filters
// for filter syntax, and
// https://developers.google.com/analytics/devguides/reporting/core/dimsmets
// for the list of supported Dimensions and Metrics.
'filters': 'ga:browser==Firefox;ga:country==United States'
}
);
console.log('View (Profile) Name: %s', results.profileInfo.profileName);
console.log('Total Sessions: %s', results.rows[0][0]);
}
Run real-time Analytics report
function runRealTimeReport() {
// See https://support.google.com/analytics/answer/1638635 to learn more about
// real-time reporting.
var profileId = 'INSERT_PROFILE_ID_HERE';
var results = Analytics.Data.Realtime.get(
'ga:' + profileId,
'rt:activeUsers',
{
'dimensions': 'rt:medium'
}
);
var headers = [];
for (var i = 0; i < results.columnHeaders.length; i++) {
headers.push(results.columnHeaders[i].name);
}
console.log(headers.join(','));
for (var i = 0; i < results.rows.length; i++) {
var rowData = [];
var row = results.rows[i];
for (var j = 0; j < row.length; j++) {
var cell = row[j];
rowData.push(cell);
}
console.log(rowData.join(','));
}
}
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2022-03-14 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2022-03-14 UTC."],[[["This webpage provides code samples demonstrating how to interact with Google Analytics accounts programmatically."],["The code snippets cover functionalities such as retrieving account information, web property details, and user profile data."],["You can use these code examples to access and analyze various Google Analytics reports, including real-time data, multi-channel funnels, and filtered stats."],["The provided code is written in JavaScript and leverages the Google Analytics API."],["By using these examples, developers can automate tasks, build custom dashboards, and integrate Google Analytics data into other applications."]]],[]]