2

I'm trying some cookie consent frameworks like https://www.cookieconsent.com in order to improve the privacy settings at my website. In general those frameworks work in the same way: All script tags get a type="text/plain" and if the visitor accepts cookie, they change the type to type="text/javascript" and execute the code.

However, it doesn't work for Google Adsense tags like

<script type="text/plain" cookie-consent="targeting" data-ad-client="XXXX" async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>

In the Chrome and Safari consoles I can see that the type is type="text/javascript" but I don't see ads. It works perfectly for everything else (e.g Google Analytics). What's wrong with the Google Adsense code and how can I show it only to people who like cookies?

Simon
  • 313
  • 5
  • 15
  • Do you know how they execute the code? By just changing `type="text/plain"` to `type="text/javascript"` I don't see browser actually fetching and executing JS file (network tab is empty). – Mikita Belahlazau Jun 17 '20 at 18:16
  • @MikitaBelahlazau In the network tab I can see that `cookie-consent.js` fetches `https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js` successfully. I guess it executes it with something like `eval()`. – Simon Jun 17 '20 at 23:03
  • Not quite, it's using `document.createElement("script");` to append a new script element to `` and changes the attribute to type="text/javascript". Maybe this is the cause why it doesn't work https://stackoverflow.com/a/15841383/10385339 – Simon Jun 18 '20 at 07:44
  • Then it should work. The question you mention uses `show_ads.js` which indeed won't work with dynamically appended scripts, but `adsbygoogle.js` should work. Do you have a page that reproduces it? – Mikita Belahlazau Jun 19 '20 at 00:48
  • @MikitaBelahlazau Yeah, I get the feeling that the workarounds won't work for the "auto ads" feature because you cannot know where Adsense wants to place ads. I guess only Google can fix it. I can reproduce it on this page: https://www.sparplaene.net The green button "Alle akzeptieren" will allow all cookies. – Simon Jun 20 '20 at 10:06
  • Thanks for the link. Looks like cookieconsent JS in addition to insering new ` – Mikita Belahlazau Jun 21 '20 at 05:19
  • Wow, I didn't notice that! Thx for pointing me to the missing `data-ad-client` attribute. I was able to fix it by adding one line to my `cookie-consent.js`: `null!==t.getAttribute("data-ad-client")&&n.setAttribute("data-ad-client",t.getAttribute("data-ad-client")),` I just set `data-ad-client` again if it existed before (This is the full file: https://www.sparplaene.net/js/cookie-consent.js). I was trying to contact cookieconsent.com but they don't have any contact information on their website. Smells a bit scammy. Anyways, my ads are finally back :-) – Simon Jun 21 '20 at 08:45

0 Answers0