Showing off my top hits!
Want to show your most popular posts? Helloooo PostHog!
This is, as usual, not the sort of thing that's going to work well in email or the ActivityPub reader. Fortunately, you know what to do...
I've been wanting a good way to show top posts in Ghost, based on analytics. Yes, Ghost has native analytics, but this blog, personally, does not have native analytics. So, I thought I'd take PostHog out for a spin. Here's the result:
How'd I do that? (Short version. Longer write-up to come, and I'm gonna make it track likes, not just views. But not today.)
- I added PostHog analytics to my sitewide code injection. (~5 minutes, once I found the right page, plus 10 minutes to figure out how to get their site out of "OS mode". Probably a me issue.)
- Added an "insight" (lots of flailing, less than 5 minutes of work)
- Added an endpoint for the endpoint (again, flailing, very little work)
- Made an API key for that endpoint, locked down the scope, made it read only, etc. (<5 minutes, for real this time)
- Slammed out a little bit of JavaScript to hit the endpoint, then the Ghost Content API for the data not in the endpoint. (Should have been 5 minutes, was really 15 because Cursor is having an outage, and I'm out of non-auto API tokens because it's the end of the month and I'm cheap.)
- Fussed with CSS styles to get cards to look like cards. (Always longer than it should be. I should have just edited the theme rather than overriding post styles when I wanted index page styling.)
And yes, aside from PostHog, it's all front-end, no extra infrastructure required. (And analytics are nice to have anyway, so...)
Other options I'm considering:
- Reduce loading time by shoving more post data into PostHog, so that I don't have to hit the Ghost Content API on page load.
- Set up a cloud function that gets the data from the PostHog endpoint and tags my top posts (and un-tags anything that's fallen off the leaderboard), so that no client-side loading script is required.
But first, I think I'll get it tracking ❤️s too... And I did!
Update
And of course, the moment I hit publish, my PostHog endpoint decided to start returning fields in a different order. Because ... reasons? (It's almost certainly a me error.)
I'm using the beta "Endpoints" functionality. I think I've about got the problem narrowed down to the flip-flop only happens if I make this a Materialized View (and then maybe sometimes), but I'm having enough of a noob moment that I don't know how to fix it, other than not making it a materialized view. (I think it should be a materialized view - I don't need a live count of all site data, and if I had a lot of site data, it could be slow.) Any PostHog experts out there? This Ghost Expert is a bit stumped at the moment.
Hey, before you go... If your finances allow you to keep this tea-drinking ghost and the freelancer behind her supplied with our hot beverage of choice, we'd both appreciate it!