Skip to content

Commit

Permalink
feat: added analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
meza committed Jun 27, 2023
1 parent c8a04c0 commit 4733e93
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,13 @@ jobs:
id: get-next-version
env:
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
POSTHOG_API_KEY: ${{ secrets.POSTHOG_API_KEY }}

- name: Release
if: ${{ runner.os == 'Linux' && matrix.java == '17' && (steps.get-next-version.outputs.new-release-published == true) }}
env:
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
POSTHOG_API_KEY: ${{ secrets.POSTHOG_API_KEY }}
run: npx semantic-release

- name: capture build artifacts
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
secrets.gradle
# gradle

.gradle/
Expand Down
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ dependencies {
// modImplementation "net.fabricmc.fabric-api:fabric-api-deprecated:${project.fabric_version}"

implementation 'com.google.code.gson:gson:2.10.1'
implementation 'com.posthog.java:posthog:+'

modApi("com.terraformersmc:modmenu:${project.modmenu_version}") {
exclude(group: "net.fabricmc.fabric-api")
Expand Down
2 changes: 2 additions & 0 deletions scripts/release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ VERSION=$1

echo "Replacing version with ${VERSION}"
sed -e "s/VERSION/${VERSION}/" -i gradle.properties
sed -e "s/VERSION_REPL/${VERSION}/" -i src/main/java/gg/meza/SoundsBeGone.java
sed -e "s/POSTHOG_API_KEY_REPL/${POSTHOG_API_KEY}/" -i src/client/java/gg/meza/analytics/Analytics.java

./gradlew build -x test # we've ran the tests earlier in the build
2 changes: 2 additions & 0 deletions src/client/java/gg/meza/ModMenuIntegration.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,10 @@ private AbstractConfigListEntry constructOption(ConfigEntryBuilder builder, Stri
.setSaveConsumer(newValue -> {
if (newValue) {
SoundsBeGoneClient.DisabledSoundMap.add(key);
SoundsBeGoneClient.analytics.mutedSound(key);
} else {
SoundsBeGoneClient.DisabledSoundMap.remove(key);
SoundsBeGoneClient.analytics.unmutedSound(key);
}
})
.setYesNoTextSupplier(bool -> bool ? Text.of("Enable") : Text.of("Disable"))
Expand Down
2 changes: 2 additions & 0 deletions src/client/java/gg/meza/SoundsBeGoneClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.gson.Gson;
import com.google.gson.JsonParseException;
import gg.meza.analytics.Analytics;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.loader.api.FabricLoader;
Expand All @@ -18,6 +19,7 @@ public class SoundsBeGoneClient implements ClientModInitializer {
public static Map<String, Date> SoundMap = new HashMap<String, Date>();
public static Set<String> DisabledSoundMap = new HashSet<>();
private int tickCounter = 0;
public static Analytics analytics = new Analytics();

@Override
public void onInitializeClient() {
Expand Down
50 changes: 50 additions & 0 deletions src/client/java/gg/meza/analytics/Analytics.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package gg.meza.analytics;

import com.posthog.java.PostHog;
import gg.meza.SoundsBeGone;
import net.minecraft.client.MinecraftClient;
import org.apache.commons.codec.digest.DigestUtils;

import java.util.HashMap;

public class Analytics {
private static final String POSTHOG_API_KEY = "POSTHOG_API_KEY_REPL";
private static final String POSTHOG_HOST = "https://eu.posthog.com";
private PostHog posthog;

// 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());
private final String OS_NAME = System.getProperty("os.name");
private final String MC_VERSION = MinecraftClient.getInstance().getGameVersion();
private final String JAVA_VERSION = System.getProperty("java.version");

public Analytics() {
this.posthog = new PostHog.Builder(POSTHOG_API_KEY).host(POSTHOG_HOST).build();
this.posthog.capture(this.uuid, "Started Minecraft");
}

private void sendEvent(String event, String sound) {
this.posthog.capture(this.uuid, event, new HashMap<String, Object>() {
{
put("sound", sound);
put("Minecraft Version", MC_VERSION);
put("OS", OS_NAME);
put("Local Time", new java.util.Date().toString());
put("ModVersion", SoundsBeGone.VERSION);
put("Java Version", JAVA_VERSION);
}
});
}

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

public void blockedSound(String sound) {
this.sendEvent("Blocked Sound", sound);
}

public void unmutedSound(String sound) {
this.sendEvent("UnMuted Sound", sound);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import net.minecraft.client.sound.SoundInstance;
import net.minecraft.client.sound.SoundSystem;
import net.minecraft.text.Text;
import net.minecraft.text.TranslatableTextContent;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
Expand All @@ -25,6 +24,7 @@ private void run(SoundInstance sound, CallbackInfo info) {

if (SoundsBeGoneClient.DisabledSoundMap.contains(id)) {
LOGGER.warn("Disabling the sound: {}", id);
SoundsBeGoneClient.analytics.blockedSound(id);
info.cancel();
}

Expand Down
1 change: 1 addition & 0 deletions src/main/java/gg/meza/SoundsBeGone.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class SoundsBeGone implements ModInitializer {
// It is considered best practice to use your mod id as the logger's name.
// That way, it's clear which mod wrote info, warnings, and errors.
public static final Logger LOGGER = LoggerFactory.getLogger("soundsbegone");
public static final String VERSION = "VERSION_REPL";

@Override
public void onInitialize() {
Expand Down

0 comments on commit 4733e93

Please sign in to comment.