arie_cristianD

inject Google Ad Manager on auto load post

Feb 12th, 2026
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function ($) {
  2.     "use strict";
  3.     $(function () {
  4.  
  5.         /* this function will render ad slot on first render (the ad that already defined on header) */
  6.  
  7.         /* Article Ad 1 */
  8.         googletag.cmd.push(function () { googletag.display('div-gpt-ad-1'); });
  9.         /* Article Ad 2 */
  10.         googletag.cmd.push(function () { googletag.display('div-gpt-ad-2'); });
  11.     })
  12.  
  13.  
  14.     /* this function will define the new ad slot and render it when new post is rendered on autoload posts*/
  15.     $(document).on('jnews-ajax-load', function (event, $newPost) {
  16.  
  17.         /* Article Ad 1 */
  18.         var $adPlaceholder1 = $newPost.find('.my-ad-class-1');
  19.         $adPlaceholder1.each(function () {
  20.             var uniqueId = 'gpt-ad-1-' + Date.now() + Math.floor(Math.random() * 100);
  21.             $(this).attr('id', uniqueId);
  22.  
  23.             console.log('uniqueId is generated ' + uniqueId);
  24.  
  25.             googletag.cmd.push(function () {
  26.                 var slot = googletag.defineSlot('/6499/example/banner', [728, 90], uniqueId).addService(googletag.pubads());
  27.                 googletag.display(uniqueId);
  28.                 googletag.pubads().refresh([slot]);
  29.             });
  30.         });
  31.  
  32.         /* Article Ad 2 */
  33.         var $adPlaceholder2 = $newPost.find('.my-ad-class-2');
  34.         $adPlaceholder2.each(function () {
  35.             var uniqueId = 'gpt-ad-2-' + Date.now() + Math.floor(Math.random() * 100);
  36.             $(this).attr('id', uniqueId);
  37.  
  38.             googletag.cmd.push(function () {
  39.                 var slot = googletag.defineSlot('/6499/example/banner', [728, 90], uniqueId).addService(googletag.pubads());
  40.                 googletag.display(uniqueId);
  41.                 googletag.pubads().refresh([slot]);
  42.             });
  43.         });
  44.     });
  45.  
  46.  
  47. })(jQuery);
  48.  
Advertisement
Add Comment
Please, Sign In to add comment