diff --git a/db/patch-add-google-analytics-status-page-tag.sql b/db/patch-add-google-analytics-status-page-tag.sql new file mode 100644 index 000000000..5de6ff37b --- /dev/null +++ b/db/patch-add-google-analytics-status-page-tag.sql @@ -0,0 +1,4 @@ +-- 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 google_analytics_tag_id VARCHAR; +COMMIT; diff --git a/server/database.js b/server/database.js index 06b811537..19c09a00f 100644 --- a/server/database.js +++ b/server/database.js @@ -69,6 +69,7 @@ class Database { "patch-ping-packet-size.sql": true, "patch-maintenance-table2.sql": true, "patch-add-gamedig-monitor.sql": true, + "patch-add-google-analytics-status-page-tag.sql": true, }; /** diff --git a/server/google-analytics.js b/server/google-analytics.js new file mode 100644 index 000000000..fc9fbec84 --- /dev/null +++ b/server/google-analytics.js @@ -0,0 +1,24 @@ +const jsesc = require("jsesc"); + +/** + * Returns a string that represents the javascript that is required to insert the Google Analytics scripts + * into a webpage. + * @param tagId Google UA/G/AW/DC Property ID to use with the Google Analytics script. + * @returns {string} + */ +function getGoogleAnalyticsScript(tagId) { + let escapedTagId = jsesc(tagId, { isScriptContext: true }); + + if (escapedTagId) { + escapedTagId = escapedTagId.trim(); + } + + return ` + + + `; +} + +module.exports = { + getGoogleAnalyticsScript, +}; diff --git a/server/model/status_page.js b/server/model/status_page.js index 0dabf5ab1..d7185a2e0 100644 --- a/server/model/status_page.js +++ b/server/model/status_page.js @@ -4,6 +4,7 @@ const cheerio = require("cheerio"); const { UptimeKumaServer } = require("../uptime-kuma-server"); const jsesc = require("jsesc"); const Maintenance = require("./maintenance"); +const googleAnalytics = require("../google-analytics"); class StatusPage extends BeanModel { @@ -53,6 +54,11 @@ class StatusPage extends BeanModel { const head = $("head"); + if (statusPage.googleAnalyticsTagId) { + let escapedGoogleAnalyticsScript = googleAnalytics.getGoogleAnalyticsScript(statusPage.googleAnalyticsTagId); + head.append($(escapedGoogleAnalyticsScript)); + } + // OG Meta Tags head.append(``); head.append(``); @@ -225,6 +231,7 @@ class StatusPage extends BeanModel { customCSS: this.custom_css, footerText: this.footer_text, showPoweredBy: !!this.show_powered_by, + googleAnalyticsId: this.google_analytics_tag_id, }; } @@ -245,6 +252,7 @@ class StatusPage extends BeanModel { customCSS: this.custom_css, footerText: this.footer_text, showPoweredBy: !!this.show_powered_by, + googleAnalyticsId: this.google_analytics_tag_id, }; } diff --git a/server/socket-handlers/status-page-socket-handler.js b/server/socket-handlers/status-page-socket-handler.js index 16d6ee73b..717aba9c4 100644 --- a/server/socket-handlers/status-page-socket-handler.js +++ b/server/socket-handlers/status-page-socket-handler.js @@ -163,6 +163,7 @@ module.exports.statusPageSocketHandler = (socket) => { statusPage.custom_css = config.customCSS; statusPage.show_powered_by = config.showPoweredBy; statusPage.modified_date = R.isoDateTime(); + statusPage.google_analytics_tag_id = config.googleAnalyticsId; await R.store(statusPage); diff --git a/src/lang/en.json b/src/lang/en.json index 368c64e05..8a195a2a9 100644 --- a/src/lang/en.json +++ b/src/lang/en.json @@ -691,5 +691,6 @@ "onebotSafetyTips": "For safety, must set access token", "PushDeer Key": "PushDeer Key", "wayToGetClickSendSMSToken": "You can get API Username and API Key from {0} .", - "Custom Monitor Type": "Custom Monitor Type" + "Custom Monitor Type": "Custom Monitor Type", + "Google Analytics ID": "Google Analytics ID" } diff --git a/src/pages/StatusPage.vue b/src/pages/StatusPage.vue index 3b89ed83a..dcee15e7b 100644 --- a/src/pages/StatusPage.vue +++ b/src/pages/StatusPage.vue @@ -67,6 +67,12 @@ + +