-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update Google Analytics to use GA4, fix #302
- Loading branch information
Showing
5 changed files
with
214 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
.../src/main/java/com/speedment/jpastreamer/analytics/standard/internal/google/HttpUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package com.speedment.jpastreamer.analytics.standard.internal.google; | ||
|
||
import java.io.*; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
import java.net.URLEncoder; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.concurrent.Executor; | ||
import java.util.concurrent.Executors; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
final class HttpUtil { | ||
|
||
private static final int DEFAULT_TIME_OUT_MS = 2_000; | ||
|
||
private static final String THREAD_NAME = "chronicle~analytics~http~client"; | ||
private static final Executor EXECUTOR = Executors.newSingleThreadExecutor(runnable -> { | ||
final Thread thread = new Thread(runnable, THREAD_NAME); | ||
thread.setDaemon(true); | ||
return thread; | ||
}); | ||
|
||
private HttpUtil() { | ||
} | ||
|
||
public static void send(final String urlString, | ||
final String body) { | ||
requireNonNull(urlString); | ||
requireNonNull(body); | ||
EXECUTOR.execute(new Sender(urlString, body)); | ||
} | ||
|
||
static String urlEncode(final String s) { | ||
requireNonNull(s); | ||
try { | ||
return URLEncoder.encode(s, StandardCharsets.UTF_8.toString()); | ||
} catch (UnsupportedEncodingException e) { | ||
System.err.println("Exception while URL encoding statistics for Google Analytics."); | ||
e.printStackTrace(); | ||
throw new InternalAnalyticsException("This should never happen as " + StandardCharsets.UTF_8 + " should always be present."); | ||
} | ||
} | ||
|
||
static final class Sender implements Runnable { | ||
|
||
private final String urlString; | ||
private final String body; | ||
|
||
Sender(final String urlString, | ||
final String body) { | ||
requireNonNull(urlString); | ||
requireNonNull(body); | ||
this.urlString = urlString; | ||
this.body = body; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
try { | ||
final URL url = new URL(urlString); | ||
final HttpURLConnection conn = (HttpURLConnection) url.openConnection(); | ||
// Do not linger if the connection is slow. Give up instead! | ||
conn.setConnectTimeout(DEFAULT_TIME_OUT_MS); | ||
conn.setReadTimeout(DEFAULT_TIME_OUT_MS); | ||
conn.setRequestMethod("POST"); | ||
conn.setRequestProperty("Content-Type", "text/plain; charset=UTF-8"); | ||
conn.setRequestProperty("t", "application/json"); | ||
conn.setDoOutput(true); | ||
try (OutputStream os = conn.getOutputStream()) { | ||
final byte[] output = body.getBytes(StandardCharsets.UTF_8); | ||
os.write(output, 0, output.length); | ||
os.flush(); | ||
} | ||
|
||
try (BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8))) { | ||
final StringBuilder response = new StringBuilder(); | ||
String sep = ""; | ||
for (String responseLine; (responseLine = br.readLine()) != null; ) { | ||
response.append(sep).append(responseLine); // preserve some white space | ||
sep = " "; | ||
} | ||
final String logMsg = response.toString().replaceAll("\\s+(?=\\S)", " "); | ||
if (!logMsg.isEmpty()) | ||
System.out.println(logMsg); | ||
} | ||
|
||
} catch (IOException ioe) { | ||
System.err.println("Exception while sending usage statistics to Google Analytics."); | ||
ioe.printStackTrace(); | ||
} | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
.../speedment/jpastreamer/analytics/standard/internal/google/InternalAnalyticsException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.speedment.jpastreamer.analytics.standard.internal.google; | ||
|
||
final class InternalAnalyticsException extends RuntimeException { | ||
|
||
InternalAnalyticsException(String message) { | ||
super(message); | ||
} | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
.../src/main/java/com/speedment/jpastreamer/analytics/standard/internal/google/JsonUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.speedment.jpastreamer.analytics.standard.internal.google; | ||
|
||
final class JsonUtil { | ||
|
||
private static final String NL = String.format("%n"); | ||
|
||
private JsonUtil() { | ||
} | ||
|
||
static String jsonElement(final String indent, | ||
final String key, | ||
final Object value) { | ||
return indent + asElement(key) + ": " + asElement(value); | ||
} | ||
|
||
static String asElement(final Object value) { | ||
return value instanceof CharSequence | ||
? '"' + escape(value.toString()) + '"' | ||
: value.toString(); | ||
|
||
} | ||
|
||
static String escape(final String raw) { | ||
return raw | ||
.replace("\\", "\\\\") | ||
.replace("\"", "\\\"") | ||
.replace("\b", "\\b") | ||
.replace("\f", "\\f") | ||
.replace("\n", "\\n") | ||
.replace("\r", "\\r") | ||
.replace("\t", "\\t"); | ||
// Todo: escape other non-printing characters ... | ||
} | ||
|
||
static String nl() { | ||
return NL; | ||
} | ||
} |