If you are reading this article on briancaffey.github.io/2021/10/31/how-and-why-i-added-adsense-and-adblock-detector-to-my-website, then you will be prompted to pause your adblocker if you are using one. If you are not using an ad blocker, I recommend that you consider installing one.
Reading this article on a browser with AdBlock enabled will allow you to see how I detect AdBlock and ask people to pause it when they are on my site.
This article is a deep dive on how I added ads to my site with Google AdSense and how I request that visitors to my site pause AdBlock so that I can make more money from Google AdSense.
I have been enjoying using my GitHub Pages website to learn more about static sites, JAMStack, and Nuxt.js, an awesome Vue.js framework with support for building statically generated sites. I have been able to learn and implement several different features which I have written about on my blog. Some examples include:
I have also been learning the suite of Google tools for monitoring and measuring traffic to my site, including Google Analytics and Google Search Console. Google Search Console is helpful for understanding the search terms that people are using when searching Google that result in organic traffic to my site.
At one point I found out that another website was using the same Google Tracking code that I had previously hard-coded into an old version of my website, and my Google Analytics started measuring traffic to URLs that I didn't recognize as belonging to my site. I was able to fix this by adding a Hostname filter rule in Google Analytics.
One area that I have not had any experience with until recently is Google AdSense. Google AdSense allows you to place ads on your website. Here's an overview of what I did to get started:
ads.txt
file generated by Google AdSense to my site
Here's the address confirmation code that I received from Google:
Here's the config code for AdSense from nuxt.config.js
:
/*
** Nuxt.js modules
*/
modules: [
// Doc: https://axios.nuxtjs.org/usage
'@nuxtjs/axios',
// Doc: https://github.com/nuxt/content
'@nuxt/content',
// Doc: https://www.npmjs.com/package/@nuxtjs/sitemap
'@nuxtjs/sitemap',
'@nuxtjs/feed',
'nuxt-i18n',
['@nuxtjs/google-adsense', { <-- AdSense config
id: 'ca-pub-4924597640144289'
}]
],
The process was pretty simple. Google now automatically places ads on my site in a few different formats:
<adsbygoogle />
Vue component
When everything was set up properly I started seeing ads on my site, and I see a non-zero value in my estimated earnings in the AdSense console. Google has a payment threshold of $100, so I need make this amount before I can start receiving money from Google.
My estimated earning report shows that I make between $0 and $4.32 in ad sales per day. I'm interested to see how much I can make with an article that I post across the many different channels that I can publish to. I explored this in a previous article, but the main channels I can use for sharing content are:
This article should be a good place to start exploring how effective the different channels are in driving content to my site, and I'll update this article later with more details and numbers from my AdSense reports.
I assume that most people reading my blog have an AdBlock extension installed in their browser like I do, such as AdBlock or ABP (AdBlock Pro). This got me thinking about how I could implement a simple AdBlock detector for my site that would hide the contents of the page if AdBlock is enabled.
I started with this question, and I came across this StackOverflow question which inspired the code that I am now using on this site to detect AdBlock.
There are a few different parts of my Nuxt Application that work together to detect if AdBlock is active and request that the user pause AdBlock for the site. The main components are:
AdBlockBlocker
adblock
default.vue
layout that is used for almost all of the pages on my sitePleaseDisableAdblock.vue
Here's an overview of each part:
AdBlockBlocker.vue
This is the key part of how the AdBlock detection works. If the client is unable to download the adsbygoogle.js
file, then that indicates that the user is using AdBlock.
<template>
<div />
</template>
<script>
export default {
mounted () {
// if adblock is detected through a value that is set local storage, then show the AdBlock message
// the `adblockEnabled` value is set in the catch block of the `detectAdBlock` method
// (see adblock/setAdblockEnabled mutation)
if (JSON.parse(localStorage.getItem('adblockEnabled')) === true) {
this.$store.commit('adblock/setAdblockEnabled', true)
}
// check to see if the URL can be accessed on a 5 second interval
setInterval(() => {
this.detectAdBlock()
}, 5000)
},
methods: {
async detectAdBlock () {
// this is a URL that should be blocked by AdBlock
const googleAdUrl = 'https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js'
// make a request to the above URL
await fetch(new Request(googleAdUrl)).then((_) => {
// isAdblockEnabled is false by default
// Check to see if isAblockEnabled was set to true by a previous request
if (this.$store.getters['adblock/isAdblockEnabled'] === true) {
// if the request was successful, then the user does not have AdBlock enabled,
// so we can set isAdblockEnabled to false using the setAdblockEnabled mutation
// this mutation will also set the `adblockEnabled` value in local storage to "false"
// `adblockEnabled` be `JSON.parse`d since it is saved in localStorage as a string
this.$store.commit('adblock/setAdblockEnabled', false)
window.localStorage.setItem('adblockEnabled', 'false')
// do a full reload of the page
window.location.reload()
}
}).catch((_) => {
// if the request was unsuccessful, then the user has AdBlock enabled.
// we can set isAdblockEnabled to true using the setAdblockEnabled mutation
// this will also set the `adblockEnabled` value in local storage to "true"
this.$store.commit('adblock/setAdblockEnabled', true)
window.localStorage.setItem('adblockEnabled', 'true')
})
}
}
}
</script>
One other important point about this component is that it runs AdBlock detection using setInterval
, meaning that it will check if AdBlock is enabled every few seconds while a user is on my site.
PleaseDisableAdblock.vue
component.false
.
Vuex Store
This is a very simple Vuex store module. The isAdblockEnabled
getter will be used in the default.vue
layout component.
let initialValue = false
if (process.window) {
initialValue = JSON.parse(window.localStorage.getItem('adblockEnabled')) || false
}
export const state = () => ({
adblockEnabled: initialValue
})
export const getters = {
isAdblockEnabled: state => state.adblockEnabled
}
export const mutations = {
setAdblockEnabled (state, payload) {
state.adblockEnabled = payload
}
}
default.vue layout logic
<template>
<div>
<Navigation />
<PleaseDisableAdblock v-if="$store.getters['adblock/isAdblockEnabled']" />
<Nuxt v-else />
<AdBlockerBlocker />
<Footer />
</div>
</template>
Content to show to request that a user pauses AdBlock
When a user has AdBlock enabled, I show a simple message that asks them to please disable AdBlock. I also want to invite people who do not wish to disable AdBlock to read my blog directly on GitHub, or to read it without ads by cloning or forking my repo, building it, and running it in development mode with yarn dev
.
Being asked to pause AdBlock is increasingly common. I have noticed that the experience is not the same on each site that requests AdBlock be paused. For example:
I remember being able to delete AdBlock detection modals and backgrounds on some sites as a way of getting around ad-block detectors. This is as easy as Cmd + Shift + C
, click on the element that is blocking the page content and delete
the selected element.
One of the reasons I wanted to implement AdBlock detection is to see what is possible from the perspective of user experience (UX). Ideally, here's how I want the "please disable AdBlock" experience to work on my site:
I don't want to ask the user to press a button or make the user think that they need to refresh the page. This is why I'm using setInterval
, I continuously make requests to the Google Ads JavaScript file that will be used to detect if AdBlock enabled.
I'm happy to pause my AdBlock for smaller sites that ask me to, or for newspaper sites that are supported by advertising, and I'm assuming that people visiting my site will also be OK with pausing AdBlock as a way of thanking me for the work that goes into what I share on my blog.
I'm mostly curious to see what happens to my site's traffic, and to see what impact it could have on the earnings I make from AdSense.
Some of my questions are:
I'll have a good follow-up article to share with numbers from my AdSense and Google analytics.
While ads are working on my site, I did notice a console error related to AdSense:
K {message: "adsbygoogle.push() error: Only one 'enable_page_level_ads' allowed per page.", name: 'TagError', pbr: true, stack: "TagError: adsbygoogle.push() error: Only one 'enab…agead/js/adsbygoogle.js?client=ca-google:77:1130)"}
message: "adsbygoogle.push() error: Only one 'enable_page_level_ads' allowed per page."
name: "TagError"
pbr: true
stack: "TagError: adsbygoogle.push() error: Only one 'enable_page_level_ads' allowed per page.
at go (http://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-google:219:326)
at fo (http://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-google:218:788)
at mo (http://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-google:225:365)
at c (http://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-google:226:38)
at no (http://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-google:226:156)
at yo (http://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-google:235:248)
at oo (http://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-google:232:89)
at http://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-google:227:47
at Od.aa.ma (http://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-google:64:802)
at Jf (http://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-google:77:1130)"
Here's a related issue on GitHub: https://github.com/nuxt-community/google-adsense-module/issues/141. I'm still not sure how to fix this issue. If anyone has any ideas, please let me know!
This article was first published here