From 2b3a3895b399c606e65aca83e5ebc4376ba86b02 Mon Sep 17 00:00:00 2001 From: c Date: Wed, 11 Jan 2023 21:44:31 +0000 Subject: [PATCH] Feature - Google Analytics - Use Regex to validate UA as per https://support.google.com/analytics/answer/9310895 --- server/modules/google-analytics.js | 7 ++++++- server/socket-handlers/status-page-socket-handler.js | 3 ++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/server/modules/google-analytics.js b/server/modules/google-analytics.js index 55820a857..0a40076d3 100644 --- a/server/modules/google-analytics.js +++ b/server/modules/google-analytics.js @@ -3,8 +3,13 @@ let GoogleAnalytics = (() => { return "" + ""; } + function isValidTag(tagInput) { + const re = /^\w{1,2}-\d{8}$/g; + return tagInput.match(re) != null; + } return { - getGoogleAnalyticsScript: getGoogleAnalyticsScript + getGoogleAnalyticsScript: getGoogleAnalyticsScript, + isValidTag: isValidTag }; })(); diff --git a/server/socket-handlers/status-page-socket-handler.js b/server/socket-handlers/status-page-socket-handler.js index 717aba9c4..359ed15bd 100644 --- a/server/socket-handlers/status-page-socket-handler.js +++ b/server/socket-handlers/status-page-socket-handler.js @@ -7,6 +7,7 @@ const Database = require("../database"); const apicache = require("../modules/apicache"); const StatusPage = require("../model/status_page"); const { UptimeKumaServer } = require("../uptime-kuma-server"); +const googleAnalytics = require("../modules/google-analytics"); /** * Socket handlers for status page @@ -163,7 +164,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; + statusPage.google_analytics_tag_id = googleAnalytics.isValidTag(config.googleAnalyticsId) ? config.googleAnalyticsId : ""; await R.store(statusPage);