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.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.AbstractMap;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
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 static GoogleAnalytics4 instance = null;
- private static final String appName;
-
- private static final String version;
+ private static final Map<String, String> defaultParams;
static
{
- appName = ChannelProperties.getProperty("app_name") + " Desktop";
- version = Cache.getProperty("VERSION") + "_"
- + Cache.getDefault("BUILD_DATE", "unknown");
+ defaultParams = new HashMap<>();
+ defaultParams.put("app_name",
+ ChannelProperties.getProperty("app_name") + " Desktop");
+ defaultParams.put("version", Cache.getProperty("VERSION"));
+ defaultParams.put("build_date",
+ Cache.getDefault("BUILD_DATE", "unknown"));
+ defaultParams.put("java_version", System.getProperty("java.version"));
+ String val = System.getProperty("sys.install4jVersion");
+ if (val != null)
+ {
+ defaultParams.put("install4j_version", val);
+ }
+ val = System.getProperty("installer_template_version");
+ if (val != null)
+ {
+ defaultParams.put("install4j_template_version", val);
+ }
+ val = System.getProperty("launcher_version");
+ if (val != null)
+ {
+ defaultParams.put("launcher_version", val);
+ }
+ defaultParams.put("java_arch",
+ System.getProperty("os.arch") + " "
+ + System.getProperty("os.name") + " "
+ + System.getProperty("os.version"));
}
private GoogleAnalytics4()
public void sendAnalytics(String eventName, String... paramsStrings)
{
+ sendAnalytics(eventName, false, paramsStrings);
+ }
+
+ /**
+ * The simplest way to send an analytic event.
+ *
+ * @param eventName
+ * The event name. To emulate a webpage view use "page_view" and set
+ * a "page_location" parameter. See
+ * https://developers.google.com/analytics/devguides/collection/ga4/events?client_type=gtag
+ * @param sendDefaultParams
+ * Flag whether to add the default params about the application.
+ * @param paramsStrings
+ * Optional multiple Strings in key, value pairs (there should be an
+ * even number of paramsStrings) to be set as parameters of the
+ * event. To emulate a webpage view use "page_location" as the URL in
+ * a "page_view" event.
+ */
+ public void sendAnalytics(String eventName, boolean sendDefaultParams,
+ String... paramsStrings)
+ {
// clear out old lists
this.resetLists();
return;
}
Map<String, String> params = new HashMap<>();
+ params.put("event_category", DESKTOP_EVENT);
+ params.put("event_label", eventName);
// add these to all events from this application instance
- params.put("app_name", appName);
- params.put("version", version);
+ if (sendDefaultParams)
+ {
+ params.putAll(defaultParams);
+ }
- // can be overwritten by passed in params
+ // add (and overwrite with) the passed in params
if (paramsStrings != null && paramsStrings.length > 0)
{
if (paramsStrings.length % 2 != 0)
addEvent(eventName, params);
addQueryStringValue("measurement_id", MEASUREMENT_ID);
addQueryStringValue("api_secret", API_SECRET);
+ addQueryStringValue("_geo", "1");
+ addQueryStringValue("ep.anonymize_ip", "false");
addJsonValue("client_id", CLIENT_ID);
addJsonValues("events", Event.toObjectList(events));
StringBuilder urlSb = new StringBuilder();
}
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)
{
sb.append('&');
}
- sb.append(URLEncoder.encode(entry.getKey(), StandardCharsets.UTF_8));
+ try
+ {
+ sb.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
+ } catch (UnsupportedEncodingException e)
+ {
+ sb.append(entry.getKey());
+ }
sb.append('=');
- sb.append(
- URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8));
+ try
+ {
+ sb.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
+ } catch (UnsupportedEncodingException e)
+ {
+ sb.append(entry.getValue());
+ }
}
return sb.toString();
}
}
if (repeat >= 0 && whitespace != null)
{
- sb.append(whitespace.repeat(repeat));
+ // sb.append(whitespace.repeat(repeat));
+ sb.append(String.join("", Collections.nCopies(repeat, whitespace)));
+
}
else
{