Adding Google Analytics

The blog is up, and definitely, I would like to know whether I have fans. Yes, I would like to know if people are visiting my main site and also this blog, how much time they spend on my site or going through my blog posts. Which country are they from. What device are they using to access the site. To achieve this, I need some sort of tracking codes. You notice I was using present tenses? Well, that is what I was thinking – the blog is still new, this is, in fact, the second post, and I want to set up Google Analytics. Adding Google Analytics codes is the way you can track visitors to your site. In this post, am going to write what I did, and include screenshots where necessary, so you can as well follow, and do it on your website or blog.

Adding Google Analytics to any site is pretty straightforward if you have just one domain to deal with. For instance, say you only have your-blog.com or your-site.com, it would be easier integrating the google analytics script. For my case, I had the site domain and the blog subdomain to integrate with google analytics and still get the analytic data as if it were one site. First I did a lot of reading about cross domain trackingdomains and directories and tracking multiple domains. For your information, I do read a lot of tech content – it takes a lot of reading to stay afloat in the tech world.

sample data

Customizing Google Analytics to track site visits across a domain and it’s subdomains

To start off, a default Google Analytics setup is designed to track visitor data on one site, e.g. https://jamesshisiah.com. What if you have  https://jamesshisiah.com and https://blog.jamesshisiah.com? You will need to customize the analytics code in order to correctly track both of the sites visits and get the data in one report. I needed this because I have backlinks between the blog and the site, hence a site visitor is likely to visit the blog and vice versa. Although it’s easier having different tracking codes on each of the URLs, but then, this will mean several sessions when a visitor moves from one domain to several subdomains across your websites. You will end up losing some data when visitors switch between subdomains. Also, imagine having to switch between google analytics account in your analytics dashboard – mark you, one account could have up to 25 views!

There are two implementations of google analytics syntax; the traditional syntax which has been in use for some time, and the Asynchronous syntax which is the most recent. I like to move with the trend so you can guess I went with the recently introduced syntax. If Google comes up with another analytics setup, I will switch immediately, and write about it -:)

Linking a site to Google Analytics

The first thing I did is go to Analytics site, signed up, and added my website address. A google tracking code is generated after this, which I copied to use in subsequent steps. The code is in the format UA-XXXXXXXX-X. 

Next is to copy the script and paste it in your <head> </head> tags, just before the CSS is loaded. An efficient way is to paste it into a JS script that you can include in all your website pages. It will be easier tracking code in case you edit your account from the Analytics website. Up to this point, am done with the site, so  I have to setup the subdomain that the blog is running on. Since the blog is powered by WordPress, I had to customize this

Adding Google Analytics to the blog(subdomain)

Since the blog is powered by WordPress, I had to customize this free plugin and do some code editing.  For this to go through, you will need access to the installation directory of your WordPress, and some basic PHP knowledge. I am my own webmaster, so I had full access to my website and blog directory – plus advanced PHP knowledge. Following the steps of setting up a simple google analytics plugin  I did the following;

  1. Checked my active theme’s header.php file to ensure it included the wp_headhook.
  2. Created a new file in my plugins folder.
  3. Got my analytics tracking code from the Google analytics site (the one we had copied in earlier steps).
  4. Added some code including the tracking code to the plugin.
  5. Activated the plugin!

For brevity, I have excluded all the details, please read the whole process on this blog post.  Remember I used the same tracking code for both the website and the blog. The next step is to add the root domain to Exclusions List. Got to Analytics Website;  Admin> Tracking Info -> Referral Exclusion List. Only the root domain should be added, as shown in the screenshot below. This ensures that a visit by one user between the root domain and subdomains don’t register as new sessions, but as a continuation of the already started session.

Google Analytics Exclusion List image

Final Steps

That looks good. Now I had to confirm that am using the latest version of Google Analytics—called Universal Analytics. It makes tracking subdomains easier, and if you are setting up Analytics on your website, I recommend you use it. How to confirm whether you are using the Universal Analytics code, input your domain into this Google Analytics Checker. Tick on the include subdomains button as in the image:

Universal Analytics checker

Here is what I got, notice the ticks on the Universal Analytics column:

expected results

That is it, we have been adding Google Analytics to track visits on our domains and subdomains and we have succeded – at least up to this point. For now, there is no data, we just finished adding google analytics, so no screenshot for that. But in a few weeks, I will probably have an article on the data(I am more of a developer and less of a data analyst so it won’t be detailed), and screenshots showing visits. If you have read through, you can leave a comment below and be sure to come back and check out more blog posts in the future.