JAL-4001 Changed for Java 8 compatibility
[jalview.git] / src / jalview / analytics / GoogleAnalytics4.java
index 5ae9c12..ba8a920 100644 (file)
@@ -2,6 +2,7 @@ package jalview.analytics;
 
 import java.io.IOException;
 import java.io.OutputStream;
+import java.io.UnsupportedEncodingException;
 import java.net.HttpURLConnection;
 import java.net.MalformedURLException;
 import java.net.URL;
@@ -10,6 +11,7 @@ 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;
@@ -37,6 +39,8 @@ public class GoogleAnalytics4
 
   private static final String BASE_URL = "https://www.google-analytics.com/mp/collect";
 
+  private static final String DESKTOP_EVENT = "desktop_event";
+
   private List<Map.Entry<String, String>> queryStringValues;
 
   private List<Map.Entry<String, Object>> jsonObject;
@@ -93,6 +97,27 @@ public class 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();
 
@@ -102,9 +127,14 @@ public class GoogleAnalytics4
       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.putAll(defaultParams);
+    if (sendDefaultParams)
+    {
+      params.putAll(defaultParams);
+    }
 
     // add (and overwrite with) the passed in params
     if (paramsStrings != null && paramsStrings.length > 0)
@@ -266,10 +296,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();
   }
@@ -405,7 +446,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
     {