0

このページの Google スプレッドシート App Script にデータをインポートする Google App Script の例を使用しています。 この例は正常に動作しますが、データを取得している Web プロパティを変更したいと考えています。

例を探して、サンプル コードのさまざまな繰り返しを試しましたが、正しくありません。このコードを変更してプロファイル内の別のプロパティをプルする方法について、誰かがこのコードを手伝ってくれますか?

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

1 に答える 1

1

Analytics Serviceのドキュメント を参照すると、すべての.getItems()メソッドが「...のリスト」または配列を返すことがわかります。この例では、常に各配列の最初の項目 を参照していることに注意してください[0]。したがって、返された配列を反復処理してすべてを取得する必要があります。

あなたが参照した関数のこの変更されたバージョンは、まさにそれを行い、構築して の配列を返しallProfilesます。(このデータのレポートを完了するには、残りの例に対応する変更を加える必要があります。)

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;
}

または、必要なものが正確にわかっている場合[0]は、最初のアカウントの最初の Web プロパティ リストの最初の項目を参照して、例にあるすべてのインデックス値を変更します。

于 2013-03-26T04:49:58.293 に答える