Skip to content

Commit 98f26fd

Browse files
committed
GA4 requires special format of client_id
1 parent d4449a3 commit 98f26fd

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

src/net/azib/ipscan/config/Config.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package net.azib.ipscan.config;
22

33
import java.util.Locale;
4-
import java.util.UUID;
4+
import java.util.Random;
55
import java.util.prefs.Preferences;
66

77
/**
@@ -13,7 +13,7 @@
1313
public final class Config {
1414
private Preferences preferences;
1515
public String language;
16-
public String uuid;
16+
public String gaClientId;
1717
public boolean allowReports;
1818

1919
/** easily accessible scanner configuration */
@@ -32,10 +32,13 @@ public final class Config {
3232
favoritesConfig = new FavoritesConfig(preferences);
3333
openersConfig = new OpenersConfig(preferences);
3434
language = preferences.get("language", "system");
35-
uuid = preferences.get("uuid", null);
36-
if (uuid == null) {
37-
uuid = UUID.randomUUID().toString();
38-
preferences.put("uuid", uuid);
35+
gaClientId = preferences.get("gaClientId", null);
36+
if (gaClientId == null) {
37+
Random random = new Random();
38+
long firstPart = 1000000000L + (long)(random.nextDouble() * 9000000000L);
39+
long secondPart = 1000000000L + (long)(random.nextDouble() * 9000000000L);
40+
gaClientId = firstPart + "." + secondPart;
41+
preferences.put("gaClientId", gaClientId);
3942
}
4043
allowReports = preferences.getBoolean("allowReports", true);
4144
}
@@ -50,7 +53,6 @@ public static Config getConfig() {
5053

5154
public void store() {
5255
preferences.put("language", language);
53-
preferences.put("uuid", uuid);
5456
preferences.putBoolean("allowReports", allowReports);
5557
scannerConfig.store();
5658
guiConfig.store();
@@ -103,7 +105,7 @@ private Locale createLocale(String locale) {
103105
return Locale.forLanguageTag(locale.replace('_', '-'));
104106
}
105107

106-
public String getUUID() {
107-
return uuid;
108+
public String getGaClientId() {
109+
return gaClientId;
108110
}
109111
}

src/net/azib/ipscan/util/GoogleAnalytics.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import java.net.HttpURLConnection;
99
import java.net.URL;
10+
import java.net.URLEncoder;
1011
import java.util.logging.Logger;
1112

1213
import static java.nio.charset.StandardCharsets.UTF_8;
@@ -19,7 +20,7 @@
1920
*/
2021
public class GoogleAnalytics {
2122
public void report(String screen) {
22-
report("screen_view", screen);
23+
report("page_view", screen);
2324
}
2425

2526
public void report(String type, String content) {
@@ -34,7 +35,7 @@ public void report(String type, String content) {
3435

3536
var payload = new StringBuilder();
3637
payload.append("{");
37-
payload.append("\"client_id\":\"").append(config.getUUID()).append("\",");
38+
payload.append("\"client_id\":\"").append(config.getGaClientId()).append("\",");
3839
payload.append("\"non_personalized_ads\":true,");
3940
payload.append("\"events\":[{");
4041
payload.append("\"name\":\"").append(type).append("\",");
@@ -51,8 +52,8 @@ public void report(String type, String content) {
5152
payload.append(",\"description\":\"").append(content).append("\"");
5253
payload.append(",\"fatal\":false");
5354
} else {
54-
payload.append(",\"firebase_screen\":\"").append(content).append("\"");
55-
payload.append(",\"firebase_screen_class\":\"").append("MainActivity").append("\"");
55+
payload.append(",\"page_title\":\"").append(content).append("\"");
56+
payload.append(",\"page_location\":\"").append("https://angryip.org/app/").append(URLEncoder.encode(content, UTF_8)).append("\"");
5657
}
5758

5859
payload.append("}}]");
@@ -64,7 +65,7 @@ public void report(String type, String content) {
6465
}
6566

6667
try (var is = conn.getInputStream()) {
67-
LoggerFactory.getLogger().info(new String(is.readAllBytes()));
68+
is.readAllBytes();
6869
}
6970

7071
conn.disconnect();

0 commit comments

Comments
 (0)