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!
via Chris Palmer Breuer