Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for umami analytics in status page #5608

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions db/knex_init_db.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ async function createTables() {
table.text("custom_css");
table.boolean("show_powered_by").notNullable().defaultTo(true);
table.string("google_analytics_tag_id");
table.string("umami_analytics_domain_url");
table.string("umami_analytics_website_id");
Comment on lines +206 to +207
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remove this and the related parts and add it to a new migration, and only there (the .sql files are a crutch which we did not have the time/energy to clean up yet)

I think this file will only be run for new users, but not existing users.

Comment on lines +206 to +207
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding DB-Design as discussed below:
I think we should name this analytics_domain_url and analytics_id (rename from google_analytics_tag_id) instead.

});

// maintenance_status_page
Expand Down
10 changes: 10 additions & 0 deletions db/old_migrations/patch-add-umami-analytics-status-page.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-- You should not modify if this have pushed to Github, unless it does serious wrong with the db.
BEGIN TRANSACTION;

ALTER TABLE status_page
ADD umami_analytics_domain_url VARCHAR;

ALTER TABLE status_page
ADD umami_analytics_website_id VARCHAR;

COMMIT;
1 change: 1 addition & 0 deletions server/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ class Database {
"patch-maintenance-table2.sql": true,
"patch-add-gamedig-monitor.sql": true,
"patch-add-google-analytics-status-page-tag.sql": true,
"patch-add-umami-analytics-status-page.sql": true,
"patch-http-body-encoding.sql": true,
"patch-add-description-monitor.sql": true,
"patch-api-key-table.sql": true,
Expand Down
10 changes: 10 additions & 0 deletions server/model/status_page.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const cheerio = require("cheerio");
const { UptimeKumaServer } = require("../uptime-kuma-server");
const jsesc = require("jsesc");
const googleAnalytics = require("../google-analytics");
const umamiAnalytics = require("../umami-analytics");
const { marked } = require("marked");
const { Feed } = require("feed");
const config = require("../config");
Expand Down Expand Up @@ -125,6 +126,11 @@ class StatusPage extends BeanModel {
head.append($(escapedGoogleAnalyticsScript));
}

if (statusPage.umamiAnalyticsDomainUrl && statusPage.umamiAnalyticsWebsiteId) {
let escapedUmamiAnalyticsScript = umamiAnalytics.getUmamiAnalyticsScript(statusPage.umamiAnalyticsDomainUrl, statusPage.umamiAnalyticsWebsiteId);
head.append($(escapedUmamiAnalyticsScript));
}

// OG Meta Tags
let ogTitle = $("<meta property=\"og:title\" content=\"\" />").attr("content", statusPage.title);
head.append(ogTitle);
Expand Down Expand Up @@ -408,6 +414,8 @@ class StatusPage extends BeanModel {
footerText: this.footer_text,
showPoweredBy: !!this.show_powered_by,
googleAnalyticsId: this.google_analytics_tag_id,
umamiAnalyticsDomainUrl: this.umami_analytics_domain_url,
umamiAnalyticsWebsiteId: this.umami_analytics_website_id,
showCertificateExpiry: !!this.show_certificate_expiry,
};
}
Expand All @@ -431,6 +439,8 @@ class StatusPage extends BeanModel {
footerText: this.footer_text,
showPoweredBy: !!this.show_powered_by,
googleAnalyticsId: this.google_analytics_tag_id,
umamiAnalyticsDomainUrl: this.umami_analytics_domain_url,
umamiAnalyticsWebsiteId: this.umami_analytics_website_id,
showCertificateExpiry: !!this.show_certificate_expiry,
};
}
Expand Down
2 changes: 2 additions & 0 deletions server/socket-handlers/status-page-socket-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ module.exports.statusPageSocketHandler = (socket) => {
statusPage.show_certificate_expiry = config.showCertificateExpiry;
statusPage.modified_date = R.isoDateTime();
statusPage.google_analytics_tag_id = config.googleAnalyticsId;
statusPage.umami_analytics_domain_url = config.umamiAnalyticsDomainUrl;
statusPage.umami_analytics_website_id = config.umamiAnalyticsWebsiteId;

await R.store(statusPage);

Expand Down
36 changes: 36 additions & 0 deletions server/umami-analytics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const jsesc = require("jsesc");
const { escape } = require("html-escaper");

/**
* Returns a string that represents the javascript that is required to insert the Umami Analytics script
* into a webpage.
* @param {string} domainUrl Domain name with tld to use with the Umami Analytics script.
* @param {string} websiteId Website ID to use with the Umami Analytics script.
* @returns {string} HTML script tags to inject into page
*/
function getUmamiAnalyticsScript(domainUrl, websiteId) {
let escapedDomainUrlJS = jsesc(domainUrl, { isScriptContext: true });
let escapedWebsiteIdJS = jsesc(websiteId, { isScriptContext: true });

if (escapedDomainUrlJS) {
escapedDomainUrlJS = escapedDomainUrlJS.trim();
}

if (escapedWebsiteIdJS) {
escapedWebsiteIdJS = escapedWebsiteIdJS.trim();
}

// Escape the domain url for use in an HTML attribute.
let escapedDomainUrlHTMLAttribute = escape(escapedDomainUrlJS);

// Escape the website id for use in an HTML attribute.
let escapedWebsiteIdHTMLAttribute = escape(escapedWebsiteIdJS);

return `
<script defer src="https://${escapedDomainUrlHTMLAttribute}/script.js" data-website-id="${escapedWebsiteIdHTMLAttribute}"></script>
`;
}

module.exports = {
getUmamiAnalyticsScript,
};
2 changes: 2 additions & 0 deletions src/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,8 @@
"wayToGetClickSendSMSToken": "You can get API Username and API Key from {0} .",
"Custom Monitor Type": "Custom Monitor Type",
"Google Analytics ID": "Google Analytics ID",
"Umami Analytics Domain Url": "Umami Analytics Domain Url",
"Umami Analytics Website ID": "Umami Analytics Website ID",
"Edit Tag": "Edit Tag",
"Server Address": "Server Address",
"Learn More": "Learn More",
Expand Down
10 changes: 10 additions & 0 deletions src/pages/StatusPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,16 @@
<input id="googleAnalyticsTag" v-model="config.googleAnalyticsId" type="text" class="form-control" data-testid="google-analytics-input">
</div>

<!-- Umami Analytics -->
<div class="my-3">
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems like a bit too much clutter to me. 🤔
I don't think people generally configure both GA and Umami

=> lets hide them via an analytics selector (None, GA, Umami) where the GA settings are only present when that is selected.

<label for="umamiAnalyticsDomainUrl" class="form-label">{{ $t("Umami Analytics Domain Url") }}</label>
<input id="umamiAnalyticsDomainUrl" v-model="config.umamiAnalyticsDomainUrl" type="text" class="form-control" data-testid="umami-analytics-domain-url-input">
</div>
<div class="my-3">
<label for="umamiAnalyticsWebsite" class="form-label">{{ $t("Umami Analytics Website ID") }}</label>
<input id="umamiAnalyticsWebsite" v-model="config.umamiAnalyticsWebsiteId" type="text" class="form-control" data-testid="umami-analytics-website-id-input">
</div>

<!-- Custom CSS -->
<div class="my-3">
<div class="mb-1">{{ $t("Custom CSS") }}</div>
Expand Down
6 changes: 6 additions & 0 deletions test/e2e/specs/status-page.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ test.describe("Status Page", () => {
const refreshInterval = 30;
const theme = "dark";
const googleAnalyticsId = "G-123";
const umamiAnalyticsDomainUrl = "example.com";
const umamiAnalyticsWebsiteId = "606487e2-bc25-45f9-9132-fa8b065aad46";
const customCss = "body { background: rgb(0, 128, 128) !important; }";
const descriptionText = "This is an example status page.";
const incidentTitle = "Example Outage Incident";
Expand Down Expand Up @@ -58,6 +60,8 @@ test.describe("Status Page", () => {
await page.getByTestId("show-powered-by-checkbox").uncheck();
await page.getByTestId("show-certificate-expiry-checkbox").uncheck();
await page.getByTestId("google-analytics-input").fill(googleAnalyticsId);
await page.getByTestId("umami-analytics-domain-url-input").fill(umamiAnalyticsDomainUrl);
await page.getByTestId("umami-analytics-website-id-input").fill(umamiAnalyticsWebsiteId);
await page.getByTestId("custom-css-input").getByTestId("textarea").fill(customCss); // Prism
await expect(page.getByTestId("description-editable")).toHaveText(descriptionText);
await expect(page.getByTestId("custom-footer-editable")).toHaveText(footerText);
Expand Down Expand Up @@ -101,6 +105,8 @@ test.describe("Status Page", () => {

await expect(page.locator("body")).toHaveClass(theme);
expect(await page.locator("head").innerHTML()).toContain(googleAnalyticsId);
expect(await page.locator("head").innerHTML()).toContain(umamiAnalyticsDomainUrl);
expect(await page.locator("head").innerHTML()).toContain(umamiAnalyticsWebsiteId);

const backgroundColor = await page.evaluate(() => window.getComputedStyle(document.body).backgroundColor);
expect(backgroundColor).toEqual("rgb(0, 128, 128)");
Expand Down
Loading