没有不值得去解决的问题,也没有不值得去学习的技术!

Integrating the A Tour of Go Multilingual Translation Project With an Ad System: From AdSense-Specific Configuration to a Universal HTML Injection Architecture

Figure 5: Public web page verifying ad script output

作者:

在

In the A Tour of Go multilingual translation project, the course content, Playground examples, production deployment, and runtime environment all adopt a design that separates public source code from production configuration.

When the project needed to integrate an advertising system, the initial consideration was to directly add AdSense-related configuration. However, after analysis, I realized that the advertising platform might change in the future, and directly binding AdSense into the code structure would reduce extensibility.

Therefore, this adjustment did not implement AdSense-specific logic. Instead, it abstracted the ad injection capability into a general HTML injection mechanism.

The final implementation:

  • Analytics code continues to use independent injection;
  • Ad code uses independent injection;
  • The production environment provides the actual content via systemd environment variables;
  • The public source code only retains the injection position and does not contain any ad account information.

Why not write the ad code directly into the source code

For a public source code project, if you write the ad code directly into the template:

HTML
<script async src="https://pagead2.googlesyndication.com/..."></script>

Then anyone who pulls the repository and deploys it will use the same ad configuration by default.

This causes:

  • Strong coupling between the source code and the production environment;
  • Inability for other deployers to configure independently;
  • Difficulty in switching ad platforms;
  • The public repository containing operational configuration that does not belong to the project itself.

Therefore, the project continues to use the existing analytics code injection method:

Plaintext
Production environment configuration
        ↓
systemd EnvironmentFile
        ↓
Go service reads
        ↓
Template rendering
        ↓
HTML head injection

From AdSense-specific configuration to general ad injection

In the initial design, the ad configuration was easily tied to the AdSense platform.

For example:

Plaintext
TOUR_ADSENSE_CLIENT

This approach can only express:

“Currently using AdSense”.

But in the future, if you need to:

  • Change the ad platform;
  • Add multiple ad scripts;
  • Use other HTML injection content;

All of these would require modifying the code structure again.

Therefore, this refactoring adjusted it to:

Plaintext
TOUR_AD_HTML

The HTML content to be injected is provided directly by the environment variable.

Corresponding commit:

Figure 1: Generalized ad HTML injection configuration
Figure 1: Generalized ad HTML injection configuration

Go service adds an independent ad injection chain

New injection chain:

Plaintext
TOUR_AD_HTML
        ↓
os.Getenv("TOUR_AD_HTML")
        ↓
template.HTML
        ↓
AdHTML
        ↓
{{.AnalyticsHTML}}{{.AdHTML}}
        ↓
HTML <head>

In the implementation:

  • AnalyticsHTML continues to handle the analytics code;
  • AdHTML handles the ad HTML;
  • The two remain independent.

This allows you to control separately:

  • Whether to enable analytics;
  • Whether to enable ads;
  • What ad content to use.
Figure 2: Go service and template layer add a new AdHTML injection chain
Figure 2: Go service and template layer add a new AdHTML injection chain

Release package only contains injection capability, not production ad configuration

When the project release process generates the production bundle:

It will not generate:

  • AdSense client ID;
  • Ad scripts;
  • Production environment configuration.

The templates in the release package only retain:

HTML
{{.AnalyticsHTML}}{{.AdHTML}}
<meta charset="utf-8">

That is:

The source code and release package know “where to inject”.

But they do not know “what to inject”.

Figure 3: Ad injection position retained in the Production Bundle
Figure 3: Ad injection position retained in the Production Bundle

Production environment injects ad code via systemd

The actual ad configuration only exists on the production server:

Bash
/etc/go-tour/go-tour.env

Where:

Bash
TOUR_ANALYTICS='...'

TOUR_AD_HTML='<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=..."></script>'

After modifying the production configuration, you only need to restart the service.

The entire process:

Plaintext
/etc/go-tour/go-tour.env
        ↓
systemd EnvironmentFile
        ↓
go-tour.service
        ↓
Read TOUR_AD_HTML
        ↓
Template rendering
        ↓
Browser HTML head
Figure 4: Production environment enables ads via environment variables
Figure 4: Production environment enables ads via environment variables

Public network verification of ad script output

After deployment, verify via the public web page:

Bash
curl -sS https://go-dev.shuijingwanwq.com/ | grep pagead2

Returns:

HTML
https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js

This shows:

The ad injection chain is now in effect.

From production configuration to the final page:

Plaintext
Production environment variables
        ↓
Go service
        ↓
HTML rendering
        ↓
Browser loads ad script

This entire process did not modify the ad account configuration in the public source code.

Figure 5: Public web page verifying ad script output
Figure 5: Public web page verifying ad script output

Summary

This ad integration was not simply adding a piece of AdSense script, but further refining the production configuration architecture of the A Tour of Go multilingual translation project.

The final implementation:

  • Decoupled ad logic from the specific ad platform;
  • Public source code does not contain production ad configuration;
  • Different deployment environments can independently set ad content;
  • Future support for other ad platforms or custom HTML injection is possible.

Through the environment variable injection mechanism, the project maintains:

Plaintext
Public source code
+
Independent production configuration
+
Extensible deployment method

This is consistent with the project’s existing analytics code injection, Playground proxy, and production release process, providing a more stable foundation for long-term maintenance.

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系