package jalview.analytics;
+import java.io.BufferedReader;
import java.io.IOException;
+import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.util.List;
import java.util.Map;
import java.util.Random;
-import java.util.UUID;
import jalview.bin.Cache;
import jalview.bin.Console;
private static final String API_SECRET = "Qb9NSbqkRDqizG6j2BBJ2g";
- // This will generate a different CLIENT_ID each time the application is
- // launched. Do we want to store it in .jalview_properties?
- private static final String CLIENT_ID = UUID.randomUUID().toString();
+ // Client ID must be generated from gtag.js, used and captured.
+ // Will this affect geolocation?
+ private static final String CLIENT_ID = "2092672487.1686663096";
- private static final String BASE_URL = "https://www.google-analytics.com/mp/collect";
+ // set to true to use the GA4 measurement protocol validation service and
+ // print
+ // validation response.
+ // see
+ // https://developers.google.com/analytics/devguides/collection/protocol/ga4/validating-events?client_type=gtag
+ private static boolean DEBUG = false;
+
+ private static final String BASE_URL = "https://www.google-analytics.com/"
+ + (DEBUG ? "debug/" : "") + "mp/collect";
private static final String DESKTOP_EVENT = "desktop_event";
+ public static final String APPLICATION_BASE_URL = "https://www.jalview.org";
+
private List<Map.Entry<String, String>> queryStringValues;
private List<Map.Entry<String, Object>> jsonObject;
addQueryStringValue("measurement_id", MEASUREMENT_ID);
addQueryStringValue("api_secret", API_SECRET);
addQueryStringValue("_geo", "1");
+ addQueryStringValue("ep.anonymize_ip", "false");
addJsonValue("client_id", CLIENT_ID);
- addJsonValue("_geo", 1);
addJsonValues("events", Event.toObjectList(events));
StringBuilder urlSb = new StringBuilder();
urlSb.append(BASE_URL);
}
int responseCode = httpURLConnection.getResponseCode();
String responseMessage = httpURLConnection.getResponseMessage();
+
if (responseCode < 200 || responseCode > 299)
{
Console.warn("GoogleAnalytics4 connection failed: '" + responseCode
Console.debug("GoogleAnalytics4 connection succeeded: '"
+ responseCode + " " + responseMessage + "'");
}
+
+ if (DEBUG)
+ {
+ BufferedReader br = new BufferedReader(new InputStreamReader(
+ (httpURLConnection.getInputStream())));
+ StringBuilder sb = new StringBuilder();
+ String response;
+ while ((response = br.readLine()) != null)
+ {
+ sb.append(response);
+ }
+ String body = sb.toString();
+ Console.debug("GoogleAnalytics4 response content; " + body);
+ }
} catch (MalformedURLException e)
{
Console.debug(
- "Somehow the GoogleAnalytics4 BASE_URL and queryString is malformed.",
+ "Somehow the GoogleAnalytics4 BASE_URL and queryString is malformed: '"
+ + urlSb.toString() + "'",
e);
return;
} catch (IOException e)