中文版本 | English Version
NamBlog supports dynamically embedding website analytics scripts (such as Umami, Google Analytics, Baidu Analytics, etc.) through configuration files, without modifying frontend code.
Add AnalyticsScript field in data/config/config.json:
{
"Blog": {
"BlogName": "My Blog",
"Blogger": "Blogger Name",
"AnalyticsScript": "<script async defer data-website-id=\"your-website-id\" src=\"https://analytics.yourdomain.com/script.js\"></script>"
}
}Set environment variable when starting Docker Compose or container:
# docker-compose.yml
services:
namblog:
environment:
- Blog__AnalyticsScript=<script async defer data-website-id="your-id" src="https://analytics.yourdomain.com/script.js"></script>Or via command line:
docker run -e 'Blog__AnalyticsScript=<script async defer data-website-id="your-id" src="https://analytics.yourdomain.com/script.js"></script>' ...Umami is a lightweight, open-source, privacy-friendly website analytics tool.
{
"AnalyticsScript": "<script async defer data-website-id=\"your-website-id\" src=\"https://analytics.yourdomain.com/script.js\"></script>"
}{
"AnalyticsScript": "<script async src=\"https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX\"></script><script>window.dataLayer = window.dataLayer || [];function gtag(){dataLayer.push(arguments);}gtag('js', new Date());gtag('config', 'G-XXXXXXXXXX');</script>"
}{
"AnalyticsScript": "<script>var _hmt = _hmt || [];(function() {var hm = document.createElement(\"script\");hm.src = \"https://hm.baidu.com/hm.js?your-baidu-token\";var s = document.getElementsByTagName(\"script\")[0];s.parentNode.insertBefore(hm, s);})();</script>"
}{
"AnalyticsScript": "<script charset=\"UTF-8\" id=\"LA_COLLECT\" src=\"//sdk.51.la/js-sdk-pro.min.js\"></script><script>LA.init({id:\"your-51la-id\",ck:\"your-51la-key\"})</script>"
}Blog configuration supports hot reload. After modifying config.json, no application restart needed. Just refresh the frontend page to take effect.
- Analytics scripts are configured by administrators and stored only in server-side configuration files
- Frontend obtains scripts via GraphQL API and injects them dynamically
- Recommended to only use trusted analytics service providers
- HTML Escaping: When configuring in JSON, ensure proper escaping of double quotes or use single quotes
- Multiple Scripts: To use multiple analytics platforms simultaneously, concatenate multiple
<script>tags together - Async Loading: Recommended to use
asyncordeferattributes for analytics scripts to avoid blocking page load - Empty Value Handling: Leave empty or don't configure this field to not load any analytics scripts
{
"AnalyticsScript": "<script async defer data-website-id=\"umami-id\" src=\"https://analytics.example.com/script.js\"></script><script async src=\"https://www.googletagmanager.com/gtag/js?id=G-XXXXX\"></script>"
}- BlogInfo.cs - Add
AnalyticsScriptproperty - BlogBasicType.cs - GraphQL Schema definition
- Configuration file supports hot reload (
IOptionsSnapshot<BlogInfo>)
- Footer.js - GraphQL query to get script
- Dynamically create
<script>tag and inject to page bottom - Error tolerance handling, analytics script loading failure doesn't affect normal usage
- Modify configuration file to add analytics script
- Refresh frontend page
- Open browser developer tools (F12)
- Check Network tab to confirm analytics script is loaded
- Check Console tab to confirm no errors
- Visit analytics platform to check if data is being reported
A: Check the following:
- JSON format is correct (note escaping)
- Refresh frontend page (Ctrl+F5 for force refresh)
- Check browser console for errors
- Confirm analytics script URL is correct
A: Yes, use double underscore separator: Blog__AnalyticsScript
A: Blog configuration supports hot reload, takes effect immediately after modification, refresh frontend page.
A: Using async or defer attributes avoids blocking page rendering, minimal impact on performance.