Guest User

Untitled

a guest
Apr 20th, 2016
1,472
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var AdBlockUtil = (function()
  2. {  
  3.    
  4.     var adBlockEnabled = false;
  5.     function checkRedirect()
  6.     {
  7.         if(document.location.href.indexOf("haaretz.co.il") < 0) // avoid redirect in preview pages: check that we're in "haaretz.co.il" domain
  8.         {
  9.             return;
  10.         }
  11.         $.ajax({
  12.             url: 'https://tpc.googlesyndication.com/simgad/1909367504824409356',
  13.             type: "GET",
  14.             error: function(jqXHR, textStatus, errorThrown) {
  15.                 adBlockEnabled = true;
  16.                 //console.log("adBlock Enabled");
  17.                 checkRedirectAfterAdBlockDetection();
  18.             },
  19.             success:  function(data, textStatus, jqXHR) {
  20.                 adBlockEnabled = false;
  21.                 //console.log("adBlock Disabled");
  22.                 checkRedirectAfterAdBlockDetection();
  23.             }
  24.         });
  25.     }
  26.  
  27.    
  28.     var redirectProbabilityPercent = redirectProbabilityPercentTest;
  29.     var url = encodeURIComponent(document.location.href);
  30.     if(url.indexOf("www.haaretz.co.il") >= 0)
  31.     {
  32.         redirectProbabilityPercent = redirectProbabilityPercentProd;
  33.     }
  34.    
  35.     function getParameterByName(name)
  36.     {
  37.         name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
  38.         var regex = new RegExp("[\\?&]" + name + "=([^&#]*)");
  39.         var results = regex.exec(location.search);
  40.         return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
  41.     }
  42.    
  43.     function getUserType()
  44.     {
  45.         CookieUtil.reinit();
  46.         var cookieMap = CookieUtil.getCookieMap();
  47.         var isTMuser = cookieMap['tmsso'] ? 'registered' : 'anonymous';
  48.         return cookieMap['HtzPusr'] ? 'paying' : isTMuser;
  49.     }
  50.    
  51.     function isOnBlockedPage()
  52.     {
  53.         return document.location.href.indexOf('/hblocked') > 0 || document.location.href.indexOf('/vblocked') > 0;
  54.     }
  55.    
  56.     function isAllowedPage() // pages allows for adBlock -> do not redirect from these pages. homepage, blocked page, promotions page, login page
  57.     {
  58.         return (typeof isInHomePage !== 'undefined' && isInHomePage) || isOnBlockedPage() || document.location.href.indexOf("/promotions") > 0 ||  document.location.href.indexOf("/login-page") > 0;
  59.     }
  60.    
  61.     function goBack()
  62.     {
  63.         // redirect back to original page or homepage
  64.         var returnTo = getParameterByName("returnTo");
  65.         if(returnTo == "")
  66.         {
  67.             document.location.href = "/";
  68.         }
  69.         else
  70.         {
  71.             document.location.href = returnTo;
  72.         }
  73.     }
  74.    
  75.     function doAction(action, onSuccess)
  76.     {  
  77.         var url = encodeURIComponent(document.location.href);
  78.        
  79.         var HtzRusr  = "Rusr=";
  80.         if(typeof CookieUtil.getCookie('HtzRusr') !== "undefined")
  81.         {
  82.             HtzRusr = HtzRusr + CookieUtil.getCookie('HtzRusr')
  83.         }
  84.        
  85.         var HtzPusr  = "Pusr=";
  86.         if(typeof CookieUtil.getCookie('HtzPusr') !== "undefined")
  87.         {
  88.             HtzPusr = HtzPusr + CookieUtil.getCookie('HtzPusr')
  89.         }
  90.              
  91.         var sendTo = 'http://ds.haaretz.co.il/dstest.php?';
  92.          
  93.         if(url.indexOf("www.haaretz.co.il") >= 0)
  94.         {
  95.             sendTo = 'http://ds.haaretz.co.il/ds.php?';
  96.         }
  97.              
  98.        
  99.         var domain = "domain=" + window.location.hostname;
  100.         var urlS = "url=" + url;
  101.          
  102.         var userId = "userId="
  103.                
  104.         if(typeof CookieUtil.getCookie("tmsso") !== "undefined")
  105.         {
  106.             userId = userId + CookieUtil.getCookie("tmsso").userId;
  107.         }
  108.         else if(typeof CookieUtil.getCookie("anonymousId") !== "undefined")
  109.         {
  110.             userId = userId + CookieUtil.getCookie("anonymousId");
  111.         }
  112.         else
  113.         {
  114.             return;
  115.         }
  116.          
  117.         data = urlS + "&" + domain + "&" + HtzRusr + "&" + HtzPusr + "&" +  userId + "&actionType=" + action;
  118.          
  119.         jQuery.ajax({
  120.                url: sendTo + data,
  121.                dataType: 'text',
  122.                type : "GET",
  123.                success : onSuccess
  124.         });
  125.     }
  126.  
  127.    
  128.     function checkRedirectAfterAdBlockDetection()
  129.     {
  130.         if(typeof CookieUtil.getCookie("skipAdblockRedirect") !== 'undefined')
  131.         {
  132.             return;
  133.         }
  134.        
  135.         if(isOnBlockedPage()) // set link actions on blocked pages
  136.         {
  137.             // set abandon logic, in case user leaves the page
  138.             $(window).on('beforeunload', function() {
  139.                  doAction(18, null); // report: abandon
  140.                  return undefined;
  141.             });
  142.            
  143.             // set other click action which will send a report and also cancel the abandon logic, e.g. if user clicks the login button, it will not be considered an abandonment
  144.             $("#go_to_purchase").click(function() {
  145.                 doAction(17, function(){$(window).off('beforeunload'); document.location.href = "https://www.haaretz.co.il/promotions-page";}); // report: go to purchase
  146.             });
  147.             var returnTo = getParameterByName("returnTo");
  148.             if(returnTo != "")
  149.             {
  150.                 $("#adblock_returnto").click(function() {
  151.                     $(window).off('beforeunload');
  152.                     document.location.href = returnTo;
  153.                 });
  154.                
  155.                 $("#go_to_login").click(function() {
  156.                     doAction(16, function(){$(window).off('beforeunload'); document.location.href = "/misc/login-page?returnTo=" + encodeURIComponent(returnTo);}); // report: go to login
  157.                 });
  158.                
  159.                 $(".js-login").attr("href","#");
  160.                 $(".js-login").click(function() {
  161.                     doAction(16, function(){$(window).off('beforeunload'); document.location.href = "/misc/login-page?returnTo=" + encodeURIComponent(returnTo);}); // report: go to login
  162.                 });
  163.             }
  164.             else
  165.             {
  166.                 $("#adblock_returnto").click(function() {
  167.                     $(window).off('beforeunload');
  168.                     document.location.href = "";
  169.                 });
  170.                
  171.                 $("#go_to_login").click(function() {
  172.                     doAction(16, function(){$(window).off('beforeunload'); document.location.href = "/misc/login-page";}); // report: go to login
  173.                 });
  174.                
  175.                 $(".js-login").attr("href","#");
  176.                 $(".js-login").click(function() {
  177.                     doAction(16, function(){$(window).off('beforeunload'); document.location.href = "/misc/login-page";}); // report: go to login
  178.                 });
  179.             }
  180.         }
  181.         if(!adBlockEnabled) // adBlock disabled
  182.         {
  183.             if(typeof CookieUtil.getCookie("adblockRedirect") !== 'undefined' && !isOnBlockedPage())
  184.             {
  185.                 CookieUtil.deleteCookie("adblockRedirect",  '/', 'haaretz.co.il'); // delete cookie
  186.                 localStorage.adblock_removed = true;
  187.                 doAction(15, function()
  188.                 {
  189.                     if(isOnBlockedPage())
  190.                     {
  191.                         $(window).off('beforeunload'); 
  192.                     }
  193.                 }); // report: user disabled adBlock for www.haaretz.co.il
  194.             }
  195.         }
  196.         else // adBlock enabled
  197.         {
  198.             localStorage.adblock_removed = false;
  199.             if(getUserType() == "paying") // paying user
  200.             {
  201.                 if(isOnBlockedPage())
  202.                 {
  203.                     goBack();
  204.                 }
  205.                 else
  206.                 {
  207.                     return;
  208.                 }
  209.             }
  210.             else if(isAllowedPage()) // if user is on a page defined not to be redirected -> return (do nothing)
  211.             {
  212.                 return;
  213.             }
  214.             else if(getUserType() != "paying") // apply redirect logic for anonymous or registered users only
  215.             {
  216.                 if(typeof CookieUtil.getCookie("adblockRedirect") !== 'undefined') // if user already has a blocked page cookie, redirect.
  217.                 {
  218.                     document.location.href = "/" + CookieUtil.getCookie("adblockRedirect") + "?returnTo=" + encodeURIComponent(document.location.href);
  219.                     return;
  220.                 }
  221.                 else // user does not have a blocked page cookie -> calculate probability for redirect
  222.                 {
  223.                     var rand = Math.random();
  224.                     if(rand < redirectProbabilityPercent / 100) // we have a winner! redirect and set cookie for next visits -> user will be redirected for 90 days
  225.                     {
  226.                         var expireMs = Date.now() + ( 90*24*3600*1000); // 90 days
  227.                         var expire = new Date(expireMs);
  228.                         if(Math.random() < 0.5) // A/B testing
  229.                         {
  230.                             CookieUtil.setCookie('adblockRedirect', 'hblocked', '/', 'haaretz.co.il', expire);
  231.                             document.location.href = "/hblocked?returnTo=" + encodeURIComponent(document.location.href);
  232.                         }
  233.                         else
  234.                         {
  235.                             CookieUtil.setCookie('adblockRedirect', 'vblocked', '/', 'haaretz.co.il', expire);
  236.                             document.location.href = "/vblocked?returnTo=" + encodeURIComponent(document.location.href);
  237.                         }
  238.                     }
  239.                     else // set cookie for next visits, user will not be redirected for at least 3 more days
  240.                     {
  241.                         var expireMs = Date.now() + (3*24*3600*1000); // 3 days
  242.                         var expire = new Date(expireMs);
  243.                         CookieUtil.setCookie('skipAdblockRedirect', 'true', '/', 'haaretz.co.il', expire);
  244.                     }
  245.                 }
  246.             }
  247.         }
  248.     }
  249.    
  250.     return{
  251.         "checkRedirect":function(){
  252.             checkRedirect();   
  253.         },
  254.         "isOnBlockedPage":function(){
  255.             return isOnBlockedPage();  
  256.         }
  257.     };
  258. })();
Advertisement
Add Comment
Please, Sign In to add comment