Last active
June 29, 2022 11:12
-
-
Save marklchaves/0210d4383c00d3446895e1c57b3d82f5 to your computer and use it in GitHub Desktop.
Check if adblockers like Ghostery block Popup Maker REST API calls
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
<?php // Ignore this first line when copying to your child theme's functions.php file. | |
add_action( 'wp_footer', function () { ?> | |
<script> | |
(function () { | |
function testRestApi(url, cb) { | |
jQuery.ajax({ | |
url: url, | |
dataType: "text", | |
type: "GET", | |
complete: function (xhr) { | |
if (typeof cb === "function") cb.apply(this, [xhr.status]); | |
}, | |
error: function () { | |
console.log("[PUM] AJAX ERROR"); | |
}, | |
}); | |
} | |
/** | |
* 1. Change twenty-twentyone.local to your website's domain | |
* 2. Add a div with id="ajax-error" to the page with your popup(s). | |
*/ | |
testRestApi( | |
"https://twenty-twentyone.local/wp-json/pum/v1/analytics/?event=open&pid=118", | |
function (status) { | |
console.log(`[PUM] testRestApi status: ${status} `); | |
switch (status) { | |
case 204: | |
console.log("[PUM] 204: That URL works!"); | |
break; | |
case 404: | |
console.log("[PUM] 404: Cannot find that URL."); | |
break; | |
case 0: | |
console.log("[PUM] BLOCKED: Cannot reach that URL."); | |
const ajaxMessage = document.querySelector("#ajax-error"); | |
if (ajaxMessage) { | |
ajaxMessage.append( | |
(document.createElement("p").textContent = | |
"Ooops! Can you turn off Ghostery so you can see all of our great content? Thank you ;-)") | |
); | |
} | |
break; | |
default: | |
console.log("[PUM] UNKNOWN: Cannot reach that URL."); | |
} | |
} | |
); | |
})(); | |
</script> | |
<?php } ); | |
/** | |
* You can add the PHP code snippet to your child theme's functions.php file | |
* or with third-party plugins such as My Custom Functions and Code Snippets. | |
* | |
* Learn more: | |
* - https://docs.wppopupmaker.com/article/84-getting-started-with-custom-js | |
* - https://docs.wppopupmaker.com/article/552-getting-started-with-custom-php | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment