This commit is contained in:
parent
ca6ba2eb51
commit
69386eed63
6 changed files with 53 additions and 3 deletions
14
package-lock.json
generated
14
package-lock.json
generated
|
|
@ -6,6 +6,9 @@
|
||||||
"": {
|
"": {
|
||||||
"name": "schmelczer-dev",
|
"name": "schmelczer-dev",
|
||||||
"license": "GPL-3.0-or-later",
|
"license": "GPL-3.0-or-later",
|
||||||
|
"dependencies": {
|
||||||
|
"@plausible-analytics/tracker": "^0.4.5"
|
||||||
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@astrojs/check": "^0.9.9",
|
"@astrojs/check": "^0.9.9",
|
||||||
"@astrojs/rss": "^4.0.18",
|
"@astrojs/rss": "^4.0.18",
|
||||||
|
|
@ -1442,6 +1445,12 @@
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
},
|
},
|
||||||
|
"node_modules/@plausible-analytics/tracker": {
|
||||||
|
"version": "0.4.5",
|
||||||
|
"resolved": "https://registry.npmjs.org/@plausible-analytics/tracker/-/tracker-0.4.5.tgz",
|
||||||
|
"integrity": "sha512-6BfAGejXY+YA3Cw6LYT2Zpn4hTxDtPQAawFsYUsQCOg78wIS5C4deAGXTfJffa5VleMWITv5lpJ/EYuQBl1tPA==",
|
||||||
|
"license": "MIT"
|
||||||
|
},
|
||||||
"node_modules/@rollup/pluginutils": {
|
"node_modules/@rollup/pluginutils": {
|
||||||
"version": "5.3.0",
|
"version": "5.3.0",
|
||||||
"resolved": "https://registry.npmjs.org/@rollup/pluginutils/-/pluginutils-5.3.0.tgz",
|
"resolved": "https://registry.npmjs.org/@rollup/pluginutils/-/pluginutils-5.3.0.tgz",
|
||||||
|
|
@ -7184,6 +7193,11 @@
|
||||||
"integrity": "sha512-70wQhgYmndg4GCPxPPxPGevRKqTIJ2Nh4OkiMWmDAVYsTQ+Ta7Sq+rPevXyXGdzr30/qZBnyOalCszoMxlyldQ==",
|
"integrity": "sha512-70wQhgYmndg4GCPxPPxPGevRKqTIJ2Nh4OkiMWmDAVYsTQ+Ta7Sq+rPevXyXGdzr30/qZBnyOalCszoMxlyldQ==",
|
||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
|
"@plausible-analytics/tracker": {
|
||||||
|
"version": "0.4.5",
|
||||||
|
"resolved": "https://registry.npmjs.org/@plausible-analytics/tracker/-/tracker-0.4.5.tgz",
|
||||||
|
"integrity": "sha512-6BfAGejXY+YA3Cw6LYT2Zpn4hTxDtPQAawFsYUsQCOg78wIS5C4deAGXTfJffa5VleMWITv5lpJ/EYuQBl1tPA=="
|
||||||
|
},
|
||||||
"@rollup/pluginutils": {
|
"@rollup/pluginutils": {
|
||||||
"version": "5.3.0",
|
"version": "5.3.0",
|
||||||
"resolved": "https://registry.npmjs.org/@rollup/pluginutils/-/pluginutils-5.3.0.tgz",
|
"resolved": "https://registry.npmjs.org/@rollup/pluginutils/-/pluginutils-5.3.0.tgz",
|
||||||
|
|
|
||||||
|
|
@ -55,5 +55,8 @@
|
||||||
},
|
},
|
||||||
"overrides": {
|
"overrides": {
|
||||||
"yaml": "^2.9.0"
|
"yaml": "^2.9.0"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"@plausible-analytics/tracker": "^0.4.5"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,14 @@ try {
|
||||||
}
|
}
|
||||||
|
|
||||||
const files = await walk(dist);
|
const files = await walk(dist);
|
||||||
const jsFiles = files.filter((file) => file.endsWith('.js'));
|
const ALLOWED_JS_ASSET_PATTERNS = [
|
||||||
|
/[/\\]_astro[/\\]Analytics\.astro_astro_type_script_index_0_lang\.[\w-]+\.js$/,
|
||||||
|
];
|
||||||
|
const jsFiles = files.filter(
|
||||||
|
(file) =>
|
||||||
|
file.endsWith('.js') &&
|
||||||
|
!ALLOWED_JS_ASSET_PATTERNS.some((pattern) => pattern.test(file))
|
||||||
|
);
|
||||||
|
|
||||||
if (jsFiles.length > 0) {
|
if (jsFiles.length > 0) {
|
||||||
failures.push(
|
failures.push(
|
||||||
|
|
@ -36,17 +43,21 @@ if (jsFiles.length > 0) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Script tags are only allowed if they declare one of these safe `type`
|
// Script tags are only allowed if they declare one of these safe `type`
|
||||||
// attributes (or are tagged with `data-theme-script`). All other scripts,
|
// attributes or match one of the known executable scripts below. All other
|
||||||
// including untyped ones, which default to executable JavaScript, are flagged.
|
// scripts, including untyped ones, which default to executable JavaScript, are
|
||||||
|
// flagged.
|
||||||
const SAFE_SCRIPT_TYPES = new Set([
|
const SAFE_SCRIPT_TYPES = new Set([
|
||||||
'application/ld+json',
|
'application/ld+json',
|
||||||
'importmap',
|
'importmap',
|
||||||
'speculationrules',
|
'speculationrules',
|
||||||
]);
|
]);
|
||||||
|
const ANALYTICS_SCRIPT_SRC_PATTERN =
|
||||||
|
/\bsrc=["']\/_astro\/Analytics\.astro_astro_type_script_index_0_lang\.[\w-]+\.js["']/i;
|
||||||
|
|
||||||
function isSafeScriptTag(tag) {
|
function isSafeScriptTag(tag) {
|
||||||
if (tag.includes('data-theme-script')) return true;
|
if (tag.includes('data-theme-script')) return true;
|
||||||
if (tag.includes('data-thumbnail-iframe-script')) return true;
|
if (tag.includes('data-thumbnail-iframe-script')) return true;
|
||||||
|
if (ANALYTICS_SCRIPT_SRC_PATTERN.test(tag)) return true;
|
||||||
const typeMatch = tag.match(/\btype=["']([^"']+)["']/i);
|
const typeMatch = tag.match(/\btype=["']([^"']+)["']/i);
|
||||||
if (!typeMatch) return false;
|
if (!typeMatch) return false;
|
||||||
return SAFE_SCRIPT_TYPES.has(typeMatch[1].trim().toLowerCase());
|
return SAFE_SCRIPT_TYPES.has(typeMatch[1].trim().toLowerCase());
|
||||||
|
|
|
||||||
3
src/components/Analytics.astro
Normal file
3
src/components/Analytics.astro
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
<script>
|
||||||
|
import '../scripts/analytics';
|
||||||
|
</script>
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
---
|
---
|
||||||
import Footer from '../components/Footer.astro';
|
import Footer from '../components/Footer.astro';
|
||||||
import Header from '../components/Header.astro';
|
import Header from '../components/Header.astro';
|
||||||
|
import Analytics from '../components/Analytics.astro';
|
||||||
import { absoluteUrl, optimizeOgImage, site } from '../lib/site';
|
import { absoluteUrl, optimizeOgImage, site } from '../lib/site';
|
||||||
import defaultOg from '../assets/og-default.jpg';
|
import defaultOg from '../assets/og-default.jpg';
|
||||||
import themeInit from '../scripts/theme-init.js?raw';
|
import themeInit from '../scripts/theme-init.js?raw';
|
||||||
|
|
@ -167,6 +168,7 @@ const jsonLdStrings = jsonLdEntries.map((entry) =>
|
||||||
<script is:inline type="application/ld+json" set:html={jsonLdString} />
|
<script is:inline type="application/ld+json" set:html={jsonLdString} />
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
<Analytics />
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<Header />
|
<Header />
|
||||||
|
|
|
||||||
17
src/scripts/analytics.ts
Normal file
17
src/scripts/analytics.ts
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
import { init as plausibleInit } from '@plausible-analytics/tracker';
|
||||||
|
|
||||||
|
const ANALYTICS_AUTO_CAPTURE_PAGEVIEWS = true;
|
||||||
|
const ANALYTICS_DOMAIN = 'schmelczer.dev';
|
||||||
|
const ANALYTICS_ENDPOINT = 'https://stats.schmelczer.dev/status';
|
||||||
|
const ANALYTICS_LOGGING = import.meta.env.DEV;
|
||||||
|
|
||||||
|
try {
|
||||||
|
plausibleInit({
|
||||||
|
domain: ANALYTICS_DOMAIN,
|
||||||
|
endpoint: ANALYTICS_ENDPOINT,
|
||||||
|
autoCapturePageviews: ANALYTICS_AUTO_CAPTURE_PAGEVIEWS,
|
||||||
|
logging: ANALYTICS_LOGGING,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.warn('Could not initialize analytics.', error);
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue