Registering for a Google Analytics account is free and easy (as with most Google services) and within a few minutes I had the tracking information to register with Shapeways. Having got this far I decided to also add the tracking information to the separate shop front I'd built, and of course this is where life got interesting.
The first problem I faced was that I couldn't get Google to recognise that I'd added the tracking code to my website. No matter what I tried it simply kept telling me that I hadn't yet added the relevant JavaScript to my pages, yet I knew it was there. After a lot of head scratching and web searching I eventually found the problem and an easy solution. The domain I purchased doesn't actually include the initial www of most web addresses, but the way I have the DNS records and forwarding configured if you type the bare domain name you will be forwarded to the www version automatically. So when completing the Google Analytics form I included the www, which turns out was my mistake. I'm still not sure why this should make a difference (I'm guessing that Google aren't following the forwarding information when checking the page) but anyway removing the www from the default URL field fixed the problem.
The second problem was that I then wanted to turn the tracking off. Not so that I could comply with the EU directive on cookies or anything (I'm taking the applied consent rule, on the basis that if you visit an online shop you have to assume, at the bare minimum, that your page views are being tracked), but so that I wouldn't end up tracking my own page views.
I tend to have two copies of every website I develop running at the same time; one is the publicly visible (hopefully stable) version, while the second is my internal development version. Once I'm happy with updates to the development version I then push them to the live version. So when it comes to tracking page views not only do I not want to track my page views on the live site, but I definitely don't want to track page views on the development version.
My solution to this is to use server side code to only embed the tracking related JavaScript into the page, if it should be used. For this I wrote a utility function to determine if I should be tracking the page view or not:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public static boolean trackPageViews(HttpServletRequest request) throws Exception { //get the address the request is coming from InetAddress client = InetAddress.getByName(request.getRemoteHost()); //the client is the same machine (an address such as 127.0.0.1) if (client.isLoopbackAddress()) return false ; //the client is within the same site (i.e. not using a world visible IP) if (client.isSiteLocalAddress()) return false ; //the client is at the same world visible IP address as the server if (client.toString().equals(getServerIP( false ))) return false ; //track all other page views return true ; } |
The problem is that you're machine itself probably doesn't have a world visible IP address; general a home network will be connected to a broadband router which will have a world visible address. So what we need to do is find the external IP address of the router. There are a number of websites available that will show you the world visible IP from which you are connecting and so we could use one of those to figure out the external address of the server, or we could write our own.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | package utils; import java.io.IOException; import java.net.InetAddress; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class IPAddressServlet extends HttpServlet { @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { InetAddress client = InetAddress.getByName(request.getRemoteHost()); response.getWriter().println(clientAddress.toString()); } } |
getServerIP
method from our trackPageViews
method.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | private static String myip = null ; private static String getServerIP( boolean refresh) throws IOException { if (myip == null || refresh) { URL url = new URL( "http" , HOST, "/myip" ); BufferedReader in = new BufferedReader( new InputStreamReader(url.openStream())); myip = in.readLine(); in.close(); } return myip; } |
Before I forget, thanks to GB for the photo I used to brighten up this post!