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

Where Should Ads Go After Keeping a SPA? From AdSense Preview to Plan B

Figure 4: The final manual course ad is actually displayed within the course content area, rather than after the footer.

作者:

在

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.js loaded 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.

Figure 1: In the AdSense ad setup preview, Google found 1 in-page ad placement, but the position is below the course footer.
Figure 1: In the AdSense ad setup preview, Google found 1 in-page ad placement, but the position is below the course footer.

The top of the preview page clearly showed:

Plaintext
1 in-page ad

And 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:

Plaintext
Course body
Code editor
Run result

footer

Ad

Google 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:

Plaintext
Read the course
↓
View or modify the example code
↓
Run / Format
↓
Click Next

Rather than:

Plaintext
Read the course
↓
Scroll to the very bottom of the entire page
↓
Pass the footer
↓
Continue looking for ads

This relationship can also be intuitively seen on the normal course page itself.

Figure 2: In a normal A Tour of Go course page, the footer is already located after the course body and the editor.
Figure 2: In a normal A Tour of Go course page, the footer is already located after the course body and the editor.

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:

Plaintext
One course page
        ↓
Corresponds to one course ad

Rather than:

Plaintext
The entire SPA Document
        ↓
An ad created who knows when

These two models are very different for an SPA.

Because from the browser’s perspective, A Tour of Go goes from:

Plaintext
/tour/basics/1

switching to:

Plaintext
/tour/basics/2

without 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:

Plaintext
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.adsbygoogle

This 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:

Plaintext
The page DOM changed
        ↓
Determine what this change means
        ↓
Infer whether the course has switched
        ↓
Then decide whether to process the ad

This 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:

Plaintext
Angular already knows the view lifecycle
        ↓
The DOM changes accordingly
        ↓
MutationObserver sees the changes
        ↓
The ad code then guesses what Angular just did

While Plan B hoped to directly become:

Plaintext
Angular creates the course view
        ↓
Create the ad

Angular destroys the course view
        ↓
Destroy the ad

This 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:

Plaintext
mount()

After the current course view is destroyed by Angular:

Plaintext
unmount()

The actual code was built around this lifecycle.

Figure 3: The courseAd directive in Plan B. mount() is executed when the course view is created, and unmount() is executed during Angular $destroy.
Figure 3: The 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:

JavaScript
lifecycle.mount(elm[0]);

Then:

JavaScript
scope.$on('$destroy', function() {
    lifecycle.unmount(elm[0]);
});

After this, when the user opens:

Plaintext
/tour/basics/1

Angular creates the current course view and simultaneously creates the ad for the current course.

After clicking Next:

Plaintext
/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:

HTML
<ins class="adsbygoogle">

Then execute a new:

JavaScript
(adsbygoogle = window.adsbygoogle || []).push({});

When the old view is destroyed, this ad node exits with it.

This makes the model:

Plaintext
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:

JavaScript
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:

Plaintext
go-dev.shuijingwanwq.com
ja-go-dev.shuijingwanwq.com

Automatic 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:

Plaintext
Auto Ads has problems
↓
Turn them all off
↓
Change all pages to manual ads

But rather:

Plaintext
Existing Auto Ads
Keep
        +
A Tour of Go course pages
Add explicit manual ad slots

This 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.

Figure 4: The final manual course ad is actually displayed within the course content area, rather than after the footer.
Figure 4: The final manual course ad is actually displayed within the course content area, rather than after the footer.

Looking at this screenshot and the first AdSense Preview together, the difference is very obvious.

The initial idea given by Google Preview was:

Plaintext
Course
↓
footer
↓
Ad

While what was finally chosen was:

Plaintext
Course body
↓
Course ad
↓
footer

The 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:

Plaintext
Observe the entire page
↓
Identify DOM changes
↓
Infer the course state
↓
Process the ad

Plan B narrowed it down to:

Plaintext
Current course view created
↓
mount ad

Current course view destroyed
↓
unmount ad

The 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:

Plaintext
/tour/basics/1
/tour/basics/2
/tour/methods/1

could 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.

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

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

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

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

关于我  ·  GitHub  ·  邮件联系