My opinionated ruby on rails template
at main 101 lines 3.0 kB view raw
1# frozen_string_literal: true 2 3say 'Setting up Ahoy for analytics...', :green 4 5gem 'ahoy_matey' 6gem 'ahoy_email' 7 8after_bundle do 9 say ' Running Ahoy generators...', :cyan 10 rails_command 'generate ahoy:install' 11 rails_command 'generate ahoy:messages --encryption=lockbox' 12 rails_command 'generate ahoy:clicks' 13end 14 15say ' Creating Ahoy initializer...', :cyan 16file 'config/initializers/ahoy.rb', <<~RUBY, force: true 17 # frozen_string_literal: true 18 19 class Ahoy::Store < Ahoy::DatabaseStore 20 end 21 22 Ahoy.api = true 23 Ahoy.server_side_visits = :when_needed 24 25 # Mask IPs for privacy (GDPR compliance) 26 Ahoy.mask_ips = true 27 28 # Cookie settings 29 Ahoy.cookies = :none # or :all for full tracking 30 31 # Visit duration 32 Ahoy.visit_duration = 30.minutes 33 34 # Geocoding (requires geocoder gem) 35 # Ahoy.geocode = true 36RUBY 37 38say ' Creating Ahoy Email initializer...', :cyan 39file 'config/initializers/ahoy_email.rb', <<~RUBY 40 # frozen_string_literal: true 41 42 # Configure ahoy_email for email tracking 43 AhoyEmail.api = true 44 45 # Default tracking options 46 AhoyEmail.default_options[:message] = true # Store message metadata 47 AhoyEmail.default_options[:open] = true # Track email opens (via tracking pixel) 48 AhoyEmail.default_options[:click] = true # Track link clicks 49 AhoyEmail.default_options[:utm_params] = false # Don't add UTM parameters 50 51 # Register the message subscriber to store messages 52 AhoyEmail.subscribers << AhoyEmail::MessageSubscriber 53 54 # Configure message model 55 AhoyEmail.message_model = -> { Ahoy::Message } 56 57 # Track email opens and clicks 58 # Note: Opens require an image to be loaded, which may be blocked by email clients 59 # Clicks work by redirecting through the application before going to the final URL 60RUBY 61 62say ' Creating Trackable concern...', :cyan 63file 'app/models/concerns/trackable.rb', <<~RUBY 64 # frozen_string_literal: true 65 66 # Include in ApplicationController for visit/event tracking 67 # 68 # Usage in controllers: 69 # ahoy.track "Viewed Product", product_id: product.id 70 # 71 # Usage in views: 72 # <% ahoy.track "Viewed Page", page: request.path %> 73 # 74 module Trackable 75 extend ActiveSupport::Concern 76 77 included do 78 # Track visits automatically 79 # before_action :track_ahoy_visit 80 end 81 82 def track_event(name, properties = {}) 83 ahoy.track(name, properties) 84 end 85 end 86RUBY 87 88say ' Adding Ahoy to ApplicationController...', :cyan 89inject_into_class 'app/controllers/application_controller.rb', 'ApplicationController', <<~RUBY 90 include Trackable 91RUBY 92 93say ' Configuring ApplicationMailer for tracking...', :cyan 94inject_into_file 'app/mailers/application_mailer.rb', after: "class ApplicationMailer < ActionMailer::Base\n" do 95 " has_history\n utm_params\n" 96end 97 98say 'Ahoy analytics configured!', :green 99say ' Track events: ahoy.track "Event Name", key: value', :cyan 100say ' Email tracking enabled for opens and clicks', :cyan 101say ' Run migrations: rails db:migrate', :yellow