Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore visits to Active Storage endpoints #465

Closed
joemasilotti opened this issue Nov 3, 2020 · 11 comments
Closed

Ignore visits to Active Storage endpoints #465

joemasilotti opened this issue Nov 3, 2020 · 11 comments

Comments

@joemasilotti
Copy link

Is it possible to ignore Ahoy visits to the Active Storage endpoints, e.g. /rails/active_storage/blobs/:id? There doesn't seem to be any hooks available from Active Storage.

@joemasilotti
Copy link
Author

P.S. This doesn't seem to work, a visit to /rails/active_storage/disk/eyJfcm...mugshot.jpeg is still being tracked.

# config/initializers/ahoy.rb:8

Ahoy.exclude_method = lambda do |controller, request|
  request.path.starts_with?("/rails")
end

@ankane
Copy link
Owner

ankane commented Nov 3, 2020

Hey @joemasilotti, I believe that should work. I'd try adding debugging to see what's going on.

@joemasilotti
Copy link
Author

FWIW, I was able to get this working by monkey patching ActiveStorage::BaseController. I strongly prefer to not do this, but figured I would share if anyone is desperate!

# app/controllers/active_storage/base_controller.rb

# frozen_string_literal: true

# The base class for all Active Storage controllers.
class ActiveStorage::BaseController < ActionController::Base
  include ActiveStorage::SetCurrent

  skip_before_action :track_ahoy_visit

  protect_from_forgery with: :exception

  self.etag_with_template_digest = false

  private
    def stream(blob)
      blob.download do |chunk|
        response.stream.write chunk
      end
    ensure
      response.stream.close
    end
end
# spec/requests/ahoy_spec.rb

require "rails_helper"

describe "Ahoy tracking" do
  it "tracks an Ahoy visit for the homepage" do
    expect { get "/" }.to change { Ahoy::Visit.count }.by(1)
  end

  it "doesn't track an Ahoy visit for Active Storage routes" do
    path = "/rails/active_storage/disk/efX0=--bb4/mugshot.jpeg"
    expect { get path }.to_not change { Ahoy::Visit.count }
  end
end
# spec/rails_helper.rb

Ahoy.track_bots = true

@ankane
Copy link
Owner

ankane commented Nov 5, 2020

Thanks for sharing. I'd encourage people to try the exclude_method route first to see if it works for them.

@ankane ankane closed this as completed Nov 5, 2020
@joemasilotti
Copy link
Author

But exclude_method doesn’t work. Were you able to narrow in on anything via your debugging? I’d love to use that instead of the risky monkey patching.

If it helps I could throw together two test cases tomorrow.

@mjbellantoni
Copy link

Howdy, Folks. I can confirm that exclude_method doesn't work here. Here's what's going on:

    def track_ahoy_visit
      defer = Ahoy.server_side_visits != true

      if defer && !Ahoy.cookies
        # avoid calling new_visit?, which triggers a database call
      elsif ahoy.new_visit?
        ahoy.track_visit(defer: defer)
      end
    end

The call to exclude? happens inside of track_visit. By the time we get there, new_visit? has already hit the database. It seems like the solution is to add a call to exclude? somewhere in this method?

@lukel97
Copy link

lukel97 commented Sep 13, 2021

Could this issue be reopened? I can confirm that Ahoy.exclude_method is still hitting the DB

@brunoprietog
Copy link

Hey @ankane, any news on this? Thanks!

@schpet
Copy link

schpet commented Dec 18, 2023

anyone come across a way of getting this working without monkey patching? with the following configuration:

# config/initializers/ahoy.rb

class Ahoy::Store < Ahoy::DatabaseStore
end

Ahoy.api = false
Ahoy.geocode = false

Ahoy.exclude_method = lambda do |controller, request|
  request.path.starts_with?("/rails")
end

cookies are still being set by ahoy:

❯ curl -s -I "http://localhost:3000/rails/active_storage/representations/proxy/eyJ….webp" | grep -i set-cookie
set-cookie: ahoy_visitor=731bf61c-7047-4c3b-84ad-e9b46471ca65; path=/; expires=Thu, 18 Dec 2025 20:52:20 GMT; SameSite=Lax
set-cookie: ahoy_visit=2f72ab22-365c-4b5b-9193-dbc6a156576e; path=/; expires=Tue, 19 Dec 2023 00:52:20 GMT; SameSite=Lax
set-cookie: ahoy_visitor=731bf61c-7047-4c3b-84ad-e9b46471ca65; path=/; expires=Thu, 18 Dec 2025 20:52:20 GMT; SameSite=Lax
set-cookie: ahoy_visit=2f72ab22-365c-4b5b-9193-dbc6a156576e; path=/; expires=Tue, 19 Dec 2023 00:52:20 GMT; SameSite=Lax

this prevents me from being able to cache images on a CDN using active storage's proxy mode.

@schpet
Copy link

schpet commented Dec 20, 2023

for anyone else running into this, a workaround that worked for me is rack-strip-cookies with the following config:

# config/application.rb
config.middleware.insert_before(ActionDispatch::Cookies, Rack::StripCookies, paths: %w[/rails/active_storage/representations/proxy])

@silva96
Copy link

silva96 commented Apr 5, 2024

@schpet Brilliant, I would add /rails/active_storage/blobs/proxy to the array too, your solution only helps with variant urls, not with original blobs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants