PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label adsense. Show all posts
Showing posts with label adsense. Show all posts

Thursday, September 8, 2022

[FIXED] How to get DIV sub-elements using javascript

 September 08, 2022     adsense, ajax, html, javascript, jquery     No comments   

Issue

HTML Code

<ins class="adsbygoogle" style="display:inline-block;width:336px;height:280px" data-ad-client="ca-pub-1714167228247329" data-ad-slot="8611448539" data-adsbygoogle-status="done" data-ad-status="unfilled">

I have checked : https://www.w3schools.com/js/js_htmldom_elements.asp and tried but it not help.

I want to take this element data-ad-status="unfilled" from HTML using javascript. So, i can use it in if else statement.

Like

if (data-ad-status="unfilled") {
some fuctions; 
}

Solution

  • As you told me that data is coming from async way I have added setTimeOut() function to wait for around 2 seconds here.
  • I do not have access to how data is coming so this time for 2s is hard coded. change it's value and try to run it. and I am sure it will solve your problem.
  • But use asyn-await function and make selectAd() wait till you data get loaded.

const selecteAd = () => {
  const myDiv = document.querySelector("[data-ad-status='unfilled']");
  const selectAllWithAttributeAdStatus = document.querySelectorAll("[data-ad-status]");
  /*This(selectAllWithAttributeAdStatus) one is just for showing how to select elements with data-ad-status attribute */
  //let adStatus = myDiv.getAttribute("data-ad-status");
  selectAllWithAttributeAdStatus.forEach(ad => {
    if ((ad.getAttribute("data-ad-status")) === "unfilled") {
      ad.style.background = "red";
      ad.style.height = "100px";
      ad.style.width = "100px";
      ad.style.display = "inline-block";
    } else {
      ad.style.background = "green";
      ad.style.height = "100px";
      ad.style.width = "100px";
      ad.style.display = "inline-block";
    };
  })
}
const myTimeout = setTimeout(selecteAd, 2000);
<div class="adsbygoogle" style="display:inline-block;width:336px;height:280px" data-ad-client="ca-pub-1714167228247329" data-ad-slot="8611448539" data-adsbygoogle-status="done" data-ad-status="unfilled"></div>
<div class="adsbygoogle" style="display:inline-block;width:336px;height:280px" data-ad-client="ca-pub-1714167228247329" data-ad-slot="8611448539" data-adsbygoogle-status="done" data-ad-status="somethingelse"></div>



Answered By - Nikkkshit
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Saturday, April 16, 2022

[FIXED] How to hide empty adsense box

 April 16, 2022     adsense, html, iframe, jquery     No comments   

Issue

How to hide adsense iframe/div when no ads for this box are available?


Solution

much better idea: http://www.google.com/support/adsense/bin/answer.py?hl=en&answer=21590&rd=1



Answered By - light
Answer Checked By - David Goodson (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Sunday, March 13, 2022

[FIXED] Show only if user-role is subscriber AND site visitor (Wordpress)

 March 13, 2022     adsense, user-roles, wordpress     No comments   

Issue

I am a bit stuck with the code below for my Wordpress site:

<?php
    $user = wp_get_current_user();
    $allowed_roles = array('subscriber', 'visitor');
     if ( array_intersect($allowed_roles, $user->roles ) ) echo '<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-XXXXXXXXXXXXXXX" crossorigin="anonymous"></script>' ?>

I want the Adsense code only be shown in the HEAD section for site visitors and the user role SUBSCRIBER. I tried 'visitor' but that didn't worked.

I don't know how to apply "array_diff" in this situation to display the Adsense code in the header for SITE VISITORS and SUBSCRIBER role?


Solution

For "subscriber" you can use function current_user_can

For guests you can use function is_user_logged_in()

if ( current_user_can( 'subscriber' ) || !is_user_logged_in()) {
           // echo your adsense code
}


Answered By - Valerii Vasyliev
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Saturday, February 19, 2022

[FIXED] How to do a redirect in Laravel for ads.txt

 February 19, 2022     .htaccess, adsense, laravel-5     No comments   

Issue

Google wants have access to ads.txt file like "example.com/ads.txt", but in Laravel we need redirect it to "public" folder.

How do it?

I try RedirectMatch 301 ^ads\.txt$ \/public\/ads\.txt, but it doesn't work.


Solution

You can create blade file has name ads.blade.php and put all text from ads.txt to this blade , and create route like that :

Route::get('/ads.txt',function(){
   return view('ads');
});

I tried that when register my website in google



Answered By - Ahmad Abo Zaid
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Thursday, February 3, 2022

[FIXED] How do you render Google ad units in localhost?

 February 03, 2022     adsense, html, javascript, localhost, mamp     No comments   

Issue

Per research there was a previous question from 2014 (Is Google adsense available for localhost?) that suggested adding:

data-adtest="on"

and after testing and reading the comments the solution no longer works. In an ad unit:

<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
    <!-- responsive_ad -->
    <ins class="adsbygoogle"
        style="display:block"
        data-ad-client="ca-pub-####"
        data-ad-slot="######"
        data-ad-format="auto"
        data-adtest="on"
        data-full-width-responsive="true">
    </ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

After testing in Chrome, Firefox and Safari a white box is rendered. After searching I'm not able to locate any solution in product forums or on any site. How can I test an ad unit in localhost?


Solution

I also tried the data-adtest="on" approach and it didnt work for me either. I found two different approaches for testing:

  • Using Placeholders, the way most Google Adsense Forum Entry suggesting it. e. g. here: https://productforums.google.com/forum/#!msg/adsense/RLeUEq6na-w/i93-TFVRiHUJ (I've tried it on my own and I was satisfied with the solution)
  • Use your own (dev) domain explained here: https://medium.com/@jeffersonlicet/testing-ads-on-localhost-300215b415d6 (I was not able to tried this on my own, because I dont have a domain)

If you have opened the developer console you would have saw that you got an 403 Error. The reasons for this error are further explained here: https://www.shoutmeloud.com/adsense-403-forbidden-error.html. To make it short: Adsense Crawlers reject Localhost. Thats why you got a white space instead of ads.

But if there is a third way I would also be glad to hear it.



Answered By - tschomacker
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home
View mobile version

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing