Last active
October 14, 2015 16:36
-
-
Save vegagame/6f4ee2f65961e2f70751 to your computer and use it in GitHub Desktop.
AdBlock A/B test code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This code work with: | |
* jQuery 1.x | |
* Google Analytics trackin code | |
* https://github.com/sitexw/BlockAdBlock/blob/master/blockadblock.js | |
* https://github.com/carhartl/jquery-cookie/blob/master/src/jquery.cookie.js | |
* #article Article id | |
* #overlay transparence overlay | |
*/ | |
var adbStart = false; | |
var percent = 0.2; | |
var adbCookieName = "ADBLOCK"; | |
function adBlockNotDetected() { | |
adbc = $.cookie(adbCookieName); | |
if (adbc) { | |
adb = JSON.parse(adbc); | |
if (adb.hasADB && adb.inSample) { | |
$.cookie(adbCookieName, JSON.stringify({hasADB: false, inSample: false}), {expires: 180, path: '/'}); | |
adbNotifyGA('Adblock', 'Removed'); | |
} | |
} | |
} | |
function adBlockDetected() { | |
adbNotifyGA('Adblock', 'Detected'); | |
adbc = $.cookie(adbCookieName); | |
if (!adbc) { | |
extractRandomUser(); | |
} else { | |
if (JSON.parse(adbc).inSample) { | |
showAdBPolicy(); | |
} | |
} | |
} | |
function extractRandomUser() { | |
var adbNumber = Math.random(); | |
if (adbNumber <= percent) { | |
$.cookie(adbCookieName, JSON.stringify({hasADB: true, inSample: true}), {expires: 180, path: '/'}); | |
showAdBPolicy(); | |
} else { | |
$.cookie(adbCookieName, JSON.stringify({hasADB: true, inSample: false}), {expires: 180, path: '/'}); | |
} | |
} | |
function checkIfUserRemoveLayer() { | |
h = $("#overlay").height(); | |
w = $("#overlay").width(); | |
d = $("#overlay").css('display'); | |
if ((h + w) == 0 || d == 'none') { | |
adbNotifyGA('Adblock', 'Bypassed'); | |
} | |
} | |
function showAdBPolicy() { | |
adbNotifyGA('Adblock', 'Selected'); | |
if (!adbStart) { | |
var ah = $("#article").height(); | |
$("#article").before("<div id='overlay'></div>"); | |
$("#overlay").height(ah - 110); | |
} | |
adbStart = true; | |
checkIfUserRemoveLayer(); | |
} | |
function adbNotifyGA(key, value) { | |
if (typeof _gaq != 'undefined') { | |
_gaq.push(['_setCustomVar', 1, key, value, 1]); | |
_gaq.push(['reportglobale._setCustomVar', 1, key, value, 1]); | |
_gaq.push(['_trackEvent', key, value]); | |
} | |
} | |
if (typeof blockAdBlock === 'undefined') { | |
adBlockDetected(); | |
} else { | |
blockAdBlock.onDetected(adBlockDetected); | |
blockAdBlock.onNotDetected(adBlockNotDetected); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment