Chris Palmer Breuer
Chris Palmer Breuer

Reputation: 690

How to ignore Adsense when pulling in AJAX data?

I use Google Adsense and infinite load. Whenever I "load more" data on the page through AJAX, it automatically pulls in another ad from Google as well, which obviously is not displayed since Google does not support to bring in ads through AJAX.

I need help with the logic of how I can only show 1 ad on the load of the page and whenever the load more button is pressed, how to ignore the @include ('components.responsive_ad_display_only')

I currently thought about using sessions. I am using session()->put('retrieve', '1'); right after I include the ad. Then once "load more" is pressed, I check if the session is set. If it is set I am not including the ad while pulling in more data. The issue with this scenario is the fact that I need to "forget" the session eventually, so that when the visitor refreshes the homepage, a new ad would appear.

This is the code I use:

@php
if (! session()->has('retrieve')) {
    $i = 1;
    $rand = rand(1, 10);
    session()->put('retrieve', '1');
} else {
    $i = 1;
    $rand = 200; // any integer higher than media items displayed
    session()->forget('retrieve');
}
@endphp

@foreach ($media as $media_item)
    @if ($i == $rand)
        <div class="ad_item">
            <!-- Ad Displayed here -->
            @include('components.responsive_ad_display_only')
        </div> 
    @endif
    <div class="media_item">
        <h1>Media Item Name</h1>
    </div>
@endforeach

This code snippet works for not displaying an ad every other time, because the session keeps getting set and unset each time.

I would appreciate any help and direction towards solving my dilemma.

Thanks a lot!

Upvotes: 0

Views: 79

Answers (2)

El Don
El Don

Reputation: 912

Use this method to display the ads with ajax once upon some time

$i = rand(1, 10);
if ($i == 1 || $i == 4 || $i == 8){
?>
<div class="ad_item">
    <!-- Ad Displayed here -->
    <?php 
        @include('components.responsive_ad_display_only');
    ?>
</div> 
<?php
foreach ($media as $media_item){

    }
    ?>
    <div class="media_item">
        <h1>Media Item Name</h1>
</div>
}

Now you can use the session to prevent the continuous display of ads this way

if(isset($_SESSION["some_key"])){
    if($_SESSION["some_key"] == 1 ||  $_SESSION["some_key"]== 4 ||
       $_SESSION["some_key"] == 8){ // The values for which ads show up
        $i == 0; // A value for which ads won't show up

Now you can delete that key once you don't need it and reset it if you do....

Upvotes: 0

El Don
El Don

Reputation: 912

To prevent the ad from showing up on ajax requests there are many ways including but not limited to

  • Adding a parameter in the ajax request url

Use it like

//in JS
...
url = "/somefile.php?page=1";
// update it to look like
url = "/somefile.php?page=1&ajax=1";
...

//in PHP
if(!isset($_GET["ajax"]) || $_GET["ajax"] != 1){ //check ajax param in URL
    /// Print your ad here
  • Checking the headers using PHP (not sure way, but it works almost everywhere)

Like this

if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) &&
   strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
{ 
    /// Print your ad here

There are other ways involving sessions, and other types of verification but these too are the simplest

Upvotes: 1

Related Questions