Hello all i am trying to use the printwriter to extract data from google analytics via JAVA
original code
private static void printGaData(GaData results) {
//csv printer
PrintWriter pw = null;
try {
pw = new PrintWriter(new BufferedWriter(new FileWriter("data.csv")));
}
catch(Exception e) {
e.printStackTrace();
}
//getting the queries to print
if (results.getRows() == null || results.getRows().isEmpty()) {
System.out.println("No results Found.");
} else {
// Print column headers.
for (ColumnHeaders header : results.getColumnHeaders()) {
System.out.printf(header.getName() + ", ");
}
pw.println();
// Print actual data.
for (List<String> row : results.getRows()) {
for (String column : row) {
System.out.printf(column + ", ");
}
pw.println();
}
pw.println();
}
}
}
it does not give me an error but however when i open the csv file there is nothing in there
EDIT:
ok so i have gotten it to print something but the only problem is that when i print the GaData only one variable shows up for example, ga:pageviews
heres my query
private static GaData executeDataQuery(Analytics analytics, String profileId) throws IOException {
return analytics.data().ga().get("ga:" + profileId, // Table Id. ga: + profile id.
"today", // Start date.
"today", // End date.
"ga:pageviews, ga:visits, ga:uniquePageviews") // Metrics.
.setDimensions("")
.setSort("-ga:visits")
.setFilters("ga:medium==organic")
.setMaxResults(25)
.execute();
}
Here is my edited code to extract the data
private static void printGaData(GaData results) {
//csv printer
PrintWriter pw = null;
try {
pw = new PrintWriter(new BufferedWriter(new FileWriter("data.csv")));
}
catch(Exception e) {
e.printStackTrace();
}
//getting the queries to print
if (results.getRows() == null || results.getRows().isEmpty()) {
pw.printf("No results Found.");
pw.close();
} else {
// Print column headers.
for (ColumnHeaders header : results.getColumnHeaders()) {
pw.printf(header.getName() + ", ");
pw.close();
}
pw.println();
pw.close();
// Print actual data.
for (List<String> row : results.getRows()) {
for (String column : row) {
pw.printf(column + ", ");
pw.close();
}
pw.println();
pw.close();
}
pw.println();
pw.close();
}
}
}
Thanks for all the help i am very new to java/programming