@Override public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { ScrollView sv = new ScrollView(getActivity()); TableLayout tl = new TableLayout(getActivity()); sv.addView(tl); if (displayReportController == null) { return sv; } AdsenseReportsGenerateResponse response = displayReportController.getReportResponse(); TableLayout.LayoutParams tableRowParams = new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); tableRowParams.setMargins(10, 10, 10, 10); TableRow.LayoutParams tvParams = new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); tvParams.setMargins(10, 10, 10, 10); List<Headers> headers = response.getHeaders(); TableRow tr = new TableRow(getActivity()); tl.addView(tr); for (Headers header : headers) { TextView tv = new TextView(getActivity()); tv.setText(header.getName()); tr.setLayoutParams(tableRowParams); tr.addView(tv); } if (response.getRows() != null && !response.getRows().isEmpty()) { for (List<String> row : response.getRows()) { TableRow trow = new TableRow(getActivity()); tl.addView(trow); for (String cell : row) { TextView tv = new TextView(getActivity()); tv.setText(cell); trow.addView(tv); tv.setLayoutParams(tvParams); tv.setPadding(15, 5, 15, 5); tv.setBackgroundColor(Color.WHITE); } } } return sv; }
/** * Runs this sample. * * @param adsense AdSense service object on which to run the requests. * @param accountId the ID for the account to be used. * @param adClientId the ad client ID on which to run the report. * @throws Exception */ public static void run(AdSense adsense, String accountId, String adClientId) throws Exception { System.out.println("================================================================="); System.out.printf("Running reports for ad client %s, and collating data\n", adClientId); System.out.println("================================================================="); // The first report is for "last week", i.e., the 7-day period ending yesterday. Generate lastWeekRequest = adsense.accounts().reports().generate(accountId, "today-7d", "today-1d"); // The second report is for the "previous week", i.e., the 7-day period ending eight days ago. Generate prevWeekRequest = adsense.accounts().reports().generate(accountId, "today-14d", "today-8d"); lastWeekRequest.setMetric(Arrays.asList("CLICKS", "EARNINGS")); prevWeekRequest.setMetric(Arrays.asList("CLICKS", "EARNINGS")); lastWeekRequest.setDimension(Arrays.asList("PLATFORM_TYPE_CODE", "PLATFORM_TYPE_NAME")); prevWeekRequest.setDimension(Arrays.asList("PLATFORM_TYPE_CODE", "PLATFORM_TYPE_NAME")); // Sort by ascending PLATFORM_TYPE_CODE. lastWeekRequest.setSort(Arrays.asList("+PLATFORM_TYPE_CODE")); prevWeekRequest.setSort(Arrays.asList("+PLATFORM_TYPE_CODE")); // Run reports. AdsenseReportsGenerateResponse lastWeekResponse = lastWeekRequest.execute(); AdsenseReportsGenerateResponse prevWeekResponse = prevWeekRequest.execute(); AdsenseReportsGenerateResponse[] responses = {lastWeekResponse, prevWeekResponse}; // Create new lists for filled data with the existing data in the responses. List<List<String>> lastRows = new ArrayList<List<String>>(); List<List<String>> prevRows = new ArrayList<List<String>>(); // Add existing data to new lists. if (lastWeekResponse.getRows() != null && !lastWeekResponse.getRows().isEmpty()) { lastRows.addAll(lastWeekResponse.getRows()); } if (prevWeekResponse.getRows() != null && !prevWeekResponse.getRows().isEmpty()) { prevRows.addAll(prevWeekResponse.getRows()); } // Compile complete set of platforms and platform codes across both report responses. Map<String, String> platformNames = new HashMap<String, String>(); for (AdsenseReportsGenerateResponse response : responses) { if (response.getRows() != null && !response.getRows().isEmpty()) { for (List<String> row : response.getRows()) { platformNames.put(row.get(0), row.get(1)); } } } Set<String> platforms = platformNames.keySet(); // How many metrics have we got? int metrics = 0; if (lastRows.size() > 0) { // Subtracting 2 to skip headers for dimensions. metrics = lastRows.get(0).size() - 2; } else if (prevRows.size() > 0) { // Subtracting 2 to skip headers for dimensions. metrics = prevRows.get(0).size() - 2; } // Add missing data to both datasets. List<List<List<String>>> datasets = Arrays.asList(lastRows, prevRows); for (List<List<String>> dataset : datasets) { if (dataset.size() < platforms.size()) { // Compile list of platforms in this dataset. List<String> datasetPlatforms = new ArrayList<String>(); for (List<String> row : dataset) { datasetPlatforms.add(row.get(0)); } // Compile list of missing platforms in this dataset. List<String> missing = new ArrayList<String>(platforms); missing.removeAll(datasetPlatforms); // Add data for all missing platforms. for (String platform : missing) { List<String> newRow = new ArrayList<String>(); // Add platform code and name. newRow.add(platform); newRow.add(platformNames.get(platform)); // Add metrics. for (int i = 0; i < metrics; i++) { newRow.add("0"); } dataset.add(newRow); } // Sort dataset. Collections.sort( dataset, new Comparator<List<String>>() { public int compare(List<String> a, List<String> b) { return a.get(0).compareTo(b.get(0)); } }); } } // Display effective date range. System.out.printf( "Results for last week (%s to %s) versus the previous week (%s to %s).\n", lastWeekResponse.getStartDate(), lastWeekResponse.getEndDate(), prevWeekResponse.getStartDate(), prevWeekResponse.getEndDate()); System.out.println(); // Display collated data. for (int platformIndex = 0; platformIndex < platforms.size(); platformIndex++) { List<String> lastRow = lastRows.get(platformIndex); List<String> prevRow = prevRows.get(platformIndex); String platform = lastRows.get(platformIndex).get(0); System.out.printf("%s:\n", platformNames.get(platform)); // Adding 2 to skip headers for dimensions. for (int metricIndex = 2; metricIndex < metrics + 2; metricIndex++) { String metricName = lastWeekResponse.getHeaders().get(metricIndex).getName(); System.out.printf( "- %f delta (%s last week vs %s in the previous week) on %s\n", Float.valueOf(lastRow.get(metricIndex)) - Float.valueOf(prevRow.get(metricIndex)), lastRow.get(metricIndex), prevRow.get(metricIndex), metricName); } } System.out.println(); }