I can't get Ahoy to work with Geocoder and the problem is related to this Ahoy line:
module Ahoy
class GeocodeV2Job < ActiveJob::Base
[...]
if location && location.country.present? # <= but my location.country is never present...
This is really odd because, from my console, I get this:
> heroku run rails c
> visit = Ahoy::Visit.last
> location = Geocoder.search(visit.ip).first
=> #<Geocoder::Result::Geoip2:0x00000006e14910 @data=#<MaxMindDB::Result:0x00000006e150b8 @raw={"city"=>{"geoname_id"=>3170918, "names"=>{"de"=>"Piasco", "en"=>"Piasco", "fr"=>"Piasco", "pt-BR"=>"Piasco"}}, "continent"=>{"code"=>"EU", "geoname_id"=>6255148, "names"=>{"de"=>"Europa", "en"=>"Europe", "es"=>"Europa", "fr"=>"Europe", "ja"=>"ヨーロッパ", "pt-BR"=>"Europa", "ru"=>"Европа", "zh-CN"=>"欧洲"}}, "country"=>{"geoname_id"=>3175395, "is_in_european_union"=>true, "iso_code"=>"IT", "names"=>{"de"=>"Italien", "en"=>"Italy", "es"=>"Italia", "fr"=>"Italie", "ja"=>"イタリア共和国", "pt-BR"=>"Itália", "ru"=>"Италия", "zh-CN"=>"意大利"}}, "location"=>{"accuracy_radius"=>50, "latitude"=>44.5611, "longitude"=>7.4442, "time_zone"=>"Europe/Rome"}, "postal"=>{"code"=>"12026"}, "registered_country"=>{"geoname_id"=>3175395, "is_in_european_union"=>true, "iso_code"=>"IT", "names"=>{"de"=>"Italien", "en"=>"Italy", "es"=>"Italia", "fr"=>"Italie", "ja"=>"イタリア共和国", "pt-BR"=>"Itália", "ru"=>"Италия", "zh-CN"=>"意大利"}}, "subdivisions"=>[{"geoname_id"=>3170831, "iso_code"=>"21", "names"=>{"de"=>"Piemont", "en"=>"Piedmont", "es"=>"Piamonte", "fr"=>"Piémont", "ja"=>"ピエモンテ州", "ru"=>"Пьемонт", "zh-CN"=>"皮埃蒙特"}}, {"geoname_id"=>3177699, "iso_code"=>"CN", "names"=>{"en"=>"Provincia di Cuneo", "fr"=>"Coni"}}], "network"=>"85.159.180.0/23"}>, @cache_hit=nil>
So location is not nil! But then:
> location.country
=> ""
Even though: 👀
irb(main):008:0> location.data['country']['names']['en']
=> "Italy"
Please note that location
is a:
> location.class
=> Geocoder::Result::Geoip2
I'm riding:
> Rails.version
=> "4.2.8"
> Ahoy::VERSION
=> "2.2.0"
Using geocoder 1.5.0.
And these are my initializers:
module Ahoy
class Store < Ahoy::DatabaseStore
def track_visit(data)
data[:ip] = request.headers['CF-Connecting-IP'] if request.headers['CF-Connecting-IP'].present? # Because I'm using Cloudflare
super(data)
end
def track_event(data)
data[:properties][:subdomain] = request.subdomain
data[:properties][:domain] = request.domain
super(data)
end
end
end
Ahoy.api = true
Ahoy.geocode = true
Geocoder.configure(
lookup: :location_iq,
api_key: ENV['LOCATION_IQ_ACCESS_TKN'],
language: 'it',
cache: Rails.cache,
ip_lookup: :geoip2,
geoip2: {
file: Rails.root.join('lib', 'geocoder', 'GeoLite2-City.mmdb')
}
)
What am I missing? Thank you!
Ok, I figured this out. The problem, as usual, is my homeland ;-)
In other words... in my geocoder initializer I set 'it'
as the default language but the GeoLite2-City.mmdb
database, embedded in the gem 'maxminddb'
, doesn't support Italian.
So I just have to figure out how to make just the ip_lookup work in English and it all be fine :)