import jalview.bin.Cache;
import jalview.bin.Console;
import jalview.util.ChannelProperties;
+import jalview.util.HttpUtils;
public class Plausible
{
- private static final String USER_AGENT = ChannelProperties
- .getProperty("app_name", "Jalview") + " "
- + Cache.getDefault("VERSION", "Unknown") + " "
- + MethodHandles.lookup().lookupClass() + " help@jalview.org";
+ private static final String USER_AGENT = HttpUtils.getUserAgent(
+ MethodHandles.lookup().lookupClass().getCanonicalName());
private static final String JALVIEW_ID = "Jalview Desktop";
private static final String DOMAIN = "jalview.org";
- private static final String BASE_URL = "https://plausible.io/api/event";
+ private static final String CONFIG_API_BASE_URL = "https://www.jalview.org/config/analytics/url";
+
+ private static final String DEFAULT_API_BASE_URL = "https://plausible.io/api/event";
+
+ private static final String API_BASE_URL;
public static final String APPLICATION_BASE_URL = "desktop://localhost";
private List<Map.Entry<String, Object>> jsonObject;
- private List<Event> events;
-
private List<Map.Entry<String, String>> cookieValues;
private static boolean ENABLED = false;
+ private static boolean DEBUG = true;
+
private static Plausible instance = null;
- private static final Map<String, String> defaultParams;
+ private static final Map<String, String> defaultProps;
static
{
- defaultParams = new HashMap<>();
- defaultParams.put("app_name",
+ defaultProps = new HashMap<>();
+ defaultProps.put("app_name",
ChannelProperties.getProperty("app_name") + " Desktop");
- defaultParams.put("version", Cache.getProperty("VERSION"));
- defaultParams.put("build_date",
+ defaultProps.put("version", Cache.getProperty("VERSION"));
+ defaultProps.put("build_date",
Cache.getDefault("BUILD_DATE", "unknown"));
- defaultParams.put("java_version", System.getProperty("java.version"));
+ defaultProps.put("java_version", System.getProperty("java.version"));
String val = System.getProperty("sys.install4jVersion");
if (val != null)
{
- defaultParams.put("install4j_version", val);
+ defaultProps.put("install4j_version", val);
}
val = System.getProperty("installer_template_version");
if (val != null)
{
- defaultParams.put("install4j_template_version", val);
+ defaultProps.put("install4j_template_version", val);
}
val = System.getProperty("launcher_version");
if (val != null)
{
- defaultParams.put("launcher_version", val);
+ defaultProps.put("launcher_version", val);
}
- defaultParams.put("java_arch",
+ defaultProps.put("java_arch",
System.getProperty("os.arch") + " "
+ System.getProperty("os.name") + " "
+ System.getProperty("os.version"));
+ defaultProps.put("os", System.getProperty("os.name"));
+ defaultProps.put("os_version", System.getProperty("os.version"));
+ defaultProps.put("os_arch", System.getProperty("os.arch"));
+ String installation = Cache.applicationProperties
+ .getProperty("INSTALLATION");
+ if (installation != null)
+ {
+ defaultProps.put("installation", installation);
+ }
+
+ // ascertain the API_BASE_URL
+ API_BASE_URL = getAPIBaseURL();
}
private Plausible()
ENABLED = b;
}
- public void sendAnalytics(String eventName, String... paramsStrings)
+ public void sendEvent(String eventName, String urlString,
+ String... propsStrings)
{
- sendAnalytics(eventName, false, paramsStrings);
+ sendEvent(eventName, urlString, false, propsStrings);
}
/**
* 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
+ * The event name. To emulate a webpage view use "pageview" and set a
+ * "url" key/value. See https://plausible.io/docs/events-api
+ * @param sendDefaultProps
+ * Flag whether to add the default props about the application.
+ * @param propsStrings
* 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.
+ * even number of propsStrings) to be set as property of the event.
+ * To emulate a webpage view set "url" as the URL in a "pageview"
+ * event.
*/
- public void sendAnalytics(String eventName, boolean sendDefaultParams,
- String... paramsStrings)
+ public void sendEvent(String eventName, String urlString,
+ boolean sendDefaultProps, String... propsStrings)
{
// clear out old lists
this.resetLists();
Console.debug("Plausible not enabled.");
return;
}
- Map<String, String> params = new HashMap<>();
+ Map<String, String> props = new HashMap<>();
// add these to all events from this application instance
- if (sendDefaultParams)
+ if (sendDefaultProps)
{
- params.putAll(defaultParams);
+ props.putAll(defaultProps);
}
- // add (and overwrite with) the passed in params
- if (paramsStrings != null && paramsStrings.length > 0)
+ // add (and overwrite with) the passed in props
+ if (propsStrings != null && propsStrings.length > 0)
{
- if (paramsStrings.length % 2 != 0)
+ if (propsStrings.length % 2 != 0)
{
Console.warn(
- "Cannot addEvent with odd number of paramsStrings. Ignoring the last one.");
+ "Cannot addEvent with odd number of propsStrings. Ignoring the last one.");
}
- for (int i = 0; i < paramsStrings.length - 1; i += 2)
+ for (int i = 0; i < propsStrings.length - 1; i += 2)
{
- String key = paramsStrings[i];
- String value = paramsStrings[i + 1];
- params.put(key, value);
+ String key = propsStrings[i];
+ String value = propsStrings[i + 1];
+ props.put(key, value);
}
}
- 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));
+ addJsonValue("domain", DOMAIN);
+ addJsonValue("name", eventName);
+ StringBuilder eventUrlSb = new StringBuilder(APPLICATION_BASE_URL);
+ if (!APPLICATION_BASE_URL.endsWith("/") && !urlString.startsWith("/"))
+ {
+ eventUrlSb.append("/");
+ }
+ eventUrlSb.append(urlString);
+ addJsonValue("url", eventUrlSb.toString());
+ addJsonObject("props", props);
StringBuilder urlSb = new StringBuilder();
- urlSb.append(BASE_URL);
- urlSb.append('?');
- urlSb.append(buildQueryString());
+ urlSb.append(API_BASE_URL);
+ String qs = buildQueryString();
+ if (qs != null && qs.length() > 0)
+ {
+ urlSb.append('?');
+ urlSb.append(qs);
+ }
try
{
URL url = new URL(urlSb.toString());
String jsonString = buildJson();
- Console.debug("GA4: HTTP Request is: '" + urlSb.toString() + "'");
- Console.debug("GA4: POSTed JSON is:\n" + jsonString);
+ Console.debug(
+ "Plausible: HTTP Request is: '" + urlSb.toString() + "'");
+ if (DEBUG)
+ {
+ Console.debug("Plausible: User-Agent is: '" + USER_AGENT + "'");
+ }
+ Console.debug("Plausible: POSTed JSON is:\n" + jsonString);
byte[] jsonBytes = jsonString.getBytes(StandardCharsets.UTF_8);
int jsonLength = jsonBytes.length;
httpURLConnection.setFixedLengthStreamingMode(jsonLength);
httpURLConnection.setRequestProperty("Content-Type",
- "application/json; charset=UTF-8");
+ "application/json");
+ httpURLConnection.setRequestProperty("User-Agent", USER_AGENT);
httpURLConnection.connect();
try (OutputStream os = httpURLConnection.getOutputStream())
{
sb.append(response);
}
String body = sb.toString();
- Console.debug("Plausible response content; " + body);
+ Console.debug("Plausible response content:\n" + body);
}
} catch (MalformedURLException e)
{
return;
} catch (IOException e)
{
- Console.debug(
- "Connection to Plausible BASE_URL '" + BASE_URL + "' failed.",
- e);
+ Console.debug("Connection to Plausible BASE_URL '" + API_BASE_URL
+ + "' failed.", e);
} catch (ClassCastException e)
{
Console.debug(
}
}
- public void addEvent(String name, Map<String, String> params)
+ private void addJsonObject(String key, Map<String, String> map)
{
- Event event = new Event(name);
- if (params != null && params.size() > 0)
+ List<Map.Entry<String, ? extends Object>> list = new ArrayList<>();
+ for (String k : map.keySet())
{
- for (String key : params.keySet())
- {
- String value = params.get(key);
- event.addParam(key, value);
- }
+ list.add(stringEntry(k, map.get(k)));
}
- events.add(event);
+ addJsonObject(key, list);
+
}
private void addJsonObject(String key,
- List<Map.Entry<String, Object>> object)
+ List<Map.Entry<String, ? extends Object>> object)
{
jsonObject.add(objectEntry(key, object));
}
private void resetLists()
{
jsonObject = new ArrayList<>();
- events = new ArrayList<Event>();
queryStringValues = new ArrayList<>();
cookieValues = new ArrayList<>();
}
return new AbstractMap.SimpleEntry<String, String>(s, v);
}
- private static class Event
+ private static String getAPIBaseURL()
{
- private String name;
-
- private List<Map.Entry<String, String>> params;
-
- @SafeVarargs
- public Event(String name, Map.Entry<String, String>... paramEntries)
+ try
{
- this.name = name;
- this.params = new ArrayList<Map.Entry<String, String>>();
- for (Map.Entry<String, String> paramEntry : paramEntries)
+ URL url = new URL(CONFIG_API_BASE_URL);
+ URLConnection urlConnection = url.openConnection();
+ HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
+ httpURLConnection.setRequestMethod("GET");
+ httpURLConnection.setRequestProperty("User-Agent", USER_AGENT);
+ httpURLConnection.setConnectTimeout(5000);
+ httpURLConnection.setReadTimeout(3000);
+ httpURLConnection.connect();
+ int responseCode = httpURLConnection.getResponseCode();
+ String responseMessage = httpURLConnection.getResponseMessage();
+
+ if (responseCode < 200 || responseCode > 299)
{
- if (paramEntry == null)
- {
- continue;
- }
- params.add(paramEntry);
+ Console.warn("Config URL connection to '" + CONFIG_API_BASE_URL
+ + "' failed: '" + responseCode + " " + responseMessage
+ + "'");
}
- }
- public void addParam(String param, String value)
- {
- params.add(Plausible.stringEntry(param, value));
- }
-
- protected List<Map.Entry<String, Object>> toObject()
- {
- List<Map.Entry<String, Object>> object = new ArrayList<>();
- object.add(Plausible.objectEntry("name", (Object) name));
- if (params.size() > 0)
+ BufferedReader br = new BufferedReader(
+ new InputStreamReader((httpURLConnection.getInputStream())));
+ StringBuilder sb = new StringBuilder();
+ String response;
+ while ((response = br.readLine()) != null)
{
- object.add(Plausible.objectEntry("params", (Object) params));
+ sb.append(response);
}
- return object;
- }
-
- protected static List<Object> toObjectList(List<Event> events)
- {
- List<Object> eventObjectList = new ArrayList<>();
- for (Event event : events)
+ if (sb.length() > 7 && sb.substring(0, 5).equals("https"))
{
- eventObjectList.add((Object) event.toObject());
+ return sb.toString();
}
- return eventObjectList;
+
+ } catch (MalformedURLException e)
+ {
+ Console.debug("Somehow the config URL is malformed: '"
+ + CONFIG_API_BASE_URL + "'", e);
+ } catch (IOException e)
+ {
+ Console.debug("Connection to Plausible BASE_URL '" + API_BASE_URL
+ + "' failed.", e);
+ } catch (ClassCastException e)
+ {
+ Console.debug(
+ "Couldn't cast URLConnection to HttpURLConnection in Plausible.",
+ e);
}
+ return DEFAULT_API_BASE_URL;
}
-
}
\ No newline at end of file