Hey there!
I don't know how but I finally managed to find a way to get Real Time Pageviews from my Website, with the help of Google Analytics. In this tutorial here they explain how to access the Google Analytics Real time API - I installed Gradle to create a Java Application and now I can see the Active Users in Terminal like this:
my questions is now, is that possible to get the last, actual Active Users (it refreshes every minute) as a Keyboard Maestro Variable?
This here is the Java class code:
import java.text.DateFormat;
import java.util.Date;
import java.util.List;
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.analytics.Analytics;
import com.google.api.services.analytics.Analytics.Data.Realtime.Get;
import com.google.api.services.analytics.model.RealtimeData;
// Displays Google analytics real time users on console using Java.
// Uses refresh token generated from oAuth2 playground
// See http://www.quickprogrammingtips.com for more details.
public class GARealTimeDisplay {
private static final String APPLICATION_NAME="GARealTimeDisplay";
private static final String GA_PROPERTY_ID = "XXXXXX";
private static final String GAPI_CLIENT_ID = "XXXXXX";
private static final String GAPI_CLIENT_SECRET = "XXXXXX";
private static final String GAPI_OAUTH_USER_REFRESH_TOKEN="XXXXXX";
public static void main(String[] args) throws Exception {
try {
Analytics analytics = initializeAnalytics();
System.out.println("The following display is refreshed every minute");
while(true) {
printRealTimeDisplay(analytics);
Thread.sleep(10000); // wait for a minute!
}
} catch (Throwable e) {
System.out.println("Error: " + e.getMessage());
}
}
// Initialize google analytics api using client id/secret and user refresh token
// client id/secret generated from developer API console
// refresh token generated from google oauth 2 playground
private static Analytics initializeAnalytics() throws Exception {
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
Credential credential = new GoogleCredential.Builder().setTransport(httpTransport).setJsonFactory(jsonFactory)
.setClientSecrets(GAPI_CLIENT_ID, GAPI_CLIENT_SECRET).build();
credential.setRefreshToken(GAPI_OAUTH_USER_REFRESH_TOKEN);
return new Analytics.Builder(httpTransport, jsonFactory, credential).setApplicationName(APPLICATION_NAME)
.build();
}
// Display only the real time users to a site as reported by google analytics
private static void printRealTimeDisplay(Analytics analytics) throws Exception {
Get realtimeRequest = analytics.data().realtime().get("ga:" + GA_PROPERTY_ID, "rt:activeUsers");
RealtimeData realtimeData = realtimeRequest.execute();
String property = realtimeData.getProfileInfo().getProfileName();
String time = DateFormat.getDateTimeInstance().format(new Date());
if (realtimeData.getTotalResults() > 0) {
for (List<String> row : realtimeData.getRows()) {
for (String element : row) {
System.out.println(element);
}
}
} else {
System.out.println("No data received");
}
}
}