Wednesday, September 28, 2022
HomeRuby On RailsMonitoring Occasions with Ahoy | Drifting Ruby

Monitoring Occasions with Ahoy | Drifting Ruby


# Terminal
bundle add ahoy_matey
bundle add faker
rails g ahoy:set up
rails g scaffold article title content material
# db/seeds.rb
25.occasions do
  Article.create(
    title: Faker::Lorem.sentence,
    content material: Faker::Lorem.sentences.be part of(' ')
  )
finish
# fashions/article.rb
class Article < ApplicationRecord
  visitable :ahoy_visit
finish
# db/migrate/20210704023417_create_articles.rb
class CreateArticles < ActiveRecord::Migration[6.1]
  def change
    create_table :articles do |t|
      t.string :title
      t.string :content material
      t.bigint :ahoy_visit_id
      t.timestamps
    finish
  finish
finish

# rails g migration add_ahoy_to_articles ahoy_visit_id:bigint
# articles_controller.rb
class ArticlesController < ApplicationController
  embody Trackable

  ...

  def present
    observe "Seen Article", title: @article.title
  finish
finish
app/controllers/issues/trackable.rb
module Trackable
  prolong ActiveSupport::Concern

  def observe(title, properties = {})
    ahoy.observe title, properties
  rescue StandardError => error
    # notify error tracker
  finish
finish
# welcome_controller.rb
class WelcomeController < ApplicationController
  def index
    @visits = Ahoy::Go to.contains(:occasions).all
  finish
finish
# views/welcome/index.html.erb
<desk class="desk">
  <tbody>
    <% @visits.every do |go to| %>
      <tr>
        <td><sturdy><%= go to.ip %></sturdy></td>
        <td><sturdy><%= go to.user_agent %></sturdy></td>
      </tr>
      <% go to.occasions.every do |occasion| %>
        <tr>
          <td><%= occasion.title %></td>
          <td><%= occasion.properties %></td>
        </tr>
      <% finish %>
    <% finish %>
  </tbody>
</desk>
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments