Advertisement
clickio

Consent ads pausing

Jul 20th, 2020
1,244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function(w) {
  2.     // initialize Ad Manager globals
  3.     w.googletag = w.googletag || {};
  4.     w.googletag.cmd = w.googletag.cmd || [];
  5.     w.adsbygoogle = w.adsbygoogle || [];
  6.  
  7.     var pauseAdSenseAds = true;
  8.  
  9.     // Consent tool logic ready queue
  10.     w.consentCallbackQueue = (function(undefined) {
  11.         let timeoutCallInSeconds  = 5; // false to disable
  12.  
  13.         let queue                 = [];
  14.         let startImmediately      = false;
  15.         let consentCallbackCalled = false;
  16.  
  17.         let addFunction = function (callback) {
  18.             if (startImmediately) {
  19.                 callback();
  20.             } else {
  21.                 queue.push(callback);
  22.             }
  23.         };
  24.  
  25.         let runQueue = function () {
  26.             startImmediately = true;
  27.             queue.map(function(callback, i){
  28.                 if (callback !== undefined) {
  29.                     callback();
  30.                     queue[i] = undefined;
  31.                 }
  32.             });
  33.         };
  34.  
  35.         // callback which is executed by CCT code upon consent state determination
  36.         (w.__lxG__consent__ = w.__lxG__consent__ || {}).consentCallback = function(consentState){
  37.             consentCallbackCalled = true;
  38.  
  39.             if (consentState === null) {
  40.                 // consent not applicable, non-eu user, executing queue right away
  41.                 runQueue();
  42.             } else if (consentState === -1) {
  43.                 // eu user, consent interface shown, cmp loaded, user has not decided yet
  44.             } else if (consentState === 0) {
  45.                 // eu user, consent rejected, for custom advertisements systems be sure to turn on anonymous ads here
  46.                 runQueue();
  47.             } else if (consentState === 1) {
  48.                 // eu user, consent accepted, executing the queue
  49.                 runQueue();
  50.             }
  51.         };
  52.  
  53.         // in case of network problems in loading consent.js
  54.         if (timeoutCallInSeconds) {
  55.             setTimeout(function () {
  56.                 if (!consentCallbackCalled) {
  57.                     // notify code there is no consent
  58.                     if (console && console.log) {
  59.                         // debug message
  60.                         console.log('consentCallbackQueue timeout call');
  61.                     }
  62.  
  63.                     // force-disable personalized ads, if not implemented in __lxG__consent__.consentCallback
  64.                     if (w.googletag.pubads) {
  65.                         w.googletag.pubads().setRequestNonPersonalizedAds(1);
  66.                     } else {
  67.                         w.googletag.cmd.unshift(function () {
  68.                             w.googletag.pubads().setRequestNonPersonalizedAds(1);
  69.                         });
  70.                     }
  71.                    
  72.                     // calling function to trigger logic
  73.                     w.__lxG__consent__.consentCallback(0);
  74.                 }
  75.             }, timeoutCallInSeconds*1000);
  76.         }
  77.  
  78.         return {
  79.             push: addFunction
  80.         };
  81.     })();
  82.  
  83.     // function to display AdManager slot by it`s div ID
  84.     // it supports automatic slot refresh if disableInitialLoad was called before
  85.     w.displayAndRefreshSlotById = function (slotId) {
  86.         consentCallbackQueue.push(function () {
  87.             googletag.cmd.push(function () {
  88.                 if (w.pubads().isInitialLoadDisabled()) {
  89.                     googletag.pubads().getSlots().forEach(function (slot) {
  90.                         if (slot.getSlotElementId() == slotId) {
  91.                             googletag.display(slotId);
  92.                             googletag.pubads().refresh([slot]);
  93.                         }
  94.                     });
  95.                 } else {
  96.                     googletag.display(slotId);
  97.                 }
  98.             });
  99.         });
  100.     }
  101.     // function for AdSense slots
  102.     w.displayAdSenseSlots = function () {
  103.         consentCallbackQueue.push(function () {
  104.             var adSenseSlots = document.querySelectorAll('ins.adsbygoogle');
  105.             adSenseSlots.forEach(function(item){
  106.                 if(item.innerHTML.trim() === "")
  107.                 {
  108.                     (adsbygoogle = window.adsbygoogle || []).push({});
  109.                 }
  110.             });
  111.         });
  112.     }
  113. })(window);
  114.  
  115. window.displayAdSenseSlots();
  116.  
  117. // for AdManager slots you should replace
  118. // googletag.cmd.push(function () {
  119. //     googletag.display('div-id-1');
  120. // });
  121. // with
  122. // displayAndRefreshSlotById('div-id-1');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement