Google Analytics on Writebook — The Dummy’s Guide

Daniel Dallos
TweakedTech
Published in
4 min readAug 24, 2024
Photo by Kenny Eliason on Unsplash

Writebook is a free publishing tool from 37signals part of their ONCE software product line.

Writebook is remarkably simple software that allows you to publish text and pictures in a simple, browsable online book format

Why Writebook?

I had an idea for a series of posts, but I wanted something special.

There are a lot of content publishing platforms available online, Substack, Ghost, beehiiv, 𝒪𝓃𝓁𝓎𝓕𝒶𝓃𝓈, Medium, and many more.

These platforms usually focus on blog or newsletter formats and attempt to monetize the content.

I wanted something special:

  • no focus on monetization (or at least not in this format)
  • preferably free
  • easy to use, minimal learning curve
  • I can learn something new while using it
  • book-ish format

Writebook was a good candidate:

  • free
  • self-hosted (so I can play with servers again)
  • book format
  • easy to use, not overcomplicated

I really admire the work that David Heinemeier Hansson and Jason Fried do at 37signals with Basecamp, HEY, and now with ONCE, so I had to give it a try.

Setup

The setup was a breeze, thanks to their helpful guides for platforms like DigitalOcean, Hetzner, and others.

The Good

On an isolated machine, Writebook works like a charm after a simple one-command installation. No extra steps are required.

The Bad

Writebook is tailored for easy online book publishing but lacks features like analytics, paywalls, or advanced user management.

I wanted to put the machine that hosts Writebook behind a CDN (e.g. Cloudflare) to provide some protection and basic analytics.
However, Writebook requires a DNS-only configuration (no proxy — probably to have SSL configured automatically) that will make no use of any of the above-mentioned features.

I really wanted analytics to track interest in my writing, so I had to look further in this direction. (I’ll figure out the CDN protection issue later.)

The Ugly

Writebook is open-source, so I hope by giving a little bit of massage to the code I could see some basic analytics.

Unfortunately, I’m neither a Ruby guy nor a Docker guy…

I just want to write… If it takes too much time to set it up, my motivation will drop, and no posts will be written.

So I needed to find a quick, minimal-effort solution…
And probably Google Analytics is a good fit for this (at least for now).

The low-effort code massage

Probably this is the worst way of doing it, but hey:

  • it is quick
  • it works
  • short learning curve (I guess I will need a longer one to make this right 😊)
  • using Google Analytics (GA) is “free”

The code change

After we have downloaded the source code, there is an application.html.erb that’s the entry point(?) for the HTML pages.

If you don’t have the source code, you can also pull this file from the running Docker image itself via

docker cp CONTAINER_ID:/rails/app/views/layouts/application.html.erb .

Once we have the file, open it in a text editor.

Right after the <head> tag we can paste the tracking code from Google Analytics:

<head>

<!-- Google tag (gtag.js) BEGIN -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());

gtag('config', 'G-XXXXXXXXXX');
</script>
<!-- Google tag (gtag.js) END -->

<title><%= content_for(:title) || "Writebook" %></title>
...
</head>

Replace `G-XXXXXXXXXX` with your Google Analytics tag

Save it and we are done with writing the code.

Now we can move the code back to the Docker Container and restart it.

docker cp application.html.erb CONTAINER_ID:/rails/app/views/layouts/application.html.erb

( You should not work like this if you want a production-grade system. 🫣
This is for experimentation purposes only.)

How to get the Container ID?

docker ps will show you all the running container. You need to find one with the name writebook

Restarting the container

docker restart e922e04bcf66

Now we should be up and running again and seeing the data coming in on Google Analytics.

Caveats

  • Copying a file in and out of a Docker container is not ideal. A re-deploy can completely revert all the changes.
  • Writebook gets updated in the background automatically, so the changes can be overridden.
  • The same work has to be done every time there is Writebook update
  • The page edits from the admin users are also captured by the analytics.

Improvement ideas

  • Make our own Docker image with the changes.
  • Disable auto-update.
  • Don’t report editing a post as an impression.
  • Built-in GA (or any analytics) integration into Writebook where we can specify the tracking ID during setup. (This would mean we don’t have to worry about breaking updates anymore)
  • Understand what I’m doing 😎 … learn more about Ruby and Docker.

First impression

Writebook does the job and delivers what it promises.

It lets me publish in a book format easily, and I can see analytics with some hacks.

I wish it had some extra open-source modules that we could easily integrate into the project and configure during setup, ideally without any manual intervention.

Stay tuned!

Follow us on LinkedIn to learn about what we are cooking now at Tweaked Tech.

Thank you for reading about our journey!

PS: We are not affiliated with Writebook, ONCE, or 37signals, we just love using their products!

--

--

Daniel Dallos
TweakedTech

Android and IoT enthusiast | iOS developer | Technology lover | https://danieldallos.com