Compare commits

...

15 Commits

Author SHA1 Message Date
Louis Lam
06dc61b343
Merge pull request #2567 from clcninja/feature-google-analytics
Feature - Added Optional Google Analytics tag for Status Page.
2023-02-04 17:58:18 +08:00
Louis Lam
afadfe32d5 Trim 2023-02-04 17:03:00 +08:00
Louis Lam
5f2affb38c Relocate and fix jsesc issue 2023-02-04 16:58:39 +08:00
Louis Lam
10c6f3b688 Merge remote-tracking branch 'origin/master' into feature-google-analytics 2023-02-04 15:40:13 +08:00
c
a823ed8ccc Feature - Google Analytics - Removed unused import. 2023-02-03 11:49:25 +00:00
Louis Lam
f153082184 Drop en.js 2023-02-02 21:52:45 +00:00
c
913bb611d5 Feature - Google Analytics - Removed regex to validate a Google Analytics tag. 2023-02-02 21:52:45 +00:00
c
3afe8013ca Feature - Google Analytics - Change TEXT type to VARCHAR. 2023-02-02 21:52:45 +00:00
c
5a94a3fe3c Google Analytics - Moved string to updated file. 2023-02-02 21:52:44 +00:00
c
c08d8a5eaf Google Analytics - Simplified retrieving Tag ID from Status Page. 2023-02-02 21:51:03 +00:00
c
3ff0cbe311 Feature - Google Analytics - Simplified Module & Escaped the Script to prevent XXS. 2023-02-02 21:51:03 +00:00
c
fb2999706c Feature - Google Analytics - Added JSDoc to Google Analytics functions. 2023-02-02 21:51:03 +00:00
c
2b3a3895b3 Feature - Google Analytics - Use Regex to validate UA as per https://support.google.com/analytics/answer/9310895 2023-02-02 21:51:03 +00:00
c
99c0b8cb71 Feature - Google Analytics - Addressing PR Comments. 2023-02-02 21:51:03 +00:00
c
29e24e0de9 Feature - Added Optional Google Analytics tag for Status Page. 2023-02-02 21:51:03 +00:00
7 changed files with 46 additions and 1 deletions

View File

@ -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;

View File

@ -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,
};
/**

View File

@ -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 `
<script async src="https://www.googletagmanager.com/gtag/js?id=${escapedTagId}"></script>
<script>window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date());gtag('config', '${escapedTagId}'); </script>
`;
}
module.exports = {
getGoogleAnalyticsScript,
};

View File

@ -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(`<meta property="og:title" content="${statusPage.title}" />`);
head.append(`<meta property="og:description" content="${description155}" />`);
@ -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,
};
}

View File

@ -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);

View File

@ -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"
}

View File

@ -67,6 +67,12 @@
</ul>
</div>
<!-- Google Analytics -->
<div class="my-3">
<label for="googleAnalyticsTag" class="form-label">{{ $t("Google Analytics ID") }}</label>
<input id="googleAnalyticsTag" v-model="config.googleAnalyticsId" type="text" class="form-control">
</div>
<!-- Custom CSS -->
<div class="my-3">
<div class="mb-1">{{ $t("Custom CSS") }}</div>