Reputation: 194
I am using a Google App Script example importing data into a Google Spreadsheet App Script on this page. The example works fine, but I want to change the web property it's pulling data.
I've looked around for examples and tried various iterations of their sample code, but I'm not getting it correct. Can someone help me with this code on how to change it to pull a different property within my profile?
function runDemo() {
try {
var firstProfile = getFirstProfile();
var results = getReportDataForProfile(firstProfile);
outputToSpreadsheet(results);
} catch (error) {
Browser.msgBox(error.message);
}
}
function getFirstProfile() {
var accounts = Analytics.Management.Accounts.list();
if (accounts.getItems()) {
var firstAccountId = accounts.getItems()[0].getId();
var webProperties = Analytics.Management.Webproperties.list(firstAccountId);
if (webProperties.getItems()) {
var firstWebPropertyId = webProperties.getItems()[0].getId();
var profiles = Analytics.Management.Profiles.list(firstAccountId, firstWebPropertyId);
if (profiles.getItems()) {
var firstProfile = profiles.getItems()[0];
return firstProfile;
} else {
throw new Error('No profiles found.');
}
} else {
throw new Error('No webproperties found.');
}
} else {
throw new Error('No accounts found.');
}
}
Upvotes: 0
Views: 519
Reputation: 45720
If you refer to the Analytics Service documentation, you'll find that all the .getItems()
methods return "a list of..." whatever, or arrays. Note that the example always referenced the first item of each array, [0]
. So you just need to iterate over the returned arrays to get everything.
This modified version of the function you referred to does just that, building up then returning an array of allProfiles
. (Corresponding changes to the rest of the example would need to be made to complete the reporting of this data.)
function getAllProfiles() {
var accounts = Analytics.Management.Accounts.list();
var allProfiles = [];
if (accounts.getItems()) {
for (var acct in accounts.getItems()) {
var accountId = accounts.getItems()[acct].getId();
var webProperties = Analytics.Management.Webproperties.list(accountId);
if (webProperties.getItems()) {
for (var prop in webProperties.getItems()) {
var webPropertyId = webProperties.getItems()[prop].getId();
var profiles = Analytics.Management.Profiles.list(accountId, webPropertyId);
if (profiles.getItems()) {
for (var item in profiles.getItems()) {
var profile = profiles.getItems()[item];
Logger.log(profile);
allProfiles.push(profile);
}
} else {
Logger.log('No profiles found [webProperty="'
+webProperties.getItems()[prop].getName()
+'"]');
}
}
} else {
Logger.log('No webproperties found [account="'
+accounts.getItems()[acct].getName()
+'"]');
}
}
} else {
Logger.log('No accounts found.');
}
return allProfiles;
}
Alternatively, if you know exactly what you want, just change the index values, which are all [0]
in the example, referencing the first item in the first Webproperties list of the first account.
Upvotes: 1