Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Google Analytics Management API
/*globals Analytics, Browser, SpreadsheetApp */
var analyticsManagement = Analytics.Management;
var accounts = analyticsManagement.Accounts;
var properties = analyticsManagement.Webproperties;
var profiles = analyticsManagement.Profiles;
var filters = analyticsManagement.Filters;
var viewFilters = analyticsManagement.ProfileFilterLinks;
var goals = analyticsManagement.Goals;
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var herokuAccounts = [{
name: 'Heroku - Universal Analytics',
id: 39697570
}, {
name: 'heroku',
id: 2989055
}];
function onOpen() {
var items = [{
name: 'Enter Account Id', functionName: 'launchMsgBox'
}];
//var items = getAccounts();
sheet.addMenu('Google Analytics Audit', items);
}
function getAccounts() {
var accountsList = accounts.list().getItems();
return accountsList.map(function(account) {
var singleObj = {};
singleObj['name'] = account.name;
singleObj['functionName'] = 'runAll';
singleObj['id'] = account.id;
return singleObj;
});
}
function getAccountNames(accountIdArray) {
var accountsList = accounts.list().getItems();
var blah = [];
accountsList.forEach(function(accountListAccount) {
if (accountIdArray.indexOf(accountListAccount.id) != -1) {
blah.push(accountListAccount.name);
}
});
Logger.log(blah);
return blah;
}
function launchMsgBox() {
var account = Browser.inputBox('Google Analytics Audit', 'Enter in an Account ID:', Browser.Buttons.OK_CANCEL);
var accounts = account.split(',');
Browser.msgBox(accounts);
var accountNames = getAccountNames(accounts);
Browser.msgBox(accountNames);
//var foo = [];
// https://sites.google.com/site/scriptsexamples/custom-methods/underscoregs
// accounts.forEach(function(acc) {
// foo.push(getAccountName(acc));
// });
//var accountName = getAccountName(account);
//Browser.msgBox(foo);
}
function runAll() {
var accountId = herokuAccounts;
runFilters(accountId);
runGoals(accountId);
runProfiles(accountId);
runViewFilters(accountId);
}
function runFilters(id) {
try {
var filters = _getFilterData(id);
_outputToSpreadsheet(filters);
} catch (error) {
Browser.msgBox(error.message);
}
}
function runGoals(id) {
try {
var goals = _getGoalData(id);
_outputToSpreadsheet(goals);
} catch (error) {
Browser.msgBox(error.message);
}
}
function runProfiles(id) {
try {
var profiles = _getProfileData(id);
_outputToSpreadsheet(profiles);
} catch (error) {
Browser.msgBox(error.message);
}
}
function runViewFilters(id) {
try {
var viewFilters = _getViewFilterData(id);
_outputToSpreadsheet(viewFilters);
} catch (error) {
Browser.msgBox(error.message);
}
}
function _getViewFilterData(inAccountsList) {
var accountsList = Array.isArray(inAccountsList) ? inAccountsList : accounts.list().getItems();
var account = {};
var propertiesList = '';
var property = {};
var profilesList = '';
var profile = {};
var filtersList = '';
var filter = {};
var i, j, k, l;
var results = {};
results.data = [];
results.sheetName = 'viewFilters';
results.header = [
'account.name',
'account.id',
'property.name',
'property.id',
'profile.name',
'profile.id',
'filter.name',
'filter.id'
];
for (i = 0; i < accountsList.length; i++) {
account.name = accountsList[i].name;
account.id = accountsList[i].id;
propertiesList = properties.list(account.id).getItems();
for (j = 0; j < propertiesList.length; j++) {
property.name = propertiesList[j].name;
property.id = propertiesList[j].id;
profilesList = profiles.list(account.id, property.id).getItems();
for (k = 0; k < profilesList.length; k++) {
profile.name = profilesList[k].name;
profile.id = profilesList[k].id;
filtersList = viewFilters.list(account.id, property.id, profile.id).getItems();
for (l = 0; l < filtersList.length; l++) {
filter.name = filtersList[l].filterRef.name;
filter.id = filtersList[l].filterRef.id;
results.data.push([
account.name,
account.id,
property.name,
property.id,
profile.name,
profile.id,
filter.name,
filter.id
]);
}
}
}
}
return results;
}
function _getFilterData(inAccountsList) {
var accountsList = Array.isArray(inAccountsList) ? inAccountsList : accounts.list().getItems();
var account = {};
var filtersList = '';
var filter = {};
var i, j;
var results = {};
results.data = [];
results.sheetName = 'filters';
results.header = [
'account.name',
'account.id',
'filter.name',
'filter.id',
'filter.type',
'filter.field',
'filter.matchType',
'filter.expressionValue',
'filter.caseSensitive',
'filter.searchString',
'filter.replaceString',
'filter.fieldA',
'filter.extractA',
'filter.fieldB',
'filter.extractB',
'filter.outputToField',
'filter.outputConstructor',
'filter.fieldARequired',
'filter.fieldBRequired',
'filter.overrideOutputField',
'filter.caseSensitive'
];
var details = '';
for (i = 0; i < accountsList.length; i++) {
account.name = accountsList[i].name;
account.id = accountsList[i].id;
filtersList = filters.list(account.id).getItems();
for (j = 0; j < filtersList.length; j++) {
filter.type = filtersList[j].type;
filter.name = filtersList[j].name;
filter.id = filtersList[j].id;
if (filter.type === 'EXCLUDE' || filter.type === 'INCLUDE') {
details = filtersList[j].getIncludeDetails() || filtersList[j].getExcludeDetails();
filter.field = details.field;
filter.matchType = details.matchType;
filter.expressionValue = details.expressionValue;
filter.caseSensitive = details.caseSensitive;
results.data.push([
account.name,
account.id,
filter.name,
filter.id,
filter.type,
filter.field,
filter.matchType,
filter.expressionValue,
filter.caseSensitive,
'-',
'-',
'-',
'-',
'-',
'-',
'-',
'-',
'-',
'-',
'-',
'-'
]);
}
if (filter.type === 'UPPERCASE' || filter.type === 'LOWERCASE') {
details = filtersList[j].uppercaseDetails || filtersList[j].lowercaseDetails;
filter.field = details.field;
results.data.push([
account.name,
account.id,
filter.name,
filter.id,
filter.type,
filter.field,
'-',
'-',
'-',
'-',
'-',
'-',
'-',
'-',
'-',
'-',
'-',
'-',
'-',
'-',
'-'
]);
}
if (filter.type === 'SEARCH_AND_REPLACE') {
details = filtersList[j].searchAndReplaceDetails;
filter.field = details.field;
filter.searchString = details.searchString;
filter.replaceString = details.replaceString;
filter.caseSensitive = details.caseSensitive;
results.data.push([
account.name,
account.id,
filter.name,
filter.id,
filter.type,
filter.field,
'-',
'-',
filter.caseSensitive,
filter.searchString,
filter.replaceString,
'-',
'-',
'-',
'-',
'-',
'-',
'-',
'-',
'-',
'-'
]);
}
if (filter.type === 'ADVANCED') {
details = filtersList[j].advancedDetails;
filter.fieldA = details.fieldA;
filter.extractA = details.extractA;
filter.fieldB = details.fieldB;
filter.extractB = details.extractB;
filter.outputToField = details.outputToField;
filter.outputConstructor = details.outputConstructor;
filter.fieldARequired = details.fieldARequired;
filter.fieldBRequired = details.fieldBRequired;
filter.overrideOutputField = details.overrideOutputField;
filter.caseSensitive = details.caseSensitive;
results.data.push([
account.name,
account.id,
filter.name,
filter.id,
filter.type,
filter.field,
'-',
'-',
'-',
'-',
'-',
filter.fieldA,
filter.extractA,
filter.fieldB,
filter.extractB,
filter.outputToField,
filter.outputConstructor,
filter.fieldARequired,
filter.fieldBRequired,
filter.overrideOutputField,
filter.caseSensitive
]);
}
}
}
return results;
}
function _getGoalData(inAccountsList) {
var accountsList = Array.isArray(inAccountsList) ? inAccountsList : accounts.list().getItems();
var account = {};
var propertiesList = '';
var property = {};
var profilesList = '';
var profile = {};
var goalsList = '';
var goal = {};
var steps = '';
var details = '';
var conditions = '';
var condition = {};
condition.detail = [];
goal.detail = {};
goal.detail.step = [];
var i, j, k, l, m, n;
var results = {};
results.data = [];
results.sheetName = 'goals';
results.header = [
'account.name',
'account.id',
'property.name',
'property.id',
'profile.name',
'profile.id',
'goal.name',
'goal.id',
'goal.type',
'goal.active',
'goal.value',
'goal.detail.url',
'goal.detail.caseSensitive',
'goal.detail.matchType',
'goal.detail.firstStepRequired',
'goal.detail.step.1',
'goal.detail.step.2',
'goal.detail.step.3',
'goal.detail.step.4',
'goal.detail.step.5',
'goal.detail.step.6',
'goal.detail.step.7',
'goal.detail.step.8',
'goal.detail.step.9',
'goal.detail.step.10',
'goal.detail.comparisonType',
'goal.detail.comparisonValue',
'goal.event.condition.type',
'goal.event.condition.matchType',
'goal.event.condition.expression',
'goal.event.condition.type',
'goal.event.condition.matchType',
'goal.event.condition.expression',
'goal.event.condition.type',
'goal.event.condition.matchType',
'goal.event.condition.expression',
'goal.event.condition.type',
'goal.event.condition.comparisonType',
'goal.event.condition.comparisonValue'
];
for (i = 0; account = accountsList[i]; i++) {
account.name = accountsList[i].name;
account.id = accountsList[i].id;
propertiesList = properties.list(account.id).getItems();
for (j = 0; j < propertiesList.length; j++) {
property.name = propertiesList[j].name;
property.id = propertiesList[j].id;
profilesList = profiles.list(account.id, property.id).getItems();
for (k = 0; k < profilesList.length; k++) {
profile.name = profilesList[k].name;
profile.id = profilesList[k].id;
goalsList = goals.list(account.id, property.id, profile.id).getItems();
for (l = 0; l < goalsList.length; l++) {
goal.name = goalsList[l].name;
goal.id = goalsList[l].id;
goal.value = goalsList[l].value;
goal.active = goalsList[l].active;
goal.type = goalsList[l].type;
goal.created = goalsList[l].created;
goal.updated = goalsList[l].updated;
if (goalsList[l].urlDestinationDetails) {
details = goalsList[l].urlDestinationDetails;
goal.detail.url = details.url;
goal.detail.caseSensitive = details.caseSensitive;
goal.detail.matchType = details.matchType;
goal.detail.firstStepRequired = details.firstStepRequired;
// Iterate through the steps.
steps = details.steps;
if (steps) {
for (m = 0; m < steps.length; m++) {
goal.detail.step.push(steps[m].url);
}
}
results.data.push([
account.name,
account.id,
property.name,
property.id,
profile.name,
profile.id,
goal.name,
goal.id,
goal.type,
goal.active,
goal.value,
goal.detail.url,
goal.detail.caseSensitive,
goal.detail.matchType,
goal.detail.firstStepRequired,
goal.detail.step[0],
goal.detail.step[1],
goal.detail.step[2],
goal.detail.step[3],
goal.detail.step[4],
goal.detail.step[5],
goal.detail.step[6],
goal.detail.step[7],
goal.detail.step[8],
goal.detail.step[9],
'-',
'-',
'-',
'-',
'-',
'-',
'-',
'-',
'-',
'-',
'-',
'-',
'-',
'-'
]);
goal.detail.step = [];
} else if (goalsList[l].visitTimeOnSiteDetails || goalsList[l].visitNumPagesDetails) {
details = goalsList[l].visitTimeOnSiteDetails || goalsList[l].visitNumPagesDetails;
goal.detail.comparisonType = details.comparisonType;
goal.detail.comparisonValue = details.comparisonValue;
results.data.push([
account.name,
account.id,
property.name,
property.id,
profile.name,
profile.id,
goal.name,
goal.id,
goal.type,
goal.active,
goal.value,
'-',
'-',
'-',
'-',
'-',
'-',
'-',
'-',
'-',
'-',
'-',
'-',
'-',
'-',
goal.detail.comparisonType,
goal.detail.comparisonValue,
'-',
'-',
'-',
'-',
'-',
'-',
'-',
'-',
'-',
'-',
'-',
'-'
]);
} else if (goalsList[l].eventDetails) {
details = goalsList[l].eventDetails;
conditions = details.eventConditions;
if (conditions) {
for (n = 0; n < conditions.length; n++) {
condition.type = conditions[n].type;
if (condition.type === 'VALUE') {
condition.comparisonType = conditions[n].comparisonType;
condition.comparisonValue = conditions[n].comparisonValue;
} else {
condition.matchType = conditions[n].matchType;
condition.expression = conditions[n].expression;
}
condition.detail.push(condition.type, condition.matchType, condition.expression, condition.comparisonType, condition.comparisonValue);
}
}
results.data.push([
account.name,
account.id,
property.name,
property.id,
profile.name,
profile.id,
goal.name,
goal.id,
goal.type,
goal.active,
goal.value,
'-',
'-',
'-',
'-',
'-',
'-',
'-',
'-',
'-',
'-',
'-',
'-',
'-',
'-',
'-',
'-',
condition.detail[0],
condition.detail[1],
condition.detail[2],
condition.detail[5],
condition.detail[6],
condition.detail[7],
condition.detail[10],
condition.detail[11],
condition.detail[12],
condition.detail[15],
condition.detail[18],
condition.detail[19]
]);
condition.detail = [];
}
}
}
}
}
return results;
}
function _getProfileData(inAccountsList) {
var accountsList = Array.isArray(inAccountsList) ? inAccountsList : accounts.list().getItems();
var account = {};
var propertiesList = '';
var property = {};
var profilesList = '';
var profile = {};
var i, j, k;
var results = {};
results.data = [];
results.sheetName = 'profiles';
results.header = [
'account.name',
'account.id',
'property.name',
'property.id',
'profile.name',
'profile.id',
'goalset.1',
'goalset.2',
'goalset.3',
'goalset.4',
'filters',
'profile.websiteUrl',
'profile.timezone',
'profile.defaultPage',
'profile.excludeQueryParameters',
'profile.currency',
'profile.botFilter (NOTHING)',
'profile.adwords (NOTHING)',
'profile.siteSearchQueryParameters',
'profile.stripSiteSearchQueryParameters',
'profile.siteSearchCategoryParameters',
'profile.stripSiteSearchCategoryParameters'
];
for (i = 0; i < accountsList.length; i++) {
account.name = accountsList[i].name;
account.id = accountsList[i].id;
propertiesList = properties.list(account.id).getItems();
for (j = 0; j < propertiesList.length; j++) {
property.name = propertiesList[j].name;
property.id = propertiesList[j].id;
profilesList = profiles.list(account.id, property.id).getItems();
for (k = 0; k < profilesList.length; k++) {
profile.name = profilesList[k].name;
profile.id = profilesList[k].id;
profile.websiteUrl = profilesList[k].websiteUrl;
profile.timezone = profilesList[k].timezone;
profile.defaultPage = profilesList[k].defaultPage;
profile.excludeQueryParameters = profilesList[k].excludeQueryParameters;
profile.currency = profilesList[k].currency;
profile.siteSearchQueryParameters = profilesList[k].siteSearchQueryParameters;
profile.stripSiteSearchQueryParameters = profilesList[k].stripSiteSearchQueryParameters;
profile.siteSearchCategoryParameters = profilesList[k].siteSearchCategoryParameters;
profile.stripSiteSearchCategoryParameters = profilesList[k].stripSiteSearchCategoryParameters;
results.data.push([
account.name,
account.id,
property.name,
property.id,
profile.name,
profile.id,
'{ADD MANUALLY}',
'{ADD MANUALLY}',
'{ADD MANUALLY}',
'{ADD MANUALLY}',
'{ADD MANUALLY}',
profile.websiteUrl,
profile.timezone,
profile.defaultPage,
profile.excludeQueryParameters,
profile.currency,
'{ADD MANUALLY}',
'{ADD MANUALLY}',
profile.siteSearchQueryParameters,
profile.stripSiteSearchQueryParameters,
profile.siteSearchCategoryParameters,
profile.stripSiteSearchCategoryParameters
]);
}
}
}
return results;
}
function _outputToSpreadsheet(results) {
var sheet = _createSheetIfNotExists(results.sheetName);
// Clear the rows of data - not the headers.
sheet.getRange(2, 1, sheet.getMaxRows(), sheet.getMaxColumns())
.clearContent();
// Print the headers.
sheet.getRange(1, 1, 1, results.header.length)
.setValues([results.header]);
// Print the rows of data.
sheet.getRange(2, 1, results.data.length, results.header.length)
.setValues(results.data);
}
function _createSheetIfNotExists(sheet) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
return ss.getSheetByName(sheet) || ss.insertSheet(sheet);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.