check if plausible is loaded before calling it

Some ad-blockers block plausible's script from loading, in which case
the `plausible` function is not defined.
We define a wrapper function that check if `plausible` is defined and if
so calls it.
This commit is contained in:
Yuri Iozzelli 2025-03-11 15:49:14 +01:00 committed by Alessandro Pignotti
parent fa6a7772da
commit 379f9f3033
5 changed files with 18 additions and 8 deletions

View File

@ -10,6 +10,7 @@
import { cpuActivity, diskActivity, cpuPercentage, diskLatency } from '$lib/activities.js' import { cpuActivity, diskActivity, cpuPercentage, diskLatency } from '$lib/activities.js'
import { introMessage, errorMessage, unexpectedErrorMessage } from '$lib/messages.js' import { introMessage, errorMessage, unexpectedErrorMessage } from '$lib/messages.js'
import { displayConfig } from '$lib/anthropic.js' import { displayConfig } from '$lib/anthropic.js'
import { tryPlausible } from '$lib/plausible.js'
export let configObj = null; export let configObj = null;
export let processCallback = null; export let processCallback = null;
@ -237,7 +238,7 @@
// Raise the display to the foreground // Raise the display to the foreground
const display = document.getElementById("display"); const display = document.getElementById("display");
display.parentElement.style.zIndex = 5; display.parentElement.style.zIndex = 5;
plausible("Display activated"); tryPlausible("Display activated");
} }
function handleProcessCreated() function handleProcessCreated()
{ {
@ -263,7 +264,7 @@
if(configObj.diskImageUrl.startsWith(wssProtocol)) if(configObj.diskImageUrl.startsWith(wssProtocol))
{ {
// WebSocket protocol failed, try agin using plain HTTP // WebSocket protocol failed, try agin using plain HTTP
plausible("WS Disk failure"); tryPlausible("WS Disk failure");
blockDevice = await CheerpX.CloudDevice.create("https:" + configObj.diskImageUrl.substr(wssProtocol.length)); blockDevice = await CheerpX.CloudDevice.create("https:" + configObj.diskImageUrl.substr(wssProtocol.length));
} }
else else

View File

@ -1,6 +1,7 @@
import { get, writable } from 'svelte/store'; import { get, writable } from 'svelte/store';
import { browser } from '$app/environment' import { browser } from '$app/environment'
import { aiActivity } from '$lib/activities.js' import { aiActivity } from '$lib/activities.js'
import { tryPlausible } from '$lib/plausible.js';
import Anthropic from '@anthropic-ai/sdk'; import Anthropic from '@anthropic-ai/sdk';
@ -16,7 +17,7 @@ export function setApiKey(key)
messageList.set(messages); messageList.set(messages);
localStorage.setItem("anthropic-api-key", key); localStorage.setItem("anthropic-api-key", key);
apiState.set("READY"); apiState.set("READY");
plausible("ClaudeAI Key"); tryPlausible("ClaudeAI Key");
} }
function clearApiKey() function clearApiKey()
@ -131,7 +132,7 @@ export function addMessage(text, handleTool)
{ {
addMessageInternal('user', text); addMessageInternal('user', text);
sendMessages(handleTool); sendMessages(handleTool);
plausible("ClaudeAI Use"); tryPlausible("ClaudeAI Use");
} }
export function clearMessageHistory() { export function clearMessageHistory() {

6
src/lib/plausible.js Normal file
View File

@ -0,0 +1,6 @@
// Some ad-blockers block the plausible script from loading. Check if `plausible`
// is defined before calling it.
export function tryPlausible(msg) {
if (plausible)
plausible(msg)
}

View File

@ -1,12 +1,13 @@
<script> <script>
import WebVM from '$lib/WebVM.svelte'; import WebVM from '$lib/WebVM.svelte';
import * as configObj from '/config_terminal' import * as configObj from '/config_terminal';
import { tryPlausible } from '$lib/plausible.js';
function handleProcessCreated(processCount) function handleProcessCreated(processCount)
{ {
// Log the first 5 processes, to get an idea of the level of interaction from the public // Log the first 5 processes, to get an idea of the level of interaction from the public
if(processCount <= 5) if(processCount <= 5)
{ {
plausible(`Process started: ${processCount}`); tryPlausible(`Process started: ${processCount}`);
} }
} }
</script> </script>

View File

@ -1,12 +1,13 @@
<script> <script>
import WebVM from '$lib/WebVM.svelte'; import WebVM from '$lib/WebVM.svelte';
import * as configObj from '/config_public_alpine' import * as configObj from '/config_public_alpine';
import { tryPlausible } from '$lib/plausible.js';
function handleProcessCreated(processCount) function handleProcessCreated(processCount)
{ {
// Log only the first process, as a proxy for successful startup // Log only the first process, as a proxy for successful startup
if(processCount == 1) if(processCount == 1)
{ {
plausible("Alpine init"); tryPlausible("Alpine init");
} }
} }
</script> </script>