Tradam.fyi

Impressions: Ahoy is really annoying to set up

Theme: Guide

Created: January 12th, 2021 08:09

Eyes laid on this: 25


I initially wanted to use the Impressionist Gem because of how simple it was and I didn't really need too much complexity. Unfortunately I had reached a bug which I didn't know how to fix, and had been documented in their issue tracker. I don't blame the gem or the devs, it could be just something weird with my configuration. Since I needed impression tracking to work before I released my game I decided to just drop it and look for another solution.

My options were either Ahoy or my own personal solution. I was debating doing my own solution but it didn't seem like a great idea since I believe there are a lot of bots who visit my site for whatever reason("judging by what Cloudflare reports to me every month") so it wouldn't be accurate. So Ahoy it was.

And Hoo Boy is Ahoy something else

Following their basic setup(install gem, migrate, run the track code) it failed after that last step. Trying to track "language: "Ruby" made my database very unhappy. I also tried just basic tracking without specifying language and other various combinations but nothing worked. There was no real guidance I could find on how to fix it. Eventually figured out that I have to drop the entire database and let rails rebuild it from the ground up so that it would work. (Meaning on my real database I would have to backup everything, drop the database, and then repopulate it.

And once this was set up Hoo Boy did it not get easier. It was all really unclear. The ""documentation"" consists of a single page mega document, that doesn't really document how to implement basic features, but rather weirdly goes into how to implement Ahoy into various 3rd party gems. Which I don't think many people would chose to do when they struggle to initially even set it up. I found lots of questions online similar to my own either unanswered, incorrect answers, or incomplete answers that don't really explain their steps.

So I guess here I go documenting my adventures again in exploring how to use the thing.

So for ahoy there is 2 distinct tracking methods, which are independent or each other. I found this confusing because myself and others(randoms online) had the impression they were somehow linked.

There is Ahoy::Event and there is Ahoy::Visit

Ahoy::Visit is the generic site wide tracker.

You just call ahoy.trackvisit somewhere in your controller(usually just in your show method) and it will record that visitor that they came to your website. This tracking contains a bunch of useful info, notably the visittoken which will come into play later with per model tracking). As far as I can tell you cant really use this to do model specific tracking on its own. It is useful however as it records most of the type data you might want, like location, user agent, etc.

Ahoy::Event is the specific tracker.

You call this with a few parameters that you yourself set and then can later retrieve them to do whatever. The 2 most important parameters that you can set are :name and :properties. You use name to specify what you are tracking, or what model the tracking belongs to. I used the format "articles:#{@article.id}" in my controller to track the different views between articles. For the properties part you are supposed to be able to store a hash that contains whatever info you want in it. However my database got mad at me when I tried, and with the lacking documentation I was unfortunately unable to figure it out. However for my basic needs just storing a single value in this parameter actually worked out for me.

This is the part where Ahoy::Visit's usefulness comes into play.

If you store the visit_token created by Ahoy::Visit, and do a check to make sure within the past X amount of hours that token hadn't been stored already, you can create the per model tracking that you want. If you want to see some of the code in action, I added it into my base ApplicationController in my website so that I can easy just call the functionality I need from all my controllers. In the future the link may be broken or possible not lead to what you want so if that is the case and you need help just shoot me an email, my email should be in the footer.

Once I had all of this set up, the rest of it was mostly a breeze. I figured out that .where is actually a built in method of Rails/ActiveRecord that I can use on Ahoy::Event and Ahoy::Visit, which hey I suppose as a rails dev it should of been obvious but eh. I'm still learning and little tips like that would of been very useful in the docs. Hope this guide is of someone. I would of loved a guide like this myself, over a day ago when I started 🙂


Back to Index