Gamco - Google Analytics Helper
This gem aims to provide a simple and easy way to send data to Google Analytics. It provides some view helpers that can be used in any rails application.
Installation
Add Gamco to your application's Gemfile:
gem "gamco"
And then install gamco:
rails g gamco:install
Configuration
Provide your TAG_ID in config/initializers/gamco.rb:
Gamco.setup do |config|
# ===> Required GA4 configuration options
config.tag_id = "G-XXXXXXXXX"
...
end
Usage
Usually you'll want to add your Google Analytics script immediately after
your <head> tag in your html. In rails, this is usually done in your
layout file (app/views/layouts/application.html.erb).
<html>
<head>
<%= ga_javascript_tags %>
...
</head>
This will send the default page_view event every time the user navigates
to a different path in your application. You can pass other dimensions if
you want to collect extra data. Example:
<html>
<head>
<%= ga_javascript_tags(environment: Rails.env) %>
...
</head>
Custom events
Also, you can send custom messages to Google Analytics per page or when
needed by using the ga_tag helper as the following example:
# app/views/products/show.html.erb
<%= ga_tag "event", "product_view", product_id: @product.id %>
...
Secure data
There are sensitive information that you might not want to send it directly
to Google Analytics to prevent some vulnerabilities. You can achieve this
by using the ga_secure helper which generates a digest for the passed value:
# app/views/products/show.html.erb
<%= ga_tag "event", "product_view",
user: ga_secure(current_user.id),
product_id: @product.id %>
...
By default the generated value is a MD5 digest. You can configure which
algorithm will be used in the initializer config/initializers/gamco.rb:
Gamco.setup do |config|
...
# ===> Method to secure sensitive data.
# config.secure = -> (value) {
# Digest::MD5.hexdigest(value)
# }
end
Usage per environment
It is possible that you want to send data to Google Analytics just in
production environments. You can control that using the active configuration
option in the initializer config/initializers/gamco.rb:
Gamco.setup do |config|
# ===> Required GA4 configuration options
config.tag_id = "G-XXXXXXXXX"
config.active = Rails.application.credentials.dig(:gamco, :active)
...
end
Development
After checking out the repo, run bin/setup to install dependencies. Then,
run rake spec to run the tests. You can also run bin/console for an
interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install.
To release a new version, update the version number in version.rb, and
then run bundle exec rake release, which will create a git tag for the
version, push git commits and the created tag, and push the .gem file
to rubygems.org.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/amco/gamco. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.
License
The gem is available as open source under the terms of the MIT License.
Code of Conduct
Everyone interacting in the Gamco project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.