JAL-4001 Added a genuine client_id (from gtag). Added /debug/mp/collect with respons...
[jalview.git] / src / jalview / analytics / GoogleAnalytics4.java
index d247662..8666e31 100644 (file)
@@ -1,7 +1,10 @@
 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;
@@ -10,12 +13,12 @@ import java.net.URLEncoder;
 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;
@@ -31,14 +34,24 @@ public class GoogleAnalytics4
 
   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;
@@ -153,6 +166,8 @@ public class GoogleAnalytics4
     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();
@@ -185,6 +200,7 @@ public class GoogleAnalytics4
       }
       int responseCode = httpURLConnection.getResponseCode();
       String responseMessage = httpURLConnection.getResponseMessage();
+
       if (responseCode < 200 || responseCode > 299)
       {
         Console.warn("GoogleAnalytics4 connection failed: '" + responseCode
@@ -195,10 +211,25 @@ public class GoogleAnalytics4
         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)
@@ -294,10 +325,21 @@ public class GoogleAnalytics4
       {
         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();
   }
@@ -433,7 +475,9 @@ public class GoogleAnalytics4
     }
     if (repeat >= 0 && whitespace != null)
     {
-      sb.append(whitespace.repeat(repeat));
+      // sb.append(whitespace.repeat(repeat));
+      sb.append(String.join("", Collections.nCopies(repeat, whitespace)));
+
     }
     else
     {