Skip to content

Commit

Permalink
Disabled geocoding by default, and made the geocoder gem an optional …
Browse files Browse the repository at this point in the history
…dependency
  • Loading branch information
ankane committed Aug 13, 2021
1 parent 781cac2 commit 04e42ae
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 63 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,7 @@
## 4.0.0 (unreleased)

- Disabled geocoding by default
- Made the `geocoder` gem an optional dependency
- Dropped support for Ruby < 2.6 and Rails < 5.2

## 3.3.0 (2021-08-13)
Expand Down
69 changes: 16 additions & 53 deletions README.md
Expand Up @@ -4,6 +4,8 @@

Track visits and events in Ruby, JavaScript, and native apps. Data is stored in your database by default, and you can customize it for any data store as you grow.

**Ahoy 4.0 was recently released** - see [how to upgrade](#upgrading)

:postbox: Check out [Ahoy Email](https://github.com/ankane/ahoy_email) for emails and [Field Test](https://github.com/ankane/field_test) for A/B testing

:tangerine: Battle-tested at [Instacart](https://www.instacart.com/opensource)
Expand Down Expand Up @@ -350,7 +352,13 @@ Safely.report_exception_method = ->(e) { Rollbar.error(e) }

Ahoy uses [Geocoder](https://github.com/alexreisner/geocoder) for geocoding. We recommend configuring [local geocoding](#local-geocoding) or [load balancer geocoding](#load-balancer-geocoding) so IP addresses are not sent to a 3rd party service. If you do use a 3rd party service and adhere to GDPR, be sure to add it to your subprocessor list. If Ahoy is configured to [mask IPs](#ip-masking), the masked IP is used (this can reduce accuracy but is better for privacy).

To enable geocoding, update `config/initializers/ahoy.rb`:
To enable geocoding, add this line to your application’s Gemfile:

```ruby
gem 'geocoder'
```

And update `config/initializers/ahoy.rb`:

```ruby
Ahoy.geocode = true
Expand Down Expand Up @@ -743,62 +751,17 @@ Send a `POST` request to `/ahoy/events` with `Content-Type: application/json` an

## Upgrading

### 3.0

If you installed Ahoy before 2.1 and want to keep legacy user agent parsing and bot detection, add to your Gemfile:
### 4.0

```ruby
gem "browser", "~> 2.0"
gem "user_agent_parser"
```
There are two notable changes to geocoding:

And add to `config/initializers/ahoy.rb`:

```ruby
Ahoy.user_agent_parser = :legacy
```

### 2.2

Ahoy now ships with better bot detection if you use Device Detector. This should be more accurate but can significantly reduce the number of visits recorded. For existing installs, it’s opt-in to start. To use it, add to `config/initializers/ahoy.rb`:

```ruby
Ahoy.bot_detection_version = 2
```

### 2.1

Ahoy recommends [Device Detector](https://github.com/podigee/device_detector) for user agent parsing and makes it the default for new installations. To switch, add to `config/initializers/ahoy.rb`:

```ruby
Ahoy.user_agent_parser = :device_detector
```

Backfill existing records with:

```ruby
Ahoy::Visit.find_each do |visit|
client = DeviceDetector.new(visit.user_agent)
device_type =
case client.device_type
when "smartphone"
"Mobile"
when "tv"
"TV"
else
client.device_type.try(:titleize)
end

visit.browser = client.name
visit.os = client.os_name
visit.device_type = device_type
visit.save(validate: false) if visit.changed?
end
```
1. Geocoding is now disabled by default (this was already the case for new installations with 3.2.0+). Check out the instructions for [how to enable it](#geocoding).

### 2.0
2. The `geocoder` gem is now an optional dependency. To use geocoding, add it to your Gemfile:

See the [upgrade guide](docs/Ahoy-2-Upgrade.md)
```ruby
gem 'geocoder'
```

## History

Expand Down
1 change: 0 additions & 1 deletion ahoy_matey.gemspec
Expand Up @@ -16,7 +16,6 @@ Gem::Specification.new do |spec|
spec.required_ruby_version = ">= 2.6"

spec.add_dependency "activesupport", ">= 5.2"
spec.add_dependency "geocoder", ">= 1.4.5"
spec.add_dependency "safely_block", ">= 0.2.1"
spec.add_dependency "device_detector"
end
3 changes: 3 additions & 0 deletions app/jobs/ahoy/geocode_v2_job.rb
Expand Up @@ -6,6 +6,9 @@ def perform(visit_token, ip)
location =
begin
Geocoder.search(ip).first
rescue NameError
# geocoder gem not installed
raise
rescue => e
Ahoy.log "Geocode error: #{e.class.name}: #{e.message}"
nil
Expand Down
3 changes: 1 addition & 2 deletions lib/ahoy.rb
Expand Up @@ -4,7 +4,6 @@
# dependencies
require "active_support"
require "active_support/core_ext"
require "geocoder"
require "safely/core"

# modules
Expand Down Expand Up @@ -44,7 +43,7 @@ module Ahoy
self.quiet = true

mattr_accessor :geocode
self.geocode = true
self.geocode = false

mattr_accessor :max_content_length
self.max_content_length = 8192
Expand Down
4 changes: 2 additions & 2 deletions lib/generators/ahoy/templates/base_store_initializer.rb.tt
Expand Up @@ -19,7 +19,7 @@ end
# set to true for JavaScript tracking
Ahoy.api = false

# set to true for geocoding
# we recommend configuring local geocoding first
# set to true for geocoding (and add the geocoder gem to your Gemfile)
# we recommend configuring local geocoding as well
# see https://github.com/ankane/ahoy#geocoding
Ahoy.geocode = false
Expand Up @@ -4,7 +4,7 @@ end
# set to true for JavaScript tracking
Ahoy.api = false

# set to true for geocoding
# we recommend configuring local geocoding first
# set to true for geocoding (and add the geocoder gem to your Gemfile)
# we recommend configuring local geocoding as well
# see https://github.com/ankane/ahoy#geocoding
Ahoy.geocode = false
13 changes: 10 additions & 3 deletions test/controller_test.rb
Expand Up @@ -264,8 +264,10 @@ def test_token_generator
end

def test_geocode_true
assert_enqueued_with(job: Ahoy::GeocodeV2Job, queue: "ahoy") do
get products_url
with_options(geocode: true) do
assert_enqueued_with(job: Ahoy::GeocodeV2Job, queue: "ahoy") do
get products_url
end
end
end

Expand All @@ -276,8 +278,13 @@ def test_geocode_false
end
end

def test_geocode_default
get products_url
assert_equal 0, enqueued_jobs.size
end

def test_job_queue
with_options(job_queue: :low_priority) do
with_options(geocode: true, job_queue: :low_priority) do
assert_enqueued_with(job: Ahoy::GeocodeV2Job, queue: "low_priority") do
get products_url
end
Expand Down

0 comments on commit 04e42ae

Please sign in to comment.