ruby-on-rails Ahoy访问未使用after_validation:geocode创建未注解

ttcibm8c  于 2023-10-21  发布在  Ruby
关注(0)|答案(1)|浏览(102)

我已经将Ahoy集成到我们的平台中,它非常适合跟踪事件等。我现在尝试通过启用地理编码来扩展功能。我遇到的问题是,一旦我取消注解,

after_validation :geocode

行时,访问停止记录在数据库中。
我做错了什么?

visit.rb

class Ahoy::Visit < ActiveRecord::Base
  self.table_name = "ahoy_visits"
  # ahoy_visit

  geocoded_by :ip
  # after_validation :geocode
  has_many :events, class_name: "Ahoy::Event"
  belongs_to :user
end

ahoy.rb(注意检查访视持续时间较短):

class Ahoy::Store < Ahoy::DatabaseStore
end

# set to true for JavaScript tracking
Ahoy.api = true

# better user agent parsing
Ahoy.user_agent_parser = :device_detector

Ahoy.geocode = true
# Ahoy.server_side_visits = :when_needed

Ahoy.visit_duration = 10.seconds
# Ahoy.visit_duration = Rails.application.secrets.visit_duration

Ahoy.quiet = false

geocoder.rb

Geocoder.configure(
    # Geocoding options
    timeout: 15, # geocoding service timeout (secs)
    lookup: :google, # name of geocoding service (symbol)
    :ip_lookup => :maxmind, # IP address geocoding service (see below for supported options):
    language: :en, # ISO-639 language code
    use_https: true, # use HTTPS for lookup requests? (if supported)
    # http_proxy: nil,            # HTTP proxy server (user:pass@host:port)
    # https_proxy: nil,           # HTTPS proxy server (user:pass@host:port)
    api_key: Rails.application.secrets.google_server_api_key, # API key for geocoding service
    # cache: nil,                 # cache object (must respond to #[], #[]=, and #keys)
    # cache_prefix: 'geocoder:',  # prefix (string) to use for all cache keys

    # Exceptions that should not be rescued by default
    # (if you want to implement custom error handling);
    # supports SocketError and Timeout::Error
    # always_raise: [],

    # Calculation options
    units: :km, # :km for kilometers or :mi for miles
    # distances: :linear          # :spherical or :linear
  )

为了重新验证,当我注解出地理编码线访问正确记录和ahoy工程预期。谢谢.

EDIT我确定有一个异常被抛出,但它不是冒泡的日志,我有调试日志启用。Ahoy仅声明“[ahoy] Event excluded since visit not created:“,但在UI上,我们可以通过ahoy的这些日志语句看到正在启动访视:

访视开始
ahoy.self-01ef77de70801670c1f7f2f12fea979d59ba70488a77db75270b57ec5b1a2f8e.js?body=1:175 {visit_token:“4c7e0bcb-7afb-48cd-91ed-1bdf0d614767”,visitor_token:“374 bf 981-d843- 45 c3 - 953 d-0 f0 b8 fd 5a 6a 7”,平台:“Web”,landing_page:“http://localhost:3000/“,screen_width:1680,.}
ahoy.self-01ef77de70801670c1f7f2f12fea979d59ba70488a77db75270b57ec5b1a2f8e.js?body=1:175 {name:“$click”,属性:{...},时间:1533656941.084,id:“c967b67f-96f7-4f18-a9a8-df3735fdc1c2”,js:真的}

tvmytwxo

tvmytwxo1#

我遇到了同样的问题,这里是要遵循的步骤。

1.删除ahoy.rb中多余的行

这里没有必要包括geocoded_by :ipafter_validation :geocode。相反,只需在config/initializers/ahoy.rb中设置以下内容:

Ahoy.geocode = true

2.开发/测试环境中的地理编码

在开发和测试环境中,本地计算机的IP看起来像127.0.0.1::1,它们不是地理位置跟踪的有效IP地址。尝试提供自定义IP地址以用于调试目的。在config/environments/development.rb的末尾添加以下代码:

class ActionController::Request
  def remote_ip
    # Write any IP address you'd like here.
    "71.212.123.5" # city: Seattle, region: Washington, country: US
  end
end

3.检查Sidekiq服务器是否运行

地理编码作为后台作业执行,以避免减慢Web请求。Ahoy的默认作业队列是:ahoy。您可以在config/initializers/ahoy.rb中设置后台作业队列,如下所示:

Ahoy.job_queue = :default # Ahoy suggests `:low_priority`, default is for quick debugging.

并在终端中运行:

$ sidekiq

4.验证地理编码服务配置

检查Google API密钥和查找方法是否已正确配置,以及是否具有地理编码所需的权限和订阅。谷歌的地理编码服务提供了详细的响应,包括错误消息。您可以捕获并记录响应,以检查可能出现的问题。
This answer可能会有更多的细节。

相关问题