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

Feature - Added Optional Google Analytics tag for Status Page. #2567

Merged
merged 14 commits into from
Feb 4, 2023
Merged
Show file tree
Hide file tree
Changes from 10 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
4 changes: 4 additions & 0 deletions db/patch-add-google-analytics-status-page-tag.sql
Original file line number Diff line number Diff line change
@@ -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;
1 change: 1 addition & 0 deletions server/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

/**
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 Maintenance = require("./maintenance");
const googleAnalytics = require("../modules/google-analytics");

class StatusPage extends BeanModel {

Expand Down Expand Up @@ -53,6 +54,13 @@ class StatusPage extends BeanModel {

const head = $("head");

if (statusPage.googleAnalyticsTagId) {
let escapedGoogleAnalyticsScript = jsesc(googleAnalytics.getGoogleAnalyticsScript(statusPage.googleAnalyticsTagId), {
Copy link
Owner

@louislam louislam Feb 4, 2023

Choose a reason for hiding this comment

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

I think jsesc is wrongly used, as getGoogleAnalyticsScript() returns with <script> tag which is not purely a javascript. I will fix it

"isScriptContext": true
});
head.append($(escapedGoogleAnalyticsScript));
}

// OG Meta Tags
head.append(`<meta property="og:title" content="${statusPage.title}" />`);
head.append(`<meta property="og:description" content="${description155}" />`);
Expand Down Expand Up @@ -225,6 +233,7 @@ class StatusPage extends BeanModel {
customCSS: this.custom_css,
footerText: this.footer_text,
showPoweredBy: !!this.show_powered_by,
googleAnalyticsId: this.google_analytics_tag_id,
};
}

Expand All @@ -245,6 +254,7 @@ class StatusPage extends BeanModel {
customCSS: this.custom_css,
footerText: this.footer_text,
showPoweredBy: !!this.show_powered_by,
googleAnalyticsId: this.google_analytics_tag_id,
};
}

Expand Down
14 changes: 14 additions & 0 deletions server/modules/google-analytics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* 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) {
return "<script async src=\"https://www.googletagmanager.com/gtag/js?id=" + tagId + "\"></script>" +
"<script>window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date());gtag('config', '" + tagId + "'); </script>";
}

module.exports = {
getGoogleAnalyticsScript,
};
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 @@ -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");
louislam marked this conversation as resolved.
Show resolved Hide resolved

/**
* Socket handlers for status page
Expand Down Expand Up @@ -163,6 +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;

await R.store(statusPage);

Expand Down
3 changes: 2 additions & 1 deletion src/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -690,5 +690,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"
}
6 changes: 6 additions & 0 deletions src/pages/StatusPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,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>
Expand Down