3

Wondering if anyone has experience exporting a list of GA accounts and properties to a spreadsheet using the new Analytics Admin API.

I've used the Management API for this purpose in the past but that limits us to UA properties and I want to hopefully include GA4 properties here as well.

I've taken a shot at converting an old script to the new API but I haven't even succeeded in pulling in account names.

function listGA4Accounts() {
  var createss = SpreadsheetApp.create("Google Analytics Accounts");
  var ssid = createss.getId();
  var openss = SpreadsheetApp.openById(ssid);
  var insertsheet = openss.insertSheet('Google Analytics Schema');
  insertsheet.setFrozenRows(1);
  insertsheet.getRange("A1:D1").setValues([['displayName', 'Account ID', 'Property Name', 'Property ID']]);
  var sheet = SpreadsheetApp.openById(createss.getId()).getSheetByName("Google Analytics Schema")
  var accounts = AnalyticsAdmin.Accounts.list();
  if (accounts && !accounts.error) {
    accounts = accounts.accounts;
  //  Logger.log(accounts[0]);
    for (var i = 0, account; account = accounts[i]; i++) {
       sheet.appendRow([accounts.accounts[i].displayName]);
    }
  }
}
2
  • Have you considered accountSummaries/list Commented Apr 7, 2022 at 9:19
  • The accountSummaries does seem promising. Apologies if it's a dumb question but would I essentially substitute the line var = AnalyticsAdmin.accountSummaries.list(); Commented Apr 8, 2022 at 11:11

2 Answers 2

2

I've been struggling with this as well. Here's what I came up with after a lot of trail and error:

function listAccounts() {
  try {
    accounts = AnalyticsAdmin.AccountSummaries.list();
    //Logger.log(accounts);
    if (!accounts.accountSummaries || !accounts.accountSummaries.length) {
      Logger.log('No accounts found.');
      return;
    }

    Logger.log(accounts.accountSummaries.length + ' accounts found');
    for (let i = 0; i < accounts.accountSummaries.length; i++) {
      const account = accounts.accountSummaries[i];
      Logger.log('** Account: name "%s", displayName "%s".', account.name, account.displayName);

      if (account.propertySummaries) {
        properties = AnalyticsAdmin.Properties.list({filter: 'parent:' + account.account});
        if (properties.properties !== null) {
          for (let j = 0 ; j < properties.properties.length ; j++) {
            var propertyID = properties.properties[j].name.replace('properties/','');
            Logger.log("GA4 Property: " + properties.properties[j].displayName + '(' + propertyID + ')')
          }
        }
      } else {
        var accountID = account.account.replace('accounts/','');
        var webProperties = Analytics.Management.Webproperties.list(accountID);
        for (var j = 0; j < webProperties.items.length; j++) {
          Logger.log("UA Property: " + webProperties.items[j].name + '(' + webProperties.items[j].id  + ')');
          var profiles = Analytics.Management.Profiles.list(accountID, webProperties.items[j].id);
          for (var k = 0; k < profiles.items.length; k++) {
            Logger.log('Profile:' + profiles.items[k].name);
          }
        }
      }
    }
  } catch (e) {
    // TODO (Developer) - Handle exception
    Logger.log('Failed with error: %s', e.error);
  }
}

Not saying it's the right way...but it does appear to be able to pull all of my UA and GA4 properties.

If you only want GA4 properties then you can leave out the else on "if (account.propertySummaries)".

Sign up to request clarification or add additional context in comments.

1 Comment

I was wrong - it's not pulling all UA properties, it's only pulling UA properties don't have GA4 properties in the same account due to that if/else. So if you want all of your UA properties as well you'll have to make some changes - but since the original question was only about GA4 properties my answer still appears to be valid on those.
-1

Not the exact solution, but I was able to pull this list using microsoft power bi. pbi has a google analytics connector, which requires you to select a specific property and view, but if you edit the underlying code using advanced editor, you can back out of all that and just get a list of properties.

This was the code at the end

let
  Source = GoogleAnalytics.Accounts([Implementation = "2.0"]),
  #"Added items" = Cube.Transform(#"Accelerating Health Access - GA4", {{Cube.AddMeasureColumn, "active1DayUsers", "active1DayUsers"}}),
  Custom = Source,
  #"Expanded Data" = Table.ExpandTableColumn(Custom, "Data", {"Id", "Name", "Data", "Kind"}, {"Id.1", "Name.1", "Data.1", "Kind.1"})
in
  #"Expanded Data"

Posting in case its helpful. Works on power bi desktop which is free, and seems like this is hard info to come by otherwise.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.