I m making a script that will populate a Google Sheet with info about our GA4 analytics properties. It starts by listing all our accounts, then looping through them and listing any GA4 properties in them, then looping through the properties and outputting their info to the sheet.
The issue I m having is when it reaches an account that doesn t contain any GA4 properties, the script just stops instead of continuing to the next account.
I made the script for UA properties first and it worked perfectly, so I m confused on why it fails in this way just because the API endpoints are different. I ve tried changing the "return" to "break" etc. and a bunch of other things but nothing I tried makes any difference.
function init_ga4() {
// define current sheet as target
const activeSheet = SpreadsheetApp.getActiveSheet();
// clear existing data
activeSheet.getRange("A2:D1000").clear();
// run main function
get_ga4_data(activeSheet);
}
function get_ga4_data(activeSheet) {
// make list of all accounts accessible by current user
const accounts = Analytics.Management.Accounts.list();
// set row to start outputting data on
let r = 2;
// loop through each account
for (let i = 0; i < accounts.items.length; i++) {
// get current account idetails
const account = accounts.items[i];
// make list of properties in current account
const propertiesList = AnalyticsAdmin.Properties.list({ filter: "parent:accounts/" + account.id });
// check if account doesn t have any GA4 properties
if (propertiesList.properties === undefined) {
return;
} else {
// count properties in list
const propertiesCount = propertiesList.properties.length;
// get the range for properties in this accout and output account name
const accountNameRange = activeSheet.getRange(`A${r}:A${r + (propertiesCount - 1)}`);
accountNameRange.setValue(account.name);
const properties = propertiesList.properties;
// loop through properties and put the property details in the right cells
for (let p = 0; p < properties.length; p++) {
const property = properties[p];
const propertyNameCell = activeSheet.getRange(`C${r + p}`);
propertyNameCell.setValue(property.displayName);
}
// update starting row for next batch of data
r += propertiesCount;
}
}
}