0

I want to use this way to add an ads segment on my pages:

$("body").append(`
 <ins class="adsbygoogle"
   style="display:block"
   data-ad-client="ca-pub-${pub_id}"
   data-ad-slot="${slot_id}"
   data-ad-format="auto"
   data-full-width-responsive="true"></ins>`);
(adsbygoogle=window.adsbygoogle||[]).push({});

Is this okay? Do I need to be aware of anything?

Thanks

1

Probably not. AFAIK Google has a fallback for users without Javascript. Your users without Javascript will not get ads.

Beside of this i have doubts, that this code is 100% the same, which Google offers for implementation. And any changes of Google's code is a violation against ToS.

| improve this answer | |
  • No, adsense doesn't fallback for users without javascript. It will just not work. – Mikita Belahlazau May 25 at 16:17
1

Yes, that's ok to dynamically add ad units. Though you still need to follow ad placements guides just as you need to follow them for statically added units: https://support.google.com/adsense/answer/1346295?hl=en And here is a page about modifying code: https://support.google.com/adsense/answer/1354736 Note that it says the recommendation is to copy-paste code as it is, the reasoning is that if you modify it somehow then AdSense can't guarantee it'll work, e.g. you mistype some parameters. But given that you are doing advanced thing of dynamically adding units - you already aware of that risk.

| improve this answer | |
  • Thanks for the information. However, the "Acceptable modifications" only talk about the JavaScript part. There's no clear clue about creating dynamical DOM <ins> way. – AGamePlayer May 26 at 2:05
  • That's true. While I can't find specific example in adsense docs that uses what you are describing - many publishers do that. It's just docs are lacking more advanced examples as they intend to cover very broad spectrum of publishers, many of which are very familiar with JS so docs recommend copy-and-past-and-don't-modify approach. Here is an example question about dynamically added ads: support.google.com/adsense/thread/4355870?hl=en – Mikita Belahlazau May 26 at 20:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.