Adding Google Analytics to your DO-Hosted Ghost Blog

It seems like quite a few folks I know are blogging with Ghost and Digital Ocean. I'm doing it too, and I'd advise following our lead. The process is simple which lets you focus on blogging.

If you're going to the trouble of blogging then you're probably interested in tracking your readers. Google Analytics is a pretty easy way to do that so I'll show you how to get it set up. I'm assuming you already have your blog set up. If you don't then go to www.digitalocean.com/ and set up a droplet with Ubuntu and Ghost preinstalled.

Step 1

Get an account. Go to google.com/analytics and set yourself up with an account. Go to admin > tracking info > tracking code. That code will send your tracking information to Google.

Step 2

ssh into your droplet by typing

$ ssh root@162.243.131.156

Change my blog's IP address with your own and use another username if you have one.

Step 3

Next, you'll need to edit one of the ghost files using the text editor of your choice. I recommend vim, and that's what I'll use in this example. You could also use nano. At any rate, open your ghost default.hbs. The directory for this file will depend on your theme. The default is casper, so that's the path I've given.

$ vim /var/www/ghost/content/themes/casper/default.hbs

Now you can paste the tracking code from Step 1 into default.hbs I chose to do so right above the </head> tag. This will render the script on every page.

...
   {{ghost_head}}
  <script>
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
    m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

    ga('create', 'YOUR TRACKING ID', 'auto');
    ga('send', 'pageview');
  </script>
</head>  

Save and close the file (:wq in vim).

Step 4

You'll need to restart Ghost for the change to take effect.

$ service ghost restart
Step 5

You should now have your Google Analytics set up, but there's another pro-move you can make. Right now, you do all your blog editing by visiting yourblog.com/ghost. Unless you're wildly popular, all your editing will skew your analytics. We can fix this problem by setting up a filter in Google Analytics to exclude traffic to the admin side of your site.

  • Go back to your Google Analytics dashboard and click on Admin.
  • Click All Filters in the Account column.
  • Click + New Filter
  • Give your new filter a name (e.g. 'exclude blog editing')
  • Choose Filter Type: Predefined, Exclude, traffic to the subdirectories, that begin with
  • Fill in the Subdirectory field with /ghost
  • Add your site to the Selected Views
  • Click Save exclude filter

Now you're really done!

Also, I'm interested in checking out Piwik, a self-hosted open source analytics platform. That one may be phase two.