哎呀,访问不是用 after_validation 创建的:地理代码未注释

问题描述 投票:0回答:1

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

after_validation :geocode

线,访问停止记录在数据库中。

我在这里做错了什么?

访问.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

地理编码器.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 只是声明“[ahoy] 由于访问未创建而被排除:”,但在 UI 上我们可以看到访问正在通过 ahoy 的这些日志语句开始:

参观开始

ahoy.self-01ef77de70801670c1f7f2f12fea979d59ba70488a77db75270b57ec5b1a2f8e.js?body=1:175 {visit_token:“4c7e0bcb-7afb-48cd-91ed-1bdf0d614767”,visitor_token: “374bf981-d843-45c3-953d-0f0b8fd5a6a7”,平台:“Web”,登陆页面: “http://localhost:3000/”,屏幕宽度:1680,…}

ahoy.self-01ef77de70801670c1f7f2f12fea979d59ba70488a77db75270b57ec5b1a2f8e.js?body=1:175 {名称:“$click”,属性:{…},时间:1533656941.084,id: “c967b67f-96f7-4f18-a9a8-df3735fdc1c2”,js:true}

ruby-on-rails ruby-on-rails-4 ahoy
1个回答
0
投票

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

1.删除
ahoy.rb

中多余的行

无需在

geocoded_by :ip
配置中包含
after_validation :geocode
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 服务器是否正在运行

地理编码作为后台作业执行,以避免减慢网络请求。 Ahoy 的默认作业队列是

:ahoy
。您可以在 config/initializers/ahoy.rb 中设置后台作业队列,如下所示:

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

并在终端中运行:

$ sidekiq

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

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

这个答案可能对了解更多细节有用。

© www.soinside.com 2019 - 2024. All rights reserved.