Beispiel #1
0
  private static String SITEProfileId(Analytics analytics) throws IOException {
    String profileId = null;

    // Query accounts collection.
    com.google.api.services.analytics.model.Accounts accounts =
        analytics.management().accounts().list().execute();

    if (accounts.getItems().isEmpty()) {
      logger.error("No Google Analytics accounts found");
    } else {
      String firstAccountId = accounts.getItems().get(0).getId();

      // Query webproperties collection.
      Webproperties webproperties =
          analytics.management().webproperties().list(firstAccountId).execute();

      if (webproperties.getItems().isEmpty()) {
        logger.error("No Google Analytics Webproperties found");
      } else {
        String firstWebpropertyId = webproperties.getItems().get(0).getId();

        // Query views (profiles) collection.
        Profiles profiles =
            analytics.management().profiles().list(firstAccountId, firstWebpropertyId).execute();

        if (profiles.getItems().isEmpty()) {
          logger.error("No Google Analytics views (profiles) found");
        } else {
          // 1 for "SITE All Projects"
          profileId = profiles.getItems().get(1).getId();
        }
      }
    }
    return profileId;
  }
 private String findProfileId(Analytics analytics, String accountName) throws IOException {
   logger.info((new StringBuilder()).append("Account Name: ").append(accountName).toString());
   String profileId = null;
   Accounts accounts = (Accounts) analytics.management().accounts().list().execute();
   if (accounts.getItems().isEmpty()) {
     logger.debug("No accounts found");
   } else {
     logger.debug(
         (new StringBuilder())
             .append("Accounts found: ")
             .append(accounts.getItems().size())
             .toString());
     String accountId = null;
     Iterator i$ = accounts.getItems().iterator();
     do {
       if (!i$.hasNext()) break;
       Account account = (Account) i$.next();
       String googleAccountName = account.getName();
       logger.debug(
           (new StringBuilder())
               .append("googleAccountName: ")
               .append(googleAccountName)
               .toString());
       logger.debug((new StringBuilder()).append("accountName: ").append(accountName).toString());
       if (!googleAccountName.equalsIgnoreCase(accountName)) continue;
       accountId = account.getId();
       break;
     } while (true);
     logger.debug((new StringBuilder()).append("accountId: ").append(accountId).toString());
     Webproperties webproperties =
         (Webproperties) analytics.management().webproperties().list(accountId).execute();
     if (webproperties.getItems().isEmpty()) {
       logger.debug("No Webproperties found");
     } else {
       String firstWebpropertyId = ((Webproperty) webproperties.getItems().get(0)).getId();
       Profiles profiles =
           (Profiles)
               analytics.management().profiles().list(accountId, firstWebpropertyId).execute();
       if (profiles.getItems().isEmpty()) logger.debug("No profiles found");
       else profileId = ((Profile) profiles.getItems().get(0)).getId();
     }
   }
   return profileId;
 }
  public boolean init(StepMetaInterface smi, StepDataInterface sdi) {
    meta = (GaInputStepMeta) smi;
    data = (GaInputStepData) sdi;

    if (!super.init(smi, sdi)) {
      return false;
    }

    // Look for deprecated field types and log error(s) for them
    String[] types = environmentSubstitute(meta.getFeedFieldType());
    if (types != null) {
      for (String type : types) {
        if (GaInputStepMeta.DEPRECATED_FIELD_TYPE_CONFIDENCE_INTERVAL.equals(type)) {
          logError(
              BaseMessages.getString(
                  PKG,
                  "GoogleAnalytics.Warn.FieldTypeNotSupported",
                  GaInputStepMeta.DEPRECATED_FIELD_TYPE_CONFIDENCE_INTERVAL));
        }
      }
    }

    String appName = environmentSubstitute(meta.getGaAppName());
    String serviceAccount = environmentSubstitute(meta.getOAuthServiceAccount());
    String OAuthKeyFile = environmentSubstitute(meta.getOAuthKeyFile());

    if (log.isDetailed()) {
      logDetailed(
          BaseMessages.getString(PKG, "GoogleAnalyticsDialog.AppName.Label") + ": " + appName);
      logDetailed(
          BaseMessages.getString(PKG, "GoogleAnalyticsDialog.OauthAccount.Label")
              + ": "
              + serviceAccount);
      logDetailed(
          BaseMessages.getString(PKG, "GoogleAnalyticsDialog.KeyFile.Label") + ": " + OAuthKeyFile);
    }

    try {
      // Create an Analytics object, and fetch what we can for later (account name, e.g.)
      analytics =
          GoogleAnalyticsApiFacade.createFor(appName, serviceAccount, OAuthKeyFile).getAnalytics();
      // There is necessarily an account name associated with this, so any NPEs or other exceptions
      // mean bail out
      accountName =
          analytics.management().accounts().list().execute().getItems().iterator().next().getName();
    } catch (TokenResponseException tre) {
      Exception exceptionToLog = tre;
      if (tre.getDetails() != null && tre.getDetails().getError() != null) {
        exceptionToLog =
            new IOException(
                BaseMessages.getString(
                    PKG, "GoogleAnalytics.Error.OAuth2.Auth", tre.getDetails().getError()),
                tre);
      }
      logError(BaseMessages.getString(PKG, "GoogleAnalytics.Error.AccessingGaApi"), exceptionToLog);
      return false;
    } catch (Exception e) {
      logError(BaseMessages.getString(PKG, "GoogleAnalytics.Error.AccessingGaApi"), e);
      return false;
    }
    return true;
  }