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; }
protected Analytics.Data.Ga.Get getQuery(Analytics analytics) { Analytics.Data dataApi = analytics.data(); Analytics.Data.Ga.Get query; try { String metrics = environmentSubstitute(meta.getMetrics()); if (Utils.isEmpty(metrics)) { logError(BaseMessages.getString(PKG, "GoogleAnalytics.Error.NoMetricsSpecified.Message")); return null; } query = dataApi .ga() .get( meta.isUseCustomTableId() ? environmentSubstitute(meta.getGaCustomTableId()) : meta.getGaProfileTableId(), // ids environmentSubstitute(meta.getStartDate()), // start date environmentSubstitute(meta.getEndDate()), // end date metrics // metrics ); String dimensions = environmentSubstitute(meta.getDimensions()); if (!Utils.isEmpty(dimensions)) { query.setDimensions(dimensions); } if (meta.isUseSegment()) { if (meta.isUseCustomSegment()) { query.setSegment(environmentSubstitute(meta.getCustomSegment())); } else { query.setSegment(meta.getSegmentId()); } } if (!Utils.isEmpty(meta.getSamplingLevel())) { query.setSamplingLevel(environmentSubstitute(meta.getSamplingLevel())); } if (!Utils.isEmpty(meta.getFilters()) && !Utils.isEmpty(environmentSubstitute(meta.getFilters()))) { query.setFilters(environmentSubstitute(meta.getFilters())); } if (!Utils.isEmpty(meta.getSort())) { query.setSort(environmentSubstitute(meta.getSort())); } return query; } catch (IOException ioe) { return null; } }
/** * Returns the top 25 source paths with most total conversions. The MCF API is used to retrieve * this data. * * @param analytics The analytics service object used to access the API. * @param tableId The table ID from which to retrieve data. * @return The response from the API. * @throws IOException If an API error occurred. */ private static McfData executePathQuery(Analytics analytics, String tableId) throws IOException { return analytics .data() .mcf() .get( tableId, "2012-01-01", // Start date. "2012-03-31", // End date. "mcf:totalConversions") // Metrics. .setDimensions("mcf:sourcePath") .setSort("-mcf:totalConversions") .setMaxResults(25) .execute(); }
/** * @param analytics * @param profileId * @param startDate format = YYYY-mm-DD * @param endDate format = YYYY-mm-DD * @return * @throws IOException */ private static GaData getData( Analytics analytics, String profileId, String startDate, String endDate) throws IOException { GaData sessionData = analytics .data() .ga() .get( "ga:" + profileId, // Table Id. ga: + profile id. startDate, // Start date endDate, // End date. "ga:pageviews,ga:sessions") .setDimensions("ga:date") .setSort("-ga:date") // Metrics. .execute(); return sessionData; }
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; }