JAL-4001 Added a genuine client_id (from gtag). Added /debug/mp/collect with respons... task/JAL-4001_migrate_googleanalytics_to_GA4
authorBen Soares <b.soares@dundee.ac.uk>
Tue, 20 Jun 2023 09:17:33 +0000 (10:17 +0100)
committerBen Soares <b.soares@dundee.ac.uk>
Tue, 20 Jun 2023 09:17:33 +0000 (10:17 +0100)
src/jalview/analytics/GoogleAnalytics4.java
src/jalview/bin/Cache.java

index 7119828..8666e31 100644 (file)
@@ -1,6 +1,8 @@
 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;
@@ -17,7 +19,6 @@ 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;
@@ -33,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;
@@ -156,8 +167,8 @@ public class GoogleAnalytics4
     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);
@@ -189,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
@@ -199,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)
index e92fc75..4e73b82 100755 (executable)
@@ -999,7 +999,8 @@ public class Cache
     String[] params;
     if (headless)
     {
-      params = new String[] { "page_location", path, "page_title",
+      params = new String[] { "page_location",
+          GoogleAnalytics4.APPLICATION_BASE_URL + path, "page_title",
           APPLICATION_STARTED, "headless", "true" };
     }
     else