2

I have a with an id of "ad". I am trying to put in an adsense ad in there using JavaScript when a user meets a condition. Basically when a user meets a certain condition, ads will be shown.

This is my code:

if ($resultTot = $db->query($queryTot)) 
    {
        $data = $resultTot->fetch_assoc();

     if($data["total"]>1)
        {
                echo "<script>
                    document.getElementById('ad').innerHTML = 'async src='//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js'
<!-- YC2 ads -->
<ins class='adsbygoogle'
     style='display:block'
     data-ad-client='ca-pub-4557496244647182'
     data-ad-slot='4151529047'
     data-ad-format='auto'></ins>

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

                </script>";


     }
  }

When I use document.getElementId() to change let's say a p tag, it works. I just can't get the adsense to display. Any ideas on how I can add it in there and why my code isn't working?

Bytes
  • 691
  • 1
  • 7
  • 22

1 Answers1

0

You just need to print regular AdSense JS code. No need to get any elements with JS.

Currently you are just editing contents of #ad DOM element. This dosen't work, because it has to be <script> and not inside of it. src is attribute.

It must end up like this

<script async src='//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js'></script>

So, use this:

if ($resultTot = $db->query($queryTot)) 
{
  $data = $resultTot->fetch_assoc();

  if($data["total"]>1)
  {
    echo "
    <div id='ad'>
      <!-- YC2 ads -->
      <ins class='adsbygoogle'
           style='display:block'
           data-ad-client='ca-pub-4557496244647182'
           data-ad-slot='4151529047'
           data-ad-format='auto'></ins>
      <script>
      (adsbygoogle = window.adsbygoogle || []).push({});
      </script>
    </div>
    <script>
      var script = document.createElement('script');
      script.async = true;
      script.src = '//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js';
      document.getElementById('ad').appendChild(script);
    </script>
    ";
  }
}
Rene Korss
  • 5,414
  • 3
  • 31
  • 38
  • Is there a way to still input an ad where the div is, without directly putting it into the div? The reason why I want it that way is because I still want it to have it's div properties in css. Also, when the user doesn't meet the condition, I don't want the ad in the div. – Bytes Aug 11 '15 at 06:23
  • I understand that PHP is taking care of if user should see ad or not. Just don't add JS code to page, if user is not supposed to see ads. I don't know about adding ads inside some div. Not sure if it's allowed by Google TOS and if they have some ways to block this. [This question](http://stackoverflow.com/questions/1142861/jquery-append-google-adsense-to-div) could be helpful. – Rene Korss Aug 11 '15 at 06:40
  • Hmm, how could I position the ads if I don't add the JS code? – Bytes Aug 11 '15 at 06:54
  • I thought you don't want to show ads in some conditon. But I have edited my answer. Give it another try. – Rene Korss Aug 11 '15 at 07:02
  • It's working really well, but there's just one problem. I have 2 div tags in the HTML document. 1 called ad and the other called ad2. This is showing ads in ad2, not ad (div tag) for some reason. ad is located at the top of the page and ad2 is located at the bottom. With your code, it's only populate ads in ad2, when the div tag is ad... – Bytes Aug 11 '15 at 07:24
  • My code has ` – Rene Korss Aug 11 '15 at 07:30
  • Yes, but it's still displaying in the wrong position of where I positioned it in css for some reason... – Bytes Aug 11 '15 at 07:36
  • Is there a better way to go about not displaying ads if the user's number of clicks reaches > 1? – Bytes Aug 11 '15 at 07:42
  • But is google ads elements inserted into ` – Rene Korss Aug 11 '15 at 07:45
  • is being inserted into
    position
    – Bytes Aug 11 '15 at 07:51
  • How it gets inserted into `ad2`? Do you load PHP code in that div? Remove `
    ` in my code and add `` just wherever in page.
    – Rene Korss Aug 11 '15 at 07:57
  • Oh, wait it's working now! I didn't update my css. Do you think this code is okay with Google's TOS? – Bytes Aug 11 '15 at 08:04
  • If you are not pushing ads below the fold and not trying to mislead visitors, you should be good to go. You can read requirements at [Ad placement policies](https://support.google.com/adsense/answer/1346295?hl=en). – Rene Korss Aug 11 '15 at 08:16