
Posted on • Edited on • Originally published atalsohelp.com
Analytics set-up for Rails
article originally published here :https://alsohelp.com/blog/rails-analytics-made-simple/
Analytics (i.e. stats about your visitors) is nowadays not cheap. Let's see why, and let's see how Rails could help.
0. Motivation
Google analytics is not anymore an option, at least in the EU. It's barely legal regarding GDPR, and we start to see companies being blamed for using it. Alternatives to Google Analytics are not free, and not cheap. Even open-source solutions are notthat easy to implement.
1. Advantages of a Rails-based analytics solution
The first advantage is that you don't need a consent banner. No cookies are sent to track the user, and the user can't say "no" to tracking. As long as it remains completely anonymous, this is not a problem. Application logs may also somehow "track" access to the app, but this is not usable for non-techies.
The second advantage is that it is completely free - apart from the hostingyou are already paying anyway.
The third advantage is that you will use your Rails skills - apart from installing a gem, no additional skills are required.
2. Active Analytics for Rails
Rails official plugins (known as "gems") often start with "Active-". You will encounter sometimes some gems outside the Rails world whose name also starts with "Active". That's the case today withActive Analytics, a gem fromBaseSecrete.
Their GitHub repo mention that you can know about :
- Sources: What are the pages and domains that bring some traffic.
- Page views: What are the pages that are the most viewed in your application ?
- Next/previous page: What are the entry and exit pages for a given page of your application.
3. Tutorial to add analytics to a fresh new Rails application
Prerequisites
First ensure you have all the classic already installed on your computer :
$> ruby-v ruby 3.1.0p0 // you need at least version 3 here$> bundle-v Bundler version 2.2.11$> npm-v 8.3.0 // you need at least version 7.1 here$> yarn-v 1.22.10$> psql--version psql(PostgreSQL) 13.1 //let's use a production-ready database locally
Minimal Rails application
mkdirrailsanalytics&&cdrailsanalyticsecho"source 'https://rubygems.org'"> Gemfileecho"gem 'rails', '~> 7.0.0'">> Gemfile bundleinstallbundleexecrails new.--force--minimal-d=postgresql bundle update
Minimal controllers and views
# Create a default controllerecho"class HomeController < ApplicationController"> app/controllers/home_controller.rbecho"end">> app/controllers/home_controller.rb# Create another controllerecho"class OtherController < ApplicationController"> app/controllers/other_controller.rbecho"end">> app/controllers/other_controller.rb# Create routesecho"Rails.application.routes.draw do"> config/routes.rbecho' get "home/index"'>> config/routes.rbecho' get "other/index"'>> config/routes.rbecho' root to: "home#index"'>> config/routes.rbecho'end'>> config/routes.rb# Create a default viewmkdirapp/views/homeecho'<h1>This is home</h1>'> app/views/home/index.html.erbecho'<div><%= link_to "go to analytics", "/analytics" %></div>'>> app/views/home/index.html.erbecho'<div><%= link_to "go to other page", other_index_path %></div>'>> app/views/home/index.html.erb# Create another viewmkdirapp/views/otherecho'<h1>This is another page</h1>'> app/views/other/index.html.erbecho'<div><%= link_to "go to home page", root_path %></div>'>> app/views/other/index.html.erb# Create database and schema.rb bin/rails db:create bin/rails db:migrate
Good ! Run
bin/rails s
And open your browser to see your app locally. You should see something like this :
Activate ActiveJob and ActionMailer
Open application.rb and uncomment line 6 and 10 as follow, this will be needed for active_analytics :
# inside config/application.rbrequire_relative"boot"require"rails"# Pick the frameworks you want:require"active_model/railtie"require"active_job/railtie"# <== !! Uncommentrequire"active_record/railtie"# require "active_storage/engine"require"action_controller/railtie"require"action_mailer/railtie"# <== !! Uncomment# ... everything else remains the same
Add active_analytics
Open your Gemfile and add
gem'active_analytics'
Then stop your local web server, and run
bundleinstall
Install active_analytics
Now run :
bin/railsactive_analytics:install:migrationsbin/railsdb:migrate
Side note : We often usebin/rails
instead of justrails
, to ensure that the version used is the one of our project, not the globally-installed version of Rails.
Now insideapp/controllers/application_controller.rb
, copy/paste the following code :
# inside app/controllers/application_controller.rbclassApplicationController<ActionController::Basebefore_action:record_page_viewdefrecord_page_viewActiveAnalytics.record_request(request)endend
Openconfig/routes.rb
# inside config/routes.rbRails.application.routes.drawdo# Add this linemountActiveAnalytics::Engine,at:"analytics"get"home/index"get"other/index"rootto:"home#index"end
Check that analytics for Rails is working, locally
Run
bin/rails s
And open your browser athttp://localhost:3000/analytics
You should see this :
Ok, no data (so far).
Now go to the homepage of your Rails app, and click on "go to other page". Then visithttp://localhost:3000/analytics once again.
Now some stats should appear.
Great ! You are able to see stats for a period range. Moreover, you can click on any url to see where the user was coming from.
4. View analytics in production
First steps, fast checking
Now it's time to push your code into production, let's say heroku.
heroku login heroku create bundle lock --add-platform x86_64-linux heroku addons:create heroku-postgresql:hobby-dev heroku buildpacks:set heroku/rubygit add . && git commit -m 'ready for prod' git push heroku main heroku run rails db:migrate
Heroku gives you a weird URL, like "https://sleepy-sands-87676.herokuapp.com/" (yours will be different). Go to this weird URL and make the same steps as before, on your local browser : go to the home page, then click on "go to the other page", then check analytics again. Everything works so far... but what if a bot visits the application ?
Is analytics for Rails able to tackle bots ?
Now go tohttps://www.webpagetest.org/, and enter the heroku URL of your webapp. Click on "Start test!" (or equivalent). Wait for the test to finish, then visit the /analytics URL of your Heroku website.
You should see that the number of visits has increased, as if "WebPageTest" has just sent a bot, not a real visitor.
Add this to your Gemfile :
gem'crawler_detect'
Then run
bundleinstall
Then change your main controller like this :
# inside app/controllers/application_controller.rbclassApplicationController<ActionController::Basebefore_action:record_page_viewdefrecord_page_view# This condition should skip bots.unlessrequest.is_crawler?ActiveAnalytics.record_request(request)endendend
Change yourconfig/application.rb
like this :
# ...moduleRailsanalyticsclassApplication<Rails::Application# Add this lineconfig.middleware.useRack::CrawlerDetect# ...
Then run in your console
git add . && git commit -m 'detect crawlers'git push heroku main
Now make the WebPageTest check again.
Analytics numbers didn't change ?
You won ! 🎉
Not covered in this tutorial
Of course the/analytics
URL is very likely to be a protected route - unless you build things in public. How to protect a route is not covered here, but maybe you want to giveRodauth a try.
Top comments(1)
For further actions, you may consider blocking this person and/orreporting abuse