import java.io.IOException;
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;
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;
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.putAll(defaultParams);
+ if (sendDefaultParams)
+ {
+ params.putAll(defaultParams);
+ }
// add (and overwrite with) the passed in params
if (paramsStrings != null && paramsStrings.length > 0)
{
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
{