DIY analytics with Digital Ocean and Countly

I transitioned away from using Google Analytics on the sites I run long ago.  It was too complex and heavy, plus I like to de-Google when I can. For a short while I used Heap Analytics. Then, I stopped using advanced tracking altogether. 
 
Old school web server logs provide enough information for many needs. Too bad I seldom run my own web server anymore.  Everything is in either AWS or Netlify, and neither provides great logs, if any.
 
It's 2019, someone must have a decent solution!

What's out there

 
I had a strong preference for something I could self-host. I run PiHole at home which blocks most analytics. That complicates management and there are others like me that block analytics too. A self-hosted solution provides the best data.
 
I installed Fathom Analytics in docker and experimented a bit. It works, but my impression was it was too simple both in functionality and looks. It could use a better UX and it lacks things like screen size reporting and GDPR compliance.
 
Matamo , previously called piwik, is popular and would be a decent choice. It has a lot of features and been around awhile. I didn't try it because I found Countly to be adequate.
 
I like the simplicity of Simple Analytics, but it isn't open-source. It costs $9/mo and would likely serve my needs. It's what I'd try if I wasn't hosting my own.
 
I decided to use Countly. It's open-source with a hosted enterprise version. It's full featured and has a modern UI. It can do some tracking even with Javascript disabled on the client. It has GDPR opt-in and opt-out tracking. The list goes on.

How to host it

 
Once you know what you want, the next step is how to host it. I'd love to put in on a docker in my NAS, but it has to be open to the Internet. The data on my NAS is too important to open any part of it.
 
That leaves cloud providers. I use AWS a lot, but EC2 felt too heavy for this and I wanted to try something new. I'd heard good things about Digital Ocean and I wanted to know if I could get everything running for $5/mo.
 
I tried two different ways: docker and standalone.

Docker

 
The docker method is quick and painless:
  1. Create your $5 droplet on Digital Ocean using the one-click docker app.
  2. Use the IP for your droplet to create a DNS A record for whatever name you plan on using for your instance. Complete this before you get the lets encrypt certificate.
  3. SSH into your droplet
  4. mkdir -p /etc/nginx/certs /etc/nginx/vhost.d /usr/local/share/nginx/html /var/data/mongodb
  5. docker run --detach --name nginx-proxy --publish 80:80 --publish 443:443 --volume /etc/nginx/certs --volume /etc/nginx/vhost.d --volume /usr/share/nginx/html --volume /var/run/docker.sock:/tmp/docker.sock:ro jwilder/nginx-proxy
  6. docker run --detach --name nginx-proxy-letsencrypt --volumes-from nginx-proxy --volume /var/run/docker.sock:/var/run/docker.sock:ro jrcs/letsencrypt-nginx-proxy-companion
  7. docker run --detach --name countly --volume /var/data/mongodb --env "VIRTUAL_HOST=host.domain.com" --env "LETSENCRYPT_HOST=host.domain.com" --env "LETSENCRYPT_EMAIL=youremail@domain.com" countly/countly-server
  8. Navigate to https://host.domain.com to setup Countly.

Advantages

  • Arguably more secure, since each piece is it's own instance
  • The docker image worked better for me. For example, the "compare applications" page worked in the docker instance, but fails in the standalone instance.
  • You could add more docker containers, optimizing the use of this one droplet.

Disadvantages

  • Each one of the docker images is running its own nginx
  • More complex and harder to debug
  • Though I didn't notice a difference in memory or CPU usage, this has to more heavy weight than the standalone.

Standalone

 
This is the method I decided to use. It feels more involved up front because of the packages and file editing.
  1. Create your $5 droplet on Digital Ocean using the one-click countly app.
  2. Use the IP for your droplet to create a DNS A record for whatever name you plan on using for your instance. Complete this step must before you get the lets encrypt certificate.
  3. SSH into your droplet
  4. sudo apt-get update
  5. sudo apt-get install software-properties-common
  6. sudo add-apt-repository universe
  7. sudo add-apt-repository ppa:certbot/certbot
  8. sudo apt-get update
  9. sudo apt-get install certbot python-certbot-nginx 
  10. Edit /etc/nginx/sites-enabled/default change the server_name to be your DNS name.
  11. certbot --nginx
  12. crontab -e to edit crontab and add to the end: 
    0 0 */80 * * /usr/bin/certbot renew
  13. Navigate to https://host.domain.com to setup Countly.

Advantages

  • Easier to debug
  • Easier to maintain
  • Light weight

Disadvantages

  • Everything is running in one instance
  • The droplet is dedicated to Countly, you can't host more applications with it.

Considerations

  • Generate your tracking code at https://code.count.ly/integration-web.html, the link in countly doesn't work for me.
  • Droplets are transient, decide how you plan to preserve your data. Countly has a commandline available, complete with backup and restore commands. I used these to move data from my Docker container with transfer.sh to my standalone and it worked great.
  • You are responsible for upgrades. You need to stay on top of ubuntu, nginx, countly, and all their dependencies.
  • Digital Ocean allows you to define a firewall for your droplets.  Use it! Your droplet should only need ports 22, 80, and 443 open for Countly.
  • Consider using a Digital Ocean Floating IP.  They're free as long as you have a droplet attached. When you migrate to a new droplet, you won't need to touch DNS!
  • Consider using cloudflare for SSL.  I prefer less accounts to manage, but some people may prefer it over managing lets encrypt.

UPDATE 2/15/2019

With the release of Countly 19.02, it no longer fits on the $5 droplet.  Instead, the one-click installer only allows the $20/mo droplet at the minimum.

This article was updated on February 15, 2019