How to Disable Disqus & Google Adsense when running Jekyll on localhost

When I migrated my WordPress website to Jekyll, one of the things that I forgot to do was disable Disqus comments and Google Adsense on my localhost when running Jekyll locally. It was one of those things that I used to do from PHP when I was still on WordPress.

disqus-localhost
Replacing the disqus_shortname variable with a test account allows you to still preview disqus comments even when on localhost without generating “http://localhost:4000/” errors.

This had me end up with a lot of http://localhost:4000/… URLs on my Disqus account and something that I’d periodically clean whenever I find myself being OCD. I would find it hard to resist not deleting these mupltiple invalid posts on my disqus and I’d end up checking on it more and wasting more time. This was not a critical problem but it was something that wouldn’t go away and was always bothering me.

Though the Disqus issue is more of a nuisance than a critical one, the same cannot be said for Adsense.

With ad impressions running from localhost, you run the risk of getting your Adsense account banned since it may appear to Google as simulated impressions which is a violation of their terms of use. Now, we’ve all must’ve already heard nightmare stories of publishers getting banned from Adsense for various valid or questionable reasons and it’s just one of those things that you just would not put to chance.

This prompted me to scout around for an elegant solution and that is how I came across this post from Michael Parker on how he disabled Disqus when running Jekyll on localhost using a second configuration file.

Apparently, Jekyll supports multiple config files running, something I just recently learned while browsing through this post and reading up on Jekyll documentation. This same principle applies for when you want to disable Google Adsense.

Following the example from the blog post above, I added the following to my _config.yml:

enable_disqus: true and enable_adsense: true

I then created a _config_dev.yml where I added the two lines but this time I set the value as false:

enable_disqus: false and enable_adsense: false

On the include file, comments.html where I place the universal disqus code I then added the following conditional statement to the disqus_shortname variable:

This allows me to still load Disqus on localhost but using a ‘testing’ account instead of my real account.

Now for Adsense, I applied the same but replaced the Adsense code with a placeholder banner ad when running on localhost.

I can now preview my site on localhost without violating Google Adsense terms but at the same time be able to see how it would appear when in production.

This is the command that I run when running Jekyll on my local machine.

jekyll serve --baseurl --drafts --config=_config.yml,_config_dev.yml

The second config file overrides the first one which results in Disqus and Adsense when running on localhost.

Hopefully, Jekyll would have a more official solution to this in the future.

Leave a Reply

Advertisement