0

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.');
    }
}

1 Answer 1

1

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.

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

3 Comments

I realize now that I have two accounts with multiple properties. Unfortunately, I was trying to be explicit with the index value and it was pulling from the first (legacy) account. I want to pull in data from my second account. – I want to pull in data from my second account.
The code you've offered throws an error on line 21. No profiles found.
True, but only if there are no profiles for a Webproperty. (Not my code, by the way - that's your example.) Since it's building a list, though, it would be more appropriate to Log the exception and carry on, so I'll make that change.

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.