2

The website is set up to opt in for cookie consent but when a first time visitor clicks the accept cookies button the adsense code does not load unless the whole page is refreshed.

Is there a way to achieve loading ads once the cookie acceptance button is clicked instead of having to refresh the whole page?

Using this cookie consent jquery plug: IHaveCookies

This is part of the code I am using that allows adsense to load:

    $(document).ready(function() {
    $('body').ihavecookies(options);

if ($.fn.ihavecookies.preference('ads') === true) {
    var src = "//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js";
    var newScript = document.createElement("script");
    newScript.type = "text/javascript";
    newScript.setAttribute("async", "true");
    newScript.setAttribute("src", src);
    document.body.appendChild(newScript);
}

The above code does work but it does not allow ads to appear until after page refresh. Is it possible to add a "refresh" attribute to the above code to make the ads load on "ihavecookies.preference('ads') === true)" without page refresh?

The website where I've implemented this can be viewed Here

fyi.... currently no ads are loading for EU or Californian visitors (but this is a separate issue not to be addressed with this topic)

Michael Joseph
  • 115
  • 1
  • 3
  • 13
  • See [Dynamic Adsense Insertion With JavaScript](https://stackoverflow.com/questions/6197768/dynamic-adsense-insertion-with-javascript) – Avatar May 29 '21 at 05:38

3 Answers3

6

Not directly related to the plugin you're using, but my answer will hopefully lead you in the right direction. Firstly, there are two AdSense Help pages that you may find helpful:

Ads personalization settings in Google’s publisher ad tags: https://support.google.com/adsense/answer/7670312

Ad code examples for ads personalization settings: https://support.google.com/adsense/answer/9042142

I'm using my custom cookie consent solution with AdSense (I use Auto Ads with non-personalized option) and here is what I am doing:

1. I put the following code between <head> tags, on pages where Ads will be displayed:

EDIT: Add the following to your page only if/after consent is given. (Thanks to @Avatar)

<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<script>(adsbygoogle=window.adsbygoogle||[]).pauseAdRequests=1;</script>

This loads the AdSense JS file, but pauses ad loading, hence does not place any cookies.

2. In my JS file where I control displaying/hiding consent banner, I put the following code to these actions: a) user gives consent by clicking OK, b) new page load if consent was given previously:

(adsbygoogle=window.adsbygoogle||[]).requestNonPersonalizedAds=1;
(adsbygoogle=window.adsbygoogle||[]).pauseAdRequests=0;
(adsbygoogle=window.adsbygoogle||[]).push({google_ad_client:'ca-pub-YOUR-ADSENSE-ID',enable_page_level_ads:true});

If you are using personalized ads, you can remove first line.

Ads are not loaded in other cases (e.g., user does nothing, user clicks REJECT, new page load where user has rejected before). I do place a cookie to keep track of rejection, so that the consent banner is not displayed at each page load.

yenren
  • 450
  • 9
  • 20
  • 1
    Already loading the domain https://pagead2.googlesyndication.com without consent of the user is **against** data privacy regulations. You can only load other domains **after** the user has given his consent (clicked the "Accept Cookies" button). – Avatar May 29 '21 at 05:30
  • @Avatar Thanks for the note. It will be better to load it only after consent is given. Do you have a clear idea if it is because it is an advertising related external resource? I mean, for example, if I have no advertising on my site, but use jquery or a font or an image etc., and include them from an external domain, should I also ask for user consent before loading those external resources? If you remember the specific item on the regulation related to this, that would be great. – yenren May 29 '21 at 10:09
  • If you are within European Union, the answer is yes. With each request to another domain the user could be tracked, which is not legal. I have noticed that not many people have an awareness of this yet. Scripts/fonts/images should be loaded from your own domain, but not an external domain. – Avatar May 29 '21 at 10:32
3

One of the easiest ways:

  1. Change all adsense script tags to type="text/plain"
  2. After the visitor accepts the cookies, change the type to type="text/javascript"
  3. Execute the code

You also want to store the cookie accept so the next time the adsense can be triggered right away.


See also:

Avatar
  • 14,622
  • 9
  • 119
  • 198
1

This is amazing thanks a lot! It you then host adsbygoogle.js localy no illegal connection to the USA will be made before the user accepted it.

 <script async src="adsbygoogle.js"></script>
 <script>(adsbygoogle=window.adsbygoogle||[]).pauseAdRequests=1;</script>

In Germany even loading google fonts from the US without the users permission seems to be illegal now: https://www.itmagazine.ch/artikel/76437/Deutsches_Gericht_erklaert_Einbindung_von_Google_Fonts_als_rechtswidrig.html

Here is a way to load Analytics only if allowed: https://stackoverflow.com/a/45838784/12668719

Update: I have tested it, unfortunately it still connects to the USA Unfortunately it still connects to the USA

Any other idea? Loading Google Adsense, Analytics and Youtube only when consent is given

Update: Dynamicly loading Google Adsense after consent OK: setTimeout("adsenseladen()", 1);

 <script type="text/javascript">
 function adsenseladen() {
 var script = document.createElement('script');
 script.type = 'text/javascript';
 script.src = 'https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js';
 document.body.appendChild(script);
 }
 </script>


 <ins class="adsbygoogle"
 style="display:block"
 data-ad-client="ca-pub-##########"
 data-ad-slot="##########"
 data-ad-format="auto"
 data-full-width-responsive="true"></ins>

 <script>
 (adsbygoogle = window.adsbygoogle || []).push({});
 </script>
human
  • 467
  • 4
  • 6