-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Add support for umami analytics in status page #5608
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
Open
hadestructhor
wants to merge
11
commits into
louislam:master
Choose a base branch
from
hadestructhor:feature/umami-analytics-status-page
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
bd118ea
Add support for umami analytics in status page
hadestructhor afae736
feat: add support for umami tracking
hadestructhor e44ec55
chore: cleanup and refactoring
hadestructhor cda2ae5
Merge branch 'master' into feature/umami-analytics-status-page
hadestructhor 6bade1f
feat: add support for plausible, cleanup and refactor code
hadestructhor a1e6339
feat: add Matomo analytics support
hadestructhor c9a3cff
Merge branch 'master' into feature/umami-analytics-status-page
hadestructhor 0588acb
Merge branch 'master' into feature/umami-analytics-status-page
hadestructhor 28e288d
fix(analytics): fixed issues with db init and refactor of code and names
hadestructhor acb0968
Merge branch 'master' into feature/umami-analytics-status-page
hadestructhor 62f828a
Merge branch 'master' into feature/umami-analytics-status-page
hadestructhor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
db/knex_migrations/2025-02-17-2142-generalize-analytics.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Udpate status_page table to generalize analytics fields | ||
exports.up = function (knex) { | ||
return knex.schema | ||
.alterTable("status_page", function (table) { | ||
table.renameColumn("google_analytics_tag_id", "analytics_id"); | ||
table.string("analytics_script_url"); | ||
table.enu("analytics_type", [ "google", "umami", "plausible", "matomo" ]).defaultTo(null); | ||
|
||
}).then(() => { | ||
// After a succesful migration, add google as default for previous pages | ||
knex("status_page").whereNotNull("analytics_id").update({ | ||
"analytics_type": "google", | ||
}); | ||
}); | ||
}; | ||
|
||
exports.down = function (knex) { | ||
return knex.schema.alterTable("status_page", function (table) { | ||
table.renameColumn("analytics_id", "google_analytics_tag_id"); | ||
table.dropColumn("analytics_script_url"); | ||
table.dropColumn("analytics_type"); | ||
}); | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
const googleAnalytics = require("./google-analytics"); | ||
const umamiAnalytics = require("./umami-analytics"); | ||
const plausibleAnalytics = require("./plausible-analytics"); | ||
const matomoAnalytics = require("./matomo-analytics"); | ||
|
||
/** | ||
* Returns a string that represents the javascript that is required to insert the selected Analytics' script | ||
* into a webpage. | ||
* @param {typeof import("../model/status_page").StatusPage} statusPage Status page populate HTML with | ||
* @returns {string} HTML script tags to inject into page | ||
*/ | ||
function getAnalyticsScript(statusPage) { | ||
switch (statusPage.analyticsType) { | ||
case "google": | ||
return googleAnalytics.getGoogleAnalyticsScript(statusPage.analyticsId); | ||
case "umami": | ||
return umamiAnalytics.getUmamiAnalyticsScript(statusPage.analyticsScriptUrl, statusPage.analyticsId); | ||
case "plausible": | ||
return plausibleAnalytics.getPlausibleAnalyticsScript(statusPage.analyticsScriptUrl, statusPage.analyticsId); | ||
case "matomo": | ||
return matomoAnalytics.getMatomoAnalyticsScript(statusPage.analyticsScriptUrl, statusPage.analyticsId); | ||
default: | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* Function that checks wether the selected analytics has been configured properly | ||
* @param {typeof import("../model/status_page").StatusPage} statusPage Status page populate HTML with | ||
* @returns {boolean} Boolean defining if the analytics config is valid | ||
*/ | ||
function isValidAnalyticsConfig(statusPage) { | ||
switch (statusPage.analyticsType) { | ||
case "google": | ||
return statusPage.analyticsId != null; | ||
case "umami": | ||
case "plausible": | ||
case "matomo": | ||
return statusPage.analyticsId != null && statusPage.analyticsScriptUrl != null; | ||
default: | ||
return false; | ||
} | ||
} | ||
|
||
module.exports = { | ||
getAnalyticsScript, | ||
isValidAnalyticsConfig | ||
}; |
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
const jsesc = require("jsesc"); | ||
const { escape } = require("html-escaper"); | ||
|
||
/** | ||
* Returns a string that represents the javascript that is required to insert the Matomo Analytics script | ||
* into a webpage. | ||
* @param {string} matomoUrl Domain name with tld to use with the Matomo Analytics script. | ||
* @param {string} siteId Site ID to use with the Matomo Analytics script. | ||
* @returns {string} HTML script tags to inject into page | ||
*/ | ||
function getMatomoAnalyticsScript(matomoUrl, siteId) { | ||
let escapedMatomoUrlJS = jsesc(matomoUrl, { isScriptContext: true }); | ||
let escapedSiteIdJS = jsesc(siteId, { isScriptContext: true }); | ||
|
||
if (escapedMatomoUrlJS) { | ||
escapedMatomoUrlJS = escapedMatomoUrlJS.trim(); | ||
} | ||
|
||
if (escapedSiteIdJS) { | ||
escapedSiteIdJS = escapedSiteIdJS.trim(); | ||
} | ||
|
||
// Escape the domain url for use in an HTML attribute. | ||
let escapedMatomoUrlHTMLAttribute = escape(escapedMatomoUrlJS); | ||
|
||
// Escape the website id for use in an HTML attribute. | ||
let escapedSiteIdHTMLAttribute = escape(escapedSiteIdJS); | ||
|
||
return ` | ||
<script type="text/javascript"> | ||
var _paq = window._paq = window._paq || []; | ||
_paq.push(['trackPageView']); | ||
_paq.push(['enableLinkTracking']); | ||
(function() { | ||
var u="//${escapedMatomoUrlHTMLAttribute}/"; | ||
_paq.push(['setTrackerUrl', u+'matomo.php']); | ||
_paq.push(['setSiteId', ${escapedSiteIdHTMLAttribute}]); | ||
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; | ||
g.type='text/javascript'; g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s); | ||
})(); | ||
</script> | ||
`; | ||
} | ||
|
||
module.exports = { | ||
getMatomoAnalyticsScript, | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 Plausible Analytics script | ||
* into a webpage. | ||
* @param {string} scriptUrl the Plausible Analytics script url. | ||
* @param {string} domainsToMonitor Domains to track seperated by a ',' to add Plausible Analytics script. | ||
* @returns {string} HTML script tags to inject into page | ||
*/ | ||
function getPlausibleAnalyticsScript(scriptUrl, domainsToMonitor) { | ||
let escapedScriptUrlJS = jsesc(scriptUrl, { isScriptContext: true }); | ||
let escapedWebsiteIdJS = jsesc(domainsToMonitor, { isScriptContext: true }); | ||
|
||
if (escapedScriptUrlJS) { | ||
escapedScriptUrlJS = escapedScriptUrlJS.trim(); | ||
} | ||
|
||
if (escapedWebsiteIdJS) { | ||
escapedWebsiteIdJS = escapedWebsiteIdJS.trim(); | ||
} | ||
|
||
// Escape the domain url for use in an HTML attribute. | ||
let escapedScriptUrlHTMLAttribute = escape(escapedScriptUrlJS); | ||
|
||
// Escape the website id for use in an HTML attribute. | ||
let escapedWebsiteIdHTMLAttribute = escape(escapedWebsiteIdJS); | ||
|
||
return ` | ||
<script defer src="${escapedScriptUrlHTMLAttribute}" data-domain="${escapedWebsiteIdHTMLAttribute}"></script> | ||
`; | ||
} | ||
|
||
module.exports = { | ||
getPlausibleAnalyticsScript | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} scriptUrl the Umami Analytics script url. | ||
* @param {string} websiteId Website ID to use with the Umami Analytics script. | ||
* @returns {string} HTML script tags to inject into page | ||
*/ | ||
function getUmamiAnalyticsScript(scriptUrl, websiteId) { | ||
let escapedScriptUrlJS = jsesc(scriptUrl, { isScriptContext: true }); | ||
let escapedWebsiteIdJS = jsesc(websiteId, { isScriptContext: true }); | ||
|
||
if (escapedScriptUrlJS) { | ||
escapedScriptUrlJS = escapedScriptUrlJS.trim(); | ||
} | ||
|
||
if (escapedWebsiteIdJS) { | ||
escapedWebsiteIdJS = escapedWebsiteIdJS.trim(); | ||
} | ||
|
||
// Escape the Script url for use in an HTML attribute. | ||
let escapedScriptUrlHTMLAttribute = escape(escapedScriptUrlJS); | ||
|
||
// Escape the website id for use in an HTML attribute. | ||
let escapedWebsiteIdHTMLAttribute = escape(escapedWebsiteIdJS); | ||
|
||
return ` | ||
<script defer src="${escapedScriptUrlHTMLAttribute}" data-website-id="${escapedWebsiteIdHTMLAttribute}"></script> | ||
`; | ||
} | ||
|
||
module.exports = { | ||
getUmamiAnalyticsScript, | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.