After officially deploying the A Tour of Go Simplified Chinese site, I started adding traffic analytics for the production environment.
This time, I planned to integrate two analytics services already in long-term use on my blog: Google Analytics 4 and Baidu Tongji.
At first glance, this seemed simple: just put two pieces of JavaScript into the page <head>.
But once I actually started, I found it involved quite a few production environment issues:
- Whether the existing Go Tour code already reserved an entry point for analytics code;
- Whether local preview and the production environment use the same initialization logic;
- Whether the homepage
/and/tour/...share the same template; - Whether the analytics code should be written directly into the template or injected via production environment configuration;
- How systemd should persistently load environment variables containing HTML and JavaScript;
- Whether EdgeOne will continue returning old HTML after the new version goes live;
- Finally, how to prove that the analytics code not only “appears in the page source” but actually completes data reporting to Google and Baidu.
Ultimately, instead of designing a separate configuration system for analytics, I continued using the project’s existing TOUR_ANALYTICS and divided the acceptance testing into three layers:
Origin server
→ EdgeOne public network
→ Real browser reporting

1. The project already had AnalyticsHTML
The first thing to confirm was: does the existing Go Tour actually have an analytics code injection mechanism?
After searching the code, you can see that the shared Tour template _content/tour/template/index.tmpl already contains:
{{.AnalyticsHTML}}
Continuing to trace up from AnalyticsHTML, I eventually got the rough call chain for the production environment:
cmd/tour-i18n publish
→ Build cmd/tour-production
→ tour.NewProductionHandler(...)
→ RegisterHandlersLocale(...)
→ analyticsHTML = template.HTML(os.Getenv("TOUR_ANALYTICS"))
→ initTour(...)
→ renderIndex(...)
→ {{.AnalyticsHTML}}
This means the project originally designed an entry point for production analytics code:
TOUR_ANALYTICS
When the production process starts, it reads this environment variable and injects its contents as HTML into the page <head>.
There is also a detail here that can easily cause confusion: the logic that reads TOUR_ANALYTICS is located in internal/tour/appengine.go.
But the current A Tour of Go Chinese site is not running on Google App Engine.
The current production chain is still:
EdgeOne
→ Nginx
→ 127.0.0.1:3999
→ Go production binary
appengine.go is just a filename left over from the historical evolution of the upstream project.
This again shows that when maintaining a repository modified from an upstream project, you cannot judge actual production behavior solely by filenames; you must trace along the real call relationships.
2. Local development does not load production analytics code by default
Normal local execution in the project uses local.go.
This defines a default empty value:
var analyticsHTML template.HTML
Local preview does not actively read TOUR_ANALYTICS.
Only during production initialization does it execute:
analyticsHTML = template.HTML(os.Getenv("TOUR_ANALYTICS"))
This exactly matches the current project’s needs:
Local development
→ No production analytics by default
Production
→ Enable analytics via TOUR_ANALYTICS
Therefore, there is no need to introduce a .env parsing library, nor to add new command-line parameters or a configuration system.
Analytics remains solely a production deployment configuration.
3. What was actually missing was the homepage /
Continuing the audit, I found that the existing analytics entry was actually incomplete.
The Tour lesson pages use _content/tour/template/index.tmpl, which already contains:
{{.AnalyticsHTML}}
But the standalone homepage / uses a different template:
_content/tour/template/home.tmpl
This template originally did not have AnalyticsHTML.
In other words, if I set TOUR_ANALYTICS for production right now, the result would be:
/tour/...
→ Has analytics code
/
→ No analytics code
For a complete standalone site, this is clearly not ideal.
Therefore, the Go project code that actually needed modification this time was very minimal: make the homepage template also reuse the existing AnalyticsHTML.
The corresponding commit is:
cac4d6e feat: 为首页接入统一统计代码
After the modification, the homepage / and the lesson pages /tour/... use the same TOUR_ANALYTICS.
I also added automated tests covering both states respectively:
analyticsHTML is empty
→ Neither homepage nor Tour shell has analytics code
analyticsHTML is configured
→ Both homepage and Tour shell are injected exactly once
I did not add a second environment variable, nor did I hardcode the Google Analytics or Baidu Tongji code into the template.
4. Baidu Tongji continues to be shared with the blog
My WordPress Chinese and English sites originally already shared the same Baidu Tongji code.
In the Baidu Tongji backend’s “Visited Domains” report, you can respectively see:
www.shuijingwanwq.comen.shuijingwanwq.com
This means that even if multiple subdomains share the same Baidu Tongji code, you can still split and view the data by visited domain.
Therefore, for the new:
go-dev.shuijingwanwq.com
I ultimately did not create a separate Baidu Tongji site, but continued to reuse the blog’s existing Baidu Tongji code.
This way, I can view overall data for all sites, and also separately analyze the Chinese blog, English blog, and A Tour of Go Chinese site by visited domain.
Google Analytics adopted the same approach, continuing to use the blog’s existing GA4 configuration.
For my current use case, this is simpler than maintaining multiple analytics accounts and independent configurations.
5. Did not use a normal .env, but used systemd EnvironmentFile
Next, I needed to solve the configuration storage problem on the production server.
The Go program reads a standard environment variable:
os.Getenv("TOUR_ANALYTICS")
It does not automatically read the .env file in the project directory.
Since the production service was already managed by systemd anyway, there was no need to add .env loading code to the Go project just for an analytics variable.
The server’s current systemd version is 239, which already supports EnvironmentFile=.
So I ultimately created a dedicated environment variable file:
/etc/go-tour/go-tour.env
Permissions set to:
root:root
600
Then, via a systemd drop-in:
/etc/systemd/system/go-tour.service.d/analytics.conf
Load this file:
[Service]
EnvironmentFile=/etc/go-tour/go-tour.env
The resulting production configuration relationship is very clear:
/etc/go-tour/go-tour.env
↓
systemd EnvironmentFile
↓
TOUR_ANALYTICS
↓
Go production binary
↓
{{.AnalyticsHTML}}
↓
Homepage / + /tour/... pages
Both the Google Analytics and Baidu Tongji code are placed in the same TOUR_ANALYTICS.
The actual analytics code and specific analytics IDs do not enter the Git repository.
6. Why this environment variable still needs strict permission control
The analytics IDs themselves will eventually appear in the page source and are not server secrets in a strict sense.
However, TOUR_ANALYTICS itself deserves more cautious management.
The reason is that the program executes:
template.HTML(...)
This means the Go template explicitly treats the contents of this environment variable as trusted HTML and will not escape it again as a normal string.
This is the basis for the analytics code to be properly injected into <script>, but it also means:
Whoever can modify
TOUR_ANALYTICScan actually inject arbitrary HTML or JavaScript into the production page<head>.
Therefore, setting the environment variable file to:
root:root
600
is reasonable.
This is also why I did not put the full analytics code directly into Git, nor did I write it as a normal startup parameter on the command line.
7. First verify systemd → Go → Tour page
After configuring the systemd drop-in, first execute:
systemctl daemon-reload
systemctl restart go-tour.service
Then directly request a lesson page from the origin server itself.
The result confirms:
Google ID count: 2
Baidu ID count: 1
It is normal for the Google ID to appear twice, because in standard GA4 code it typically appears separately in the tag loading URL and in gtag("config", ...).
The Baidu Tongji ID appears once.
This step proves:
EnvironmentFile
→ TOUR_ANALYTICS
→ production binary
→ Tour shell
The entire chain is working properly.
8. Regenerate the production release so the homepage also gets the analytics code
However, the production server was still running the old binary at this point.
The old version already supported /tour/...‘s TOUR_ANALYTICS, but did not yet include the analytics entry just added for the homepage /.
So next I regenerated a production bundle:
/tmp/go-tour-release-20260812-zh-CN-cac4d6e
The build results remained:
locale=zh-CN
ready=103
pending=0
blocked=0
pages=103
articles=7
I also checked the full SHA256SUMS, and all files passed.
The two templates in the release package were also confirmed to respectively contain:
{{.AnalyticsHTML}}
Afterwards, I uploaded the new bundle to:
/data/go-tour/releases/20260812-zh-CN-cac4d6e
I first completed the server-side SHA-256 checksum and go-tour user access permission checks in the standalone release directory, without immediately switching production.
After confirming everything passed, I then switched:
/data/go-tour/current
to the new release, and restarted go-tour.service.
The service status was normal:
active
Then I rechecked the origin server homepage / and /tour/welcome/1:
/ Google=2 Baidu=1
/tour/welcome/1 Google=2 Baidu=1
At this point, the Go service itself had completed the analytics integration.

9. Origin server updated, but EdgeOne still returned old HTML
The most noteworthy issue during this deployment actually occurred at the CDN layer.
The origin server clearly contained the Google Analytics and Baidu Tongji code, but accessing via the public domain returned:
Google ID count: 0
Baidu ID count: 0
After further inspecting the response headers, I found:
EO-Cache-Status: HIT
Age: 4727
This meant the problem was not with the Go service, nor with TOUR_ANALYTICS.
Rather, EdgeOne was still returning old HTML cached from over an hour ago.
This is also a very typical production environment issue:
Successful verification on the origin server does not mean public users are already seeing the new version.
Especially since what was modified this time was the HTML shell, not versioned static JS or CSS.
If the CDN cache was still hit, even if the analytics code had already gone live on the origin server, real visitors still would not receive it.
Therefore, I performed a Hostname-level cache flush for go-dev.shuijingwanwq.com in EdgeOne.
I did not just flush / or a specific lesson page, because a large number of URLs under the entire /tour/... might have already been cached.
After flushing, I tested again, and the response became:
EO-Cache-Status: MISS
And the public HTML had changed to:
Google=2
Baidu=1
It was completely normal for HIT to reappear for some requests afterwards, because at that point the cached pages were already the new version containing the analytics code.
The final production acceptance summary I compiled this time was:
Release: 20260812-zh-CN-cac4d6e
Service: active
EnvironmentFile: /etc/go-tour/go-tour.env
Source / Google=2 Baidu=1
Source /tour/welcome/1 Google=2 Baidu=1
Public / Google=2 Baidu=1
Public /tour/welcome/1 Google=2 Baidu=1
This step led me to explicitly split the analytics deployment acceptance into two layers:
Origin server correct
≠
Public network definitely correct
The CDN must be verified separately.
10. Code appearing in the HTML still does not mean analytics are actually working
At this point, I could still only prove:
The Google Analytics and Baidu Tongji code had entered the HTML received by visitors.
But whether the analytics services were actually working still needed to be confirmed after the browser executed the JavaScript.
Therefore, the final layer of acceptance was done in the Network panel of the Firefox Developer Tools.
11. Google Analytics: real g/collect returns 204
First, filter by:
collect
You can see the request is sent to:
analytics.google.com/g/collect
The request contains the current GA4 tid, and the dl parameter has correctly recorded:
https://go-dev.shuijingwanwq.com/tour/welcome/1
One of the requests was a 90% page scroll event:
en=scroll
epn.percent_scrolled=90
Response status:
204
This is no longer just “Google tag loaded successfully”.
It proves that the browser has actually executed the GA4 code and sent real user events from the A Tour of Go Chinese site to Google Analytics.

g/collect request returns HTTP 204 and carries the current lesson page URL, confirming Google Analytics has generated a real event report12. Baidu Tongji: hm.gif returns 200
Next, I changed the Network filter to:
hm.baidu
You can see two main types of requests:
hm.jshm.gif
Here, hm.js is the Baidu Tongji script, while hm.gif contains the actual analytics reporting.
The current page can also be seen in the request parameters:
https://go-dev.shuijingwanwq.com/tour/welcome/1
The response status is:
200 OK
Therefore, Baidu Tongji has also completed real data reporting.

hm.gif request returns HTTP 200 and carries the current lesson page URL, confirming Baidu Tongji has completed real data reporting13. Finally forming a three-layer analytics acceptance
After this deployment, I fixed the acceptance method for the production analytics feature into three layers.
The first layer is the origin server.
Directly check:
http://127.0.0.1:3999/
http://127.0.0.1:3999/tour/welcome/1
This verifies:
systemd
→ EnvironmentFile
→ TOUR_ANALYTICS
→ Go template
The second layer is the public network.
Check:
https://go-dev.shuijingwanwq.com/
https://go-dev.shuijingwanwq.com/tour/welcome/1
Here, besides the analytics code itself, you also need to pay attention to:
EO-Cache-Status
Age
To avoid the CDN still returning old HTML.
The third layer is the real browser request.
For Google Analytics, check:
analytics.google.com/g/collect
For Baidu Tongji, check:
hm.baidu.com
Finally confirm that the response statuses are respectively normal.
The conclusion drawn this way is much more reliable than simply “there is <script> in the page source”.
14. Writing production configuration into operations documentation
After all verification was complete, I also added this configuration to:
docs/PRODUCTION_RUNBOOK.md
Corresponding commit:
0759457 docs: 补充生产统计配置与验收流程
The documentation now explicitly records:
- The unified analytics entry for
TOUR_ANALYTICS; /etc/go-tour/go-tour.env;- systemd
EnvironmentFiledrop-in; - The shared analytics mechanism for the homepage
/and/tour/...; - Actual analytics IDs and full code not entering Git;
- Service restart requirements after modifying environment variables;
- EdgeOne HTML cache potentially continuing to return old pages;
- The three-layer acceptance method of “origin server → public network → browser”;
- Real reporting verification for Google
g/collectand Baiduhm.gif.
This way, even if I migrate servers, rebuild the systemd service, or redeploy A Tour of Go in the future, I won’t forget the analytics system integration method just because the configuration only existed on the server.
15. What was actually solved this time was not “adding two pieces of analytics code”
In retrospect, the truly valuable part of this work was not copying the Google Analytics and Baidu Tongji JavaScript into the web pages.
More importantly, it integrated the analytics capability into the existing production architecture:
Production configuration
→ systemd
→ Go service
→ Template
→ EdgeOne
→ Browser
→ Third-party analytics service
And each layer has independently verifiable results.
What ultimately formed is:
Local development
→ No production analytics by default
Production environment
→ EnvironmentFile injects TOUR_ANALYTICS
Homepage /
→ Has analytics
/tour/...
→ Has analytics
EdgeOne
→ New HTML confirmed
Google Analytics
→ g/collect HTTP 204
Baidu Tongji
→ hm.gif HTTP 200
This took a few more steps than directly hardcoding the analytics script into the template, but it will be much simpler to maintain in the long run.
Next time the analytics code needs to be replaced, there is no need to modify the Go source code again; when migrating the production server, there are also clear environment variable files and systemd configurations to restore.
And whether the CDN has fetched the new HTML, and whether the browser has completed real reporting, both have fixed acceptance methods.
For an A Tour of Go multilingual project that has just completed its official release, this fills in another fairly important piece of production infrastructure.

