Note: This post is over two years old and so the information contained here might be out of date. If you do spot something please leave a comment and we will endeavour to correct.
3rd August 2009 - 4 minutes read time
After running coding sites for a couple years there is one little problem that really annoys me, so when I set up #! code I resolved to fix it. The issue arises when you use some JavaScript based analytics software that allows multiple domains to be used, like Google Analytics. There is nothing wrong with analytics software that allows this, and it is potentially useful for tracking all manner of things. However, some web masters might not be that good at running sites and will lift code from your site (analytics and all) in order to implement a single widget on their site. What this means is that when a user lands on one of their pages it will register as a hit on your site.
This sort of problem might be quite hard to spot as all you will notice is that people start finding your sites for strange keywords, or pages that you know are not there seem to be appearing in the visited pages list. I once had a site that appeared to get over 1,000 visits per day for the keyword "corvette", which clearly wasn't the case!
So how do you stop this sort of thing? You can just detect the current URL that the user is looking at and see if it contains your domain name. If it does then you can run the code needed to register a hit with the analytics software.
The code below is an example of this in action, which I use on #! code. This also pops up an alert if the URL does not contain your chosen URL. The idea behind this code is not to cripple the site but to force the web master to look at what he is doing a little more carefully and maybe learn one or two things about JavaScript along the way.
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
if ( document.location.href.match(/http:\/\/www\.YOUDOMAIN\.com/) != 'http://www.hashbangcode.com') {
alert('I got this code from www.YOUDOMAIN.com!');
} else {
var pageTracker = _gat._getTracker("UA-YOUR-ID");
pageTracker._trackPageview();
}
} catch(err) {}
</script>
I have always said that the best thing a bout the Internet is that anyone can do it, but the worst thing about the Internet is that anyone can do it. There are lots of coding blogs out there that use analytics so I present this code here to help out anyone with the same issue.
A pie chart is a great way of showing the relationship between numbers, especially when showing percentages. Drawing a pie chart from scratch takes a fair amount of maths to get working and so people usually go for third party charting libraries in order to add charts to the page.
Nightwatch.js is an end to end testing framework, written in JavaScript. It can be used to test websites and applications and uses the W3C WebDriver API to drive modern browsers to perform the tests.
In this article I will look at setting up Nightwatch.js in a project and getting started with writing tests.
Marp or Markdown Presentation Ecosystem is a collection of tools that allows the generation of presentations of different formats from a markdown template. The tools are written in JavaScript and have a number of options that allow presentations of different styles and formats.
Add new comment