ruby 无法使用Ahoy gem Rails 5.1.3跟踪位置

z6psavjg  于 2023-10-17  发布在  Ruby
关注(0)|答案(1)|浏览(119)

我刚刚将ahoy gem安装到我的Rails 5.1.3应用程序中。我正在取回visitevent,但访问位置属性总是返回country: "Reserved"region: nilcity: nilpostal_code: nil。这是我按照Ahoy github上的说明实现的。任何人都可以阐明我错过了什么或做错了什么?这在生产环境和开发环境中都有发生。

application.js

$(document).ready(function(){
  ahoy.trackAll();
});

Gemfile

gem 'ahoy_matey'
# Did not include geocoder because it is detailed in the documents as a dependency.

访问示例:

[2] pry(main)> Visit.last
  Visit Load (0.5ms)  SELECT  "visits".* FROM "visits" ORDER BY "visits"."id" DESC LIMIT $1  [["LIMIT", 1]]
=> #<Visit:0x007fd045e04b48
 id: 2,
 visit_token: "7750594c-7163-46c1-8ec7-18f3069619d0",
 visitor_token: "efdcb332-742a-4de3-971b-c141f24ff0f6",
 ip: "74.174.236.84",
 user_agent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36",
 referrer: nil,
 landing_page: "http://my_website.com/users/sign_in",
 user_id: nil,
 referring_domain: nil,
 search_keyword: nil,
 browser: "Chrome",
 os: "Mac OS X",
 device_type: "Desktop",
 screen_height: 800,
 screen_width: 1280,
 country: "Reserved",
 region: nil,
 city: nil,
 postal_code: nil,
 latitude: 0.0,
 longitude: 0.0,
 utm_source: nil,
 utm_medium: nil,
 utm_term: nil,
 utm_content: nil,
 utm_campaign: nil,
 started_at: Wed, 22 Nov 2017 21:10:52 UTC +00:00>
qojgxg4l

qojgxg4l1#

问题不在于Ahoy gem本身,因为它只是跟踪用户设备,而不是位置服务。要获取位置属性,我们可以使用Geocoder沿着Ahoy。

Gemfile

Geocoder添加到Gemfile中,因为它在Ahoy文档中被列为依赖项:

gem 'ahoy_matey'
gem 'geocoder'

地理编码

可以通过以下方法之一来实现:

1.作为后台作业(默认和推荐)

地理编码在后台作业中执行,因此不会减慢Web请求。默认作业队列是:ahoy。在config/initializers/ahoy.rb中启用地理编码和后台作业队列:

Ahoy.geocode = true

Ahoy.job_queue = :default # Ahoy suggests `:low_priority`

启动后台作业处理库,如果您在终端中使用sidekiq类型:

$ sidekiq

2.无后台作业(不推荐)

不需要在config/initializers/ahoy.rb中启用地理编码和设置作业队列,只需将此代码添加到Ahoy::Visit模型中即可自行设置属性。

after_validation :update_geolocation_data

private

def update_geolocation_data
  location = Geocoder.search(ip).first

  return if location.blank?

  self.city = location.city
  self.region = location.region
  self.country = location.country
  self.latitude = location.latitude
  self.longitude = location.longitude
end

开发

IP地址在开发和生产环境中的行为不同。如果您在开发/测试环境中从Ahoy对象或简单地使用request.ip调试IP地址,它将看起来像127.0.0.1::1,这不是检测位置的有效IP。
将以下代码添加到config/environments/development.rb:

class ActionController::Request
  def remote_ip
    # Write any IP address you'd like here.
    "71.212.123.5"
  end
end

重新启动服务器,更新后的IP地址和位置数据将添加到Ahoy对象中:

> Ahoy::Visit.last
  Ahoy::Visit Load (4.2ms)  SELECT "ahoy_visits".* FROM "ahoy_visits" ORDER BY "ahoy_visits"."id" DESC LIMIT $1  [["LIMIT", 1]]
=> #<Ahoy::Visit:0x00007fc6a84fa698
 id: 18,
 visit_token: "28ff1d9d-f680-4bfc-8fa9-0235c73a24ff",
 visitor_token: "c7ad1fac-e05f-4008-b2a9-4ac60893d638",
 user_id: 3,
 ip: "71.212.123.5",
 user_agent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36",
 referrer: "http://localhost:3000/dashboard",
 referring_domain: "localhost",
 landing_page: "http://localhost:3000/dashboard",
 browser: "Chrome",
 os: "Mac",
 device_type: "Desktop",
 country: "US",
 region: "Washington",
 city: "Seattle",
 latitude: 47.5412,
 longitude: -122.275,
 utm_source: nil,
 utm_medium: nil,
 utm_term: nil,
 utm_content: nil,
 utm_campaign: nil,
 app_version: nil,
 os_version: nil,
 platform: nil
 started_at: Fri, 06 Oct 2023 11:37:49.004724000 UTC +00:00>

获取随机IP地址的方法:

1.使用随机方法:

Array.new(4){rand(256)}.join('.')

1.在开发/测试中使用Faker gem:

Faker::Internet.ip_v4_address

制作

在添加Geocoder Gem并如上所述使用Ahoy配置它之后,它将正常工作。

相关问题