Skip to content

Commit

Permalink
fix: reducing the analytics chatter
Browse files Browse the repository at this point in the history
  • Loading branch information
meza committed Aug 20, 2023
1 parent cce8310 commit 4d63f16
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
11 changes: 11 additions & 0 deletions src/client/java/gg/meza/SoundsBeGoneClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

import gg.meza.analytics.Analytics;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents;
import net.minecraft.world.WorldEvents;

import java.util.*;

Expand All @@ -26,6 +29,14 @@ public void onInitializeClient() {
SoundMap.entrySet().removeIf(entry -> new Date().getTime() - entry.getValue().getTime() > 60000);
tickCounter = 0;
});

ClientLifecycleEvents.CLIENT_STOPPING.register(client -> {
analytics.flush();
});

ClientPlayConnectionEvents.DISCONNECT.register((handler, client) -> {
analytics.flush();
});
}


Expand Down
42 changes: 37 additions & 5 deletions src/client/java/gg/meza/analytics/Analytics.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@
import net.minecraft.client.MinecraftClient;
import org.apache.commons.codec.digest.DigestUtils;

import java.util.HashMap;
import java.util.Map;
import static gg.meza.SoundsBeGone.LOGGER;

public class Analytics {
private static final String POSTHOG_API_KEY = "POSTHOG_API_KEY_REPL";
// private static final String POSTHOG_API_KEY = "POSTHOG_API_KEY_REPL";
private static final String POSTHOG_API_KEY = "phc_FwrcOlz7iwXh4IlKbFn7yawZ2LiZJ4ew0p5Yy2OoeRSL";
private static final String POSTHOG_HOST = "https://eu.posthog.com";
private PostHog posthog;
private final PostHog posthog;
private final Map<String, Integer> blockedSoundsCount = new HashMap<>();

// Not actually sending any user info, just using the uuid to create a new uuid that cannot be traced back to the user
private final String uuid = DigestUtils.sha256Hex(MinecraftClient.getInstance().getSession().getUsername());
Expand All @@ -25,31 +29,59 @@ public Analytics() {
this.sendEvent("Started Minecraft", "");
}
}

private void sendEvent(String event, String sound) {
this.sendEvent(event, sound, Map.of());
}

private void sendEvent(String event, String sound, Map<String, Object> extraProps) {
if (!SoundsBeGoneClient.config.isAnalyticsEnabled()) {
return;
}

this.posthog.capture(this.uuid, event, Map.of(
Map<String, Object> baseProps = new HashMap<>(Map.of(
"sound", sound,
"Minecraft Version", MC_VERSION,
"OS", OS_NAME,
"Local Time", new java.util.Date().toString(),
"ModVersion", SoundsBeGone.VERSION,
"Java Version", JAVA_VERSION
));

baseProps.putAll(extraProps);

this.posthog.capture(this.uuid, event, baseProps);
}

public void mutedSound(String sound) {
this.sendEvent("Muted Sound", sound);
}

public void blockedSound(String sound) {
this.sendEvent("Blocked Sound", sound);
if (this.blockedSoundsCount.containsKey(sound)) {
this.blockedSoundsCount.put(sound, this.blockedSoundsCount.get(sound) + 1);
} else {
this.blockedSoundsCount.put(sound, 1);
}

if(this.blockedSoundsCount.get(sound) > Integer.MAX_VALUE - 1000) {
this.sendEvent("Blocked Sound", sound, Map.of(
"count", this.blockedSoundsCount.get(sound)
));
this.blockedSoundsCount.remove(sound);
}
}

public void unmutedSound(String sound) {
this.sendEvent("UnMuted Sound", sound);
}

public void flush() {
LOGGER.debug("Flushing analytics");
this.blockedSoundsCount.forEach((sound, count) -> {
this.sendEvent("Blocked Sound", sound, Map.of(
"count", count
));
this.blockedSoundsCount.remove(sound);
});
}
}

0 comments on commit 4d63f16

Please sign in to comment.