Home › Forums › JavaScript › Beset Practices for adding jQuery code before tag
- This topic is empty.
-
AuthorPosts
-
January 6, 2016 at 12:41 pm #236462
Crssp
ParticipantI have this jQuery code from a tutorial, but not sure if it should be just wrapped in <script> tags or it should use some sort of document ready???
The code adds an event to your Google Analytics tracking to track Ad Blocker uses among your users, and was from the tutorial here:
https://www.isaumya.com/how-to-track-adblock-users-in-wordpress-with-google-analytics/
…
jQuery(function ($) {
/Checking Adblock enabled and pushing it to analytics/
if ($(“.adsbygoogle”).length > 0 ){
if (typeof(window.google_jobrunner) === “undefined”) {
__gaTracker(‘send’, ‘event’, ‘Adblock’, ‘Blocked’, {‘nonInteracion’:1});
} else {
__gaTracker(‘send’, ‘event’, ‘Adblock’, ‘Unblocked’, {‘nonInteracion’:1});
}
}
});
…January 6, 2016 at 12:48 pm #236463Crssp
ParticipantLooks like maybe I’ll just go with this script from labnol instead.
…
<script>window.onload = function() {
// Delay to allow the async Google Ads to load setTimeout(function() { // Get the first AdSense ad unit on the page var ad = document.querySelector("ins.adsbygoogle"); // If the ads are not loaded, track the event if (ad && ad.innerHTML.replace(/\s/g, "").length == 0) { if (typeof ga !== 'undefined') { // Log an event in Universal Analytics // but without affecting overall bounce rate ga('send', 'event', 'Adblock', 'Yes', {'nonInteraction': 1}); } else if (typeof _gaq !== 'undefined') { // Log a non-interactive event in old Google Analytics _gaq.push(['_trackEvent', 'Adblock', 'Yes', undefined, undefined, true]); } } }, 2000); // Run ad block detection 2 seconds after page load
};
</script>
…http://www.labnol.org/internet/adblock-with-google-analytics/28819/
January 6, 2016 at 1:41 pm #236465Shikkediel
Participantit should use some sort of document ready
It does already – this is shorthand for it :
jQuery(function ($) { ... });
If you’re placing it inside the HTML structure, it will indeed need script tags around it.
-
AuthorPosts
- The forum ‘JavaScript’ is closed to new topics and replies.