In the previous article, I ultimately decided not to abandon A Tour of Go’s original SPA architecture for the sake of Google AdSense.
That decision resolved the architectural direction, but it did not solve the actual advertising problem.
After keeping the SPA, two more specific questions had to be answered next:
Where exactly should the ads be placed?
And:
When a user clicks “Next” to enter a new course page, who should be responsible for recreating and destroying the ads?
These two questions later went through two actual implementations.
The first can be called Plan A: observing the SPA’s DOM changes via MutationObserver to determine when the course ad needed to be recreated.
It was indeed implemented and it worked.
However, after continued testing, some low-probability issues that were difficult to reproduce stably appeared, such as the example code occasionally being empty or the “Next” button occasionally failing to respond properly.
It was precisely because Plan A had been actually implemented and exposed these issues that Plan B was later designed: instead of inferring the SPA state through global DOM changes, the ads were bound directly to the lifecycle of the Angular current course view.
Google AdSense actually found an ad placement
Previous troubleshooting had already confirmed:
- AdSense Auto Ads was enabled;
adsbygoogle.jsloaded normally;- Although the course page had large visual blank spaces, standard display ads never actually showed up.
I then went back into the ad setup preview in the Google AdSense backend.
This time, I finally saw something very interesting.
Google’s page preview did find a place where an in-page ad could be inserted.

The top of the preview page clearly showed:
1 in-page adAnd the ad example Google provided was not in the course body area where I previously thought there was a lot of blank space.
It appeared:
After the footer.
This actually further confirmed the judgment in the previous article.
From a human perspective, there was clearly a large blank space to the left of the course body.
However, based on Google’s automatic ad layout judgment, it did not choose these positions; instead, it found an area where it thought an ad could be inserted only after the entire course body and footer had ended.
However, an important distinction must be made here.
This was the ad setup preview in the AdSense backend, and it does not mean that I later saw Auto Ads automatically displaying standard display ads below the footer on the actual public course page.
In fact, up until this round of ad optimization was basically completed, I still had not actually seen Auto Ads automatically insert standard page display ads in the A Tour of Go course pages.
The Auto Ads I actually saw were mainly in other forms, such as popup ads.
So what this screenshot really shows is:
Google’s preview system thinks that after the footer is a possible in-page ad placement.
Rather than:
Google is already stably displaying ads after the footer on the production site.
These two conclusions should not be confused.
Google thinking “it can be placed” does not mean this is a good ad spot
From a purely technical perspective, placing ads after the footer is of course not a problem.
The page structure would roughly become:
Course body
Code editor
Run result
footer
AdGoogle does not need to intrude into the complex left-right column structure.
It also does not need to determine whether the blank space below the course body on the left is a stable layout area.
Appending an ad after the entire main body has ended is indeed much simpler from an automatic layout perspective.
But from an actual user experience perspective, this position is not ideal.
A Tour of Go is a learning-based page.
After users enter the course page, their main path of action is:
Read the course
↓
View or modify the example code
↓
Run / Format
↓
Click NextRather than:
Read the course
↓
Scroll to the very bottom of the entire page
↓
Pass the footer
↓
Continue looking for adsThis relationship can also be intuitively seen on the normal course page itself.

If the core display ad continued to be placed after the footer, then although it “technically exists”, it would be too far from the actual course content.
For many course pages, users would have no reason at all to scroll to that position.
At this point I began to realize:
The automatic ad system solves the problem of “where ads can be inserted”, while the site itself still needs to decide “where it is suitable to display ads”.
The two are not the same problem.
I prefer the ad to belong to the current course rather than the end of the entire page
Finally, a direction gradually became clear:
The display ad should ideally become part of the current course page.
That is, it should appear inside the course content area, rather than becoming an extra piece of content appended after the footer.
This way, users would naturally see the ad while normally reading and interacting with the course.
More importantly, this design could also establish a very clear relationship:
One course page
↓
Corresponds to one course adRather than:
The entire SPA Document
↓
An ad created who knows whenThese two models are very different for an SPA.
Because from the browser’s perspective, A Tour of Go goes from:
/tour/basics/1switching to:
/tour/basics/2without the Document reloading.
But from the user’s perspective, this is already a new course page.
Therefore, I wanted the ad to follow the lifecycle of the “course page” rather than the lifecycle of the entire browser Document.
Plan A: Using MutationObserver to track SPA page changes
After keeping the SPA, the first actually implemented plan was to listen to the page DOM via MutationObserver.
The basic idea was similar to:
MutationObserver
↓
Observe page DOM changes
↓
Determine if the course content has switched
↓
Find the current ad mount position
↓
Clean up the old ad
↓
Create a new ins.adsbygoogleThis was the implementation that later came to be known as Plan A.
It was not just a concept stuck in the discussion phase.
After the code was actually deployed, the course SPA pagination could produce new ads, which showed that from the most basic functional goal perspective, Plan A was valid.
And it had an obvious advantage:
It did not require heavily intervening in the original Angular routing logic.
By just observing the changes in the final DOM, ads could be reprocessed after a page switch.
For a project that did not want to heavily modify the upstream Tour, this approach was actually quite attractive at first.
Plan A’s problem was not that it was “completely unusable”, but that the boundaries were too blurry
The real problems gradually emerged during subsequent continuous testing.
A Tour of Go’s DOM does not only change when “Next” is clicked.
There are also many dynamic behaviors within the page, such as:
- Angular route switching;
- CodeMirror initialization;
- Editor content changes;
- Playground run results;
- Course table of contents;
- Angular digest;
- Various dynamic node creations and destructions.
If a global MutationObserver was used to observe these changes, then the ad code was actually doing an indirect thing:
The page DOM changed
↓
Determine what this change means
↓
Infer whether the course has switched
↓
Then decide whether to process the adThis meant that the ad logic and Tour’s own runtime DOM became strongly coupled.
During initial testing, most situations were normal.
But after continued testing, I started encountering some low-probability issues that were difficult to reproduce stably.
For example, there had been:
- The example code area occasionally being empty;
- Clicking “Next” occasionally not responding properly;
- The page experiencing some abnormal states during initialization and switching.
The most troublesome part of these issues was not just that they existed.
It was that:
They could not be stably reproduced.
Sometimes it was normal after a refresh.
Sometimes re-entering the page caused no problems.
These sporadic issues were actually more troublesome for a production environment, because it was very difficult to clearly prove what kind of timing relationship occurred between a certain DOM observer callback and a certain Angular / CodeMirror lifecycle event.
At this point, the question shifted from:
Can Plan A display ads?
to:
To display ads, is it necessary to have a global DOM observer long-term participate in the entire Tour’s runtime lifecycle?
The answer started leaning towards no.
Why it was later changed to Plan B
After further analysis, an obvious problem emerged:
Angular itself actually already knew:
- When the current course view was created;
- When the current course view was destroyed;
- When the user entered another route-owned editor view.
Since the application itself already had this information, inferring it from the outside of the DOM via MutationObserver:
“Has the page changed now?”
was a bit of a roundabout way.
Plan A was roughly equivalent to:
Angular already knows the view lifecycle
↓
The DOM changes accordingly
↓
MutationObserver sees the changes
↓
The ad code then guesses what Angular just didWhile Plan B hoped to directly become:
Angular creates the course view
↓
Create the ad
Angular destroys the course view
↓
Destroy the adThis way, there was a direct relationship between the ad lifecycle and the course lifecycle.
Plan B: Binding ads to the Angular course view
The final Plan B was actually very simple at its core:
Make the ad part of the route-owned editor view.
After the current course view is created:
mount()After the current course view is destroyed by Angular:
unmount()The actual code was built around this lifecycle.

courseAd directive in Plan B. mount() is executed when the course view is created, and unmount() is executed during Angular $destroy.The core relationship can be simplified to:
lifecycle.mount(elm[0]);Then:
scope.$on('$destroy', function() {
lifecycle.unmount(elm[0]);
});After this, when the user opens:
/tour/basics/1Angular creates the current course view and simultaneously creates the ad for the current course.
After clicking Next:
/tour/basics/1
↓
$destroy
↓
unmount(oldAd)
↓
/tour/basics/2
↓
new view
↓
mount(newAd)Each course page has its own independent ad lifecycle.
Plan A was:
Observe the DOM, then infer the course lifecycle.
Plan B then became:
Directly use the course lifecycle.
This is the most essential difference between the two plans.
Creating a new adsbygoogle node for each page
Plan B also had an important principle:
Do not repeatedly reuse the same already-processed ins.adsbygoogle node.
After the new course view is established, it will create a new:
<ins class="adsbygoogle">Then execute a new:
(adsbygoogle = window.adsbygoogle || []).push({});When the old view is destroyed, this ad node exits with it.
This makes the model:
Course 1
├── ins.adsbygoogle #1
└── push({})
Next
Course 2
├── ins.adsbygoogle #2
└── push({})
Next
Course 3
├── ins.adsbygoogle #3
└── push({})Rather than repeatedly moving the same ad node already processed by Google throughout the entire SPA lifecycle.
Ad failures must not affect course navigation
Plan B also defined a clear boundary:
Ads are an附加 capability and must not affect A Tour of Go itself.
Therefore, exception isolation was specifically implemented in the courseAd directive.
For example:
try {
lifecycle.mount(elm[0]);
} catch (error) {
report(error);
}The same applied during destruction.
That is, even if:
- AdSense failed to load;
adsbygoogle.push()threw an exception;- The ad lifecycle helper was unavailable;
- A certain ad request failed;
It should at most affect:
This particular ad block.
It must not affect:
- The next page;
- The previous page;
- The course body;
- The editor;
- The Playground;
- The Angular route.
This was also an important lesson learned from the sporadic issues in Plan A:
The closer the ad code is to Tour’s core runtime path, the more clearly its scope of impact needs to be limited.
Why Auto Ads was still kept
After adopting manual course ads, there was another important decision:
Auto Ads was not turned off.
This was not just because “manual ads and automatic ads can coexist”.
There was also a more practical reason.
My AdSense did not only serve:
go-dev.shuijingwanwq.com
ja-go-dev.shuijingwanwq.comAutomatic ads were already being used normally on other sites and domains, such as the www and en environments.
That is, I was facing a larger existing AdSense usage system.
If I just turned off automatic ads entirely because A Tour of Go’s course pages were not suitable for Auto Ads, it might affect other pages that were already running normally.
This was obviously not worth the loss.
So the approach finally adopted was not:
Auto Ads has problems
↓
Turn them all off
↓
Change all pages to manual adsBut rather:
Existing Auto Ads
Keep
+
A Tour of Go course pages
Add explicit manual ad slotsThis way, it neither broke the already properly working Auto Ads on existing sites, nor did it fail to individually solve the problem of this special SPA layout in A Tour of Go.
In other words:
It was not about replacing the entire AdSense automatic ad system with manual ads, but simply adding a course ad area for A Tour of Go that Auto Ads had never been able to reliably handle.
The ad finally appeared in the actually desired position
After subsequent implementation and adjustments, the manual course ad was indeed able to display on the actual production page.

Looking at this screenshot and the first AdSense Preview together, the difference is very obvious.
The initial idea given by Google Preview was:
Course
↓
footer
↓
AdWhile what was finally chosen was:
Course body
↓
Course ad
↓
footerThe ad thus truly became part of the course page.
From a page experience perspective, this position was also more natural:
- Users did not need to pass the footer;
- The ad was in the same visual area as the current course;
- It did not intrude into the right-side code editor;
- It did not require restructuring the entire SPA for the ad;
- Each SPA page turn had its own independent ad lifecycle.
From Plan A to Plan B, what really changed was the control method
In retrospect, the most important change in this round was not actually adding a <ins>.
Plan A had already proven one thing:
SPA course pages could completely re-request ads without a full page refresh.
So Plan B was not started over because Plan A was “completely unusable”.
What really prompted me to continue adjusting was:
A working plan was not necessarily a plan suitable for long-term production maintenance.
Plan A’s control model was:
Observe the entire page
↓
Identify DOM changes
↓
Infer the course state
↓
Process the adPlan B narrowed it down to:
Current course view created
↓
mount ad
Current course view destroyed
↓
unmount adThe ad ultimately no longer needed to understand what was happening to the entire Tour’s DOM.
It only needed to understand:
Whether the course view it belonged to still existed.
For a project that needed to follow the upstream long-term, this boundary was clearly much clearer.
Plan B solved the ad lifecycle, but new problems lay ahead
At this point, the main body of Plan B was confirmed.
It solved:
- The placement of course display ads;
- Ad creation after SPA page switching;
- Old ad destruction;
- Non-accumulation of ad nodes;
- Isolation of ad exceptions from course functionality;
- No longer relying on global DOM changes to determine course switching;
- Not needing to abandon the SPA for AdSense.
But at the same time, A Tour of Go still had an earlier existing problem that had not been truly solved.
Although users entering:
/tour/basics/1
/tour/basics/2
/tour/methods/1could eventually see different content, it was still a typical client-side SPA.
Search engines had never been friendly to such pages.
And this round of ad optimization added another consideration:
If, when the server returned a course URL for the first time, it already included the current course’s:
- Title;
- Body;
- Example code;
- Metadata;
Then, in addition to helping search engines understand the actual content differences between these URLs, it might also make it easier for Google to understand the specific topic of the current page, thereby providing a clearer page context for subsequent ad matching.
This did not guarantee an increase in ad fill rates or relevance.
But from the perspective of page semantic completeness, it was obviously more reasonable than letting Google first receive a large number of highly similar SPA shells.
Thus, the entire optimization work entered the next long-existing problem:
SPA SEO.

