<script async src="https://securepubads.g.doubleclick.net/tag/js/gpt.js"></script>
<script>
  window.googletag = window.googletag || {cmd: []};
  googletag.cmd.push(function() {
    googletag.defineSlot('/23032124618/shl_ff_LR1', [[200, 200], [300, 250], [250, 250], [336, 280]], 'div-gpt-ad-1775504045871-0').addService(googletag.pubads());
    googletag.defineSlot('/23032124618/shl_ff_LR2', [[300, 250], [250, 250], [336, 280], [200, 200]], 'div-gpt-ad-1775504174544-0').addService(googletag.pubads());
    googletag.pubads().enableSingleRequest();
    googletag.enableServices();
  });
</script>

<script async src="https://securepubads.g.doubleclick.net/tag/js/gpt.js"></script>
<script>
  window.googletag = window.googletag || { cmd: [] };
  var interstitialSlot;
  googletag.cmd.push(function () {
    interstitialSlot = googletag.defineOutOfPageSlot('/23032124618/shl_ff_inter', googletag.enums.OutOfPageFormat.INTERSTITIAL).addService(googletag.pubads());
    googletag.enableServices();
  });
  googletag.cmd.push(function () {
    googletag.display(interstitialSlot);
  });
</script>


<!-- Rewarded Code Starts -->
<script async='async' src='https://securepubads.g.doubleclick.net/tag/js/gpt.js'></script>
<script>
  window.googletag = window.googletag || {cmd: []};

  // SESSION LIMIT CONFIG
  var MAX_ADS_PER_SESSION = 1;
  var adCount = sessionStorage.getItem("rewarded_ad_count");

  if (!adCount) {
    sessionStorage.setItem("rewarded_ad_count", 0);
    adCount = 0;
  } else {
    adCount = parseInt(adCount);
  }

  googletag.cmd.push(function() {
    googletag.pubads().setTargeting('id_post_wp', ['']);

    if (window.location.search) {
      var queryString = window.location.search;
      var urlParams = new URLSearchParams(queryString);
      var utm_source = urlParams.get('utm_source');
      var utm_medium = urlParams.get('utm_medium');
      var utm_campaign = urlParams.get('utm_campaign');

      if (utm_source) {
        googletag.pubads().setTargeting('utm_source', [utm_source]);
      }
      if (utm_medium) {
        googletag.pubads().setTargeting('utm_medium', [utm_medium]);
      }
      if (utm_campaign) {
        googletag.pubads().setTargeting('utm_campaign', [utm_campaign]);
      }
    }
  });

  // REWARDED AD WITH LIMIT
  googletag.cmd.push(function() {

    // CHECK LIMIT BEFORE LOADING AD
    if (adCount >= MAX_ADS_PER_SESSION) {
      console.log("Ad limit reached for this session.");
      return;
    }

    var rewardSlot = googletag.defineOutOfPageSlot('/23032124618/shl_ff_rwd', googletag.enums.OutOfPageFormat.REWARDED);

    if (rewardSlot) {
      rewardSlot.addService(googletag.pubads());

      googletag.pubads().addEventListener('rewardedSlotReady', function(event) {
        if (event.slot === rewardSlot) {
          console.log('Rewarded ad ready.');

          event.makeRewardedVisible();

          // INCREMENT COUNT WHEN AD IS SHOWN
          adCount++;
          sessionStorage.setItem("rewarded_ad_count", adCount);
        }
      });

      googletag.pubads().addEventListener('rewardedSlotGranted', function(event) {
        if (event.slot === rewardSlot) {
          console.log('Reward granted.');
        }
      });

      googletag.pubads().addEventListener('rewardedSlotClosed', function(event) {
        if (event.slot === rewardSlot) {
          console.log('Ad closed.');
        }
      });
    }

    googletag.pubads().addEventListener('slotRenderEnded', function(event) {
      if (event.slot === rewardSlot) {
        console.log('Ad rendered.');
        if (event.isEmpty) {
          console.log('No ad returned.');
        }
      }
    });

    googletag.enableServices();
    googletag.display(rewardSlot);
  });
</script>
<!-- Rewarded Code Ends -->

