Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

feat: Custom script path for Plausible

Andras Bacsai24c655d7ef68
diff --git a/prisma/migrations/20220509130501_custom_plausible_script/migration.sql b/prisma/migrations/20220509130501_custom_plausible_script/migration.sql
new file mode 100644
index 0000000000..6c8c28ff4c
--- /dev/null
+++ b/prisma/migrations/20220509130501_custom_plausible_script/migration.sql
@@ -0,0 +1,24 @@
+-- RedefineTables
+PRAGMA foreign_keys=OFF;
+CREATE TABLE "new_PlausibleAnalytics" (
+ "id" TEXT NOT NULL PRIMARY KEY,
+ "email" TEXT,
+ "username" TEXT,
+ "password" TEXT NOT NULL,
+ "postgresqlUser" TEXT NOT NULL,
+ "postgresqlPassword" TEXT NOT NULL,
+ "postgresqlDatabase" TEXT NOT NULL,
+ "postgresqlPublicPort" INTEGER,
+ "secretKeyBase" TEXT,
+ "scriptName" TEXT NOT NULL DEFAULT 'plausible.js',
+ "serviceId" TEXT NOT NULL,
+ "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
+ "updatedAt" DATETIME NOT NULL,
+ CONSTRAINT "PlausibleAnalytics_serviceId_fkey" FOREIGN KEY ("serviceId") REFERENCES "Service" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
+);
+INSERT INTO "new_PlausibleAnalytics" ("createdAt", "email", "id", "password", "postgresqlDatabase", "postgresqlPassword", "postgresqlPublicPort", "postgresqlUser", "secretKeyBase", "serviceId", "updatedAt", "username") SELECT "createdAt", "email", "id", "password", "postgresqlDatabase", "postgresqlPassword", "postgresqlPublicPort", "postgresqlUser", "secretKeyBase", "serviceId", "updatedAt", "username" FROM "PlausibleAnalytics";
+DROP TABLE "PlausibleAnalytics";
+ALTER TABLE "new_PlausibleAnalytics" RENAME TO "PlausibleAnalytics";
+CREATE UNIQUE INDEX "PlausibleAnalytics_serviceId_key" ON "PlausibleAnalytics"("serviceId");
+PRAGMA foreign_key_check;
+PRAGMA foreign_keys=ON;
diff --git a/prisma/schema.prisma b/prisma/schema.prisma
index db5b89cba8..77fd59d4c0 100644
--- a/prisma/schema.prisma
+++ b/prisma/schema.prisma
@@ -322,6 +322,7 @@ model PlausibleAnalytics {
postgresqlDatabase String
postgresqlPublicPort Int?
secretKeyBase String?
+ scriptName String @default("plausible.js")
serviceId String @unique
service Service @relation(fields: [serviceId], references: [id])
createdAt DateTime @default(now())
diff --git a/src/lib/database/services.ts b/src/lib/database/services.ts
index 99bbf224c5..cc2ab5152e 100644
--- a/src/lib/database/services.ts
+++ b/src/lib/database/services.ts
@@ -329,7 +329,8 @@ export async function updatePlausibleAnalyticsService({
email,
exposePort,
username,
- name
+ name,
+ scriptName
}: {
id: string;
fqdn: string;
@@ -337,8 +338,12 @@ export async function updatePlausibleAnalyticsService({
name: string;
email: string;
username: string;
+ scriptName: string;
}): Promise<void> {
- await prisma.plausibleAnalytics.update({ where: { serviceId: id }, data: { email, username } });
+ await prisma.plausibleAnalytics.update({
+ where: { serviceId: id },
+ data: { email, username, scriptName }
+ });
await prisma.service.update({ where: { id }, data: { name, fqdn, exposePort } });
}
diff --git a/src/lib/haproxy/configuration.ts b/src/lib/haproxy/configuration.ts
index 745e8f4af4..eb6b7ef00b 100644
--- a/src/lib/haproxy/configuration.ts
+++ b/src/lib/haproxy/configuration.ts
@@ -55,6 +55,9 @@ frontend http
http-request redirect location {{{redirectValue}}} code ${
dev ? 302 : 301
} if { req.hdr(host) -i {{redirectTo}} }
+ {{#scriptName}}
+ http-request set-path /js/plausible.js if { hdr(host) -i {{domain}} } { path_beg -i /js/{{scriptName}} }
+ {{/scriptName}}
{{/services}}
{{#coolify}}
@@ -218,7 +221,15 @@ export async function configureHAProxy(): Promise<void> {
const services = await listServicesWithIncludes();
for (const service of services) {
- const { fqdn, id, type, destinationDocker, destinationDockerId, updatedAt } = service;
+ const {
+ fqdn,
+ id,
+ type,
+ destinationDocker,
+ destinationDockerId,
+ updatedAt,
+ plausibleAnalytics
+ } = service;
if (destinationDockerId) {
const { engine } = destinationDocker;
const found = supportedServiceTypesAndVersions.find((a) => a.name === type);
@@ -232,6 +243,12 @@ export async function configureHAProxy(): Promise<void> {
const isWWW = fqdn.includes('www.');
const redirectValue = `${isHttps ? 'https://' : 'http://'}${domain}%[capture.req.uri]`;
if (isRunning) {
+ // Plausible Analytics custom script
+ let scriptName = false;
+ if (type === 'plausibleanalytics' && plausibleAnalytics.scriptName !== 'plausible.js') {
+ scriptName = plausibleAnalytics.scriptName;
+ }
+
data.services.push({
id,
port,
@@ -241,7 +258,8 @@ export async function configureHAProxy(): Promise<void> {
isHttps,
redirectValue,
redirectTo: isWWW ? domain.replace('www.', '') : 'www.' + domain,
- updatedAt: updatedAt.getTime()
+ updatedAt: updatedAt.getTime(),
+ scriptName
});
}
}
diff --git a/src/routes/services/[id]/_Services/_PlausibleAnalytics.svelte b/src/routes/services/[id]/_Services/_PlausibleAnalytics.svelte
index 26b11c6f7d..90a923dff6 100644
--- a/src/routes/services/[id]/_Services/_PlausibleAnalytics.svelte
+++ b/src/routes/services/[id]/_Services/_PlausibleAnalytics.svelte
@@ -1,13 +1,32 @@
<script lang="ts">
+ import { session } from '$app/stores';
+
import CopyPasswordField from '$lib/components/CopyPasswordField.svelte';
+ import Explainer from '$lib/components/Explainer.svelte';
import { t } from '$lib/translations';
export let service;
export let readOnly;
+ export let isRunning;
</script>
<div class="flex space-x-1 py-5 font-bold">
<div class="title">Plausible Analytics</div>
</div>
+<div class="grid grid-cols-2 items-center px-10">
+ <label for="scriptName">Script Name</label>
+ <input
+ name="scriptName"
+ id="scriptName"
+ readonly={!$session.isAdmin && !isRunning}
+ disabled={!$session.isAdmin || isRunning}
+ placeholder="plausible.js"
+ bind:value={service.plausibleAnalytics.scriptName}
+ required
+ />
+ <Explainer
+ text="Useful if you would like to rename the collector script to prevent it blocked by AdBlockers."
+ />
+</div>
<div class="grid grid-cols-2 items-center px-10">
<label for="email">{$t('forms.email')}</label>
<input
diff --git a/src/routes/services/[id]/_Services/_Services.svelte b/src/routes/services/[id]/_Services/_Services.svelte
index a34f140f0f..519a2cb6ce 100644
--- a/src/routes/services/[id]/_Services/_Services.svelte
+++ b/src/routes/services/[id]/_Services/_Services.svelte
@@ -188,7 +188,7 @@
{/if}
{#if service.type === 'plausibleanalytics'}
- <PlausibleAnalytics bind:service {readOnly} />
+ <PlausibleAnalytics bind:service {isRunning} {readOnly} />
{:else if service.type === 'minio'}
<MinIo {service} />
{:else if service.type === 'vscodeserver'}
diff --git a/src/routes/services/[id]/plausibleanalytics/index.json.ts b/src/routes/services/[id]/plausibleanalytics/index.json.ts
index 0a5d375e7a..aeaf655ce9 100644
--- a/src/routes/services/[id]/plausibleanalytics/index.json.ts
+++ b/src/routes/services/[id]/plausibleanalytics/index.json.ts
@@ -12,15 +12,28 @@ export const post: RequestHandler = async (event) => {
name,
fqdn,
exposePort,
- plausibleAnalytics: { email, username }
+ plausibleAnalytics: { email, username, scriptName }
} = await event.request.json();
if (fqdn) fqdn = fqdn.toLowerCase();
if (email) email = email.toLowerCase();
if (exposePort) exposePort = Number(exposePort);
-
+ if (scriptName) {
+ scriptName = scriptName.toLowerCase();
+ if (scriptName.startsWith('/')) {
+ scriptName = scriptName.replaceAll(/\//gi, '');
+ }
+ }
try {
- await db.updatePlausibleAnalyticsService({ id, fqdn, name, email, username, exposePort });
+ await db.updatePlausibleAnalyticsService({
+ id,
+ fqdn,
+ name,
+ email,
+ username,
+ exposePort,
+ scriptName
+ });
return { status: 201 };
} catch (error) {
return ErrorHandler(error);