ahoy_matey 3.0.5 → 4.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 17572400fdaab440040c753cf23084f2a47868e5c1ed12229e85fd1904e9c937
4
- data.tar.gz: cc917af046906e308830fa1ffa9543db1bc1fe0f79488ccaddef090e6366433d
3
+ metadata.gz: fc5fc1f8ed102bd4a3fa7e78a5404c7d40b698d59cbbc644c5322e2bcb1b2e52
4
+ data.tar.gz: 7100ab100ed3a6ae66a4c4aa6b14c3c2e817c273ab677b7befbc6350fec5cff8
5
5
  SHA512:
6
- metadata.gz: 325caa06934a20bf7a58f573fff7359b7058cf63e3e7576c48faf1bd5d08ac2564810a28e5a775c3ac324170754db4de35d46f43c0b421f8c4977760af19d153
7
- data.tar.gz: e7af674522e09e3fb472b52546f30d10b743e2b2814efd97162d05cb1d4f9b13615dce211cd83e291da16e370a86cf7d8d384b16ad60d8af07c8ced8fbd17f59
6
+ metadata.gz: a41dd47317ab2932e0ddcc9231f3cca78887cdf2cf98ca3506cb0ccd8f1f70ed050dafe8e17e764224fe29018ba5cb35afab5068059338f63e832298d98547cd
7
+ data.tar.gz: f170800b1f7490aec5be31dab5e59f5824ab0a261ceb4fac9b5a1ad31fd8525c7f424c3f2080c8e75f9564763a69c9c6a56a854c848d1453cc3299b5f5c822f0
data/CHANGELOG.md CHANGED
@@ -1,3 +1,59 @@
1
+ ## 4.2.1 (2023-02-23)
2
+
3
+ - Updated Ahoy.js to 0.4.2
4
+
5
+ ## 4.2.0 (2023-02-07)
6
+
7
+ - Added primary key type to generated migration
8
+ - Updated Ahoy.js to 0.4.1
9
+
10
+ ## 4.1.0 (2022-06-12)
11
+
12
+ - Ensure `exclude_method` is only called once per request
13
+ - Fixed error with Mongoid when `Mongoid.raise_not_found_error` is `true`
14
+ - Fixed association for Mongoid
15
+
16
+ ## 4.0.3 (2022-01-15)
17
+
18
+ - Support for `importmap-rails` is no longer experimental
19
+ - Fixed asset precompilation error with `importmap-rails`
20
+
21
+ ## 4.0.2 (2021-11-06)
22
+
23
+ - Added experimental support for `importmap-rails`
24
+
25
+ ## 4.0.1 (2021-08-18)
26
+
27
+ - Added support for `where_event`, `where_props`, and `where_group` for SQLite
28
+ - Fixed results with `where_event` for MySQL, MariaDB, and Postgres `hstore`
29
+ - Fixed results with `where_props` and `where_group` when used with other scopes for MySQL, MariaDB, and Postgres `hstore`
30
+
31
+ ## 4.0.0 (2021-08-14)
32
+
33
+ - Disabled geocoding by default (this was already the case for new installations with 3.2.0+)
34
+ - Made the `geocoder` gem an optional dependency
35
+ - Updated Ahoy.js to 0.4.0
36
+ - Updated API to return 400 status code when missing required parameters
37
+ - Dropped support for Ruby < 2.6 and Rails < 5.2
38
+
39
+ ## 3.3.0 (2021-08-13)
40
+
41
+ - Added `country_code` to geocoding
42
+ - Updated Ahoy.js to 0.3.9
43
+ - Fixed install generator for MariaDB
44
+
45
+ ## 3.2.0 (2021-03-01)
46
+
47
+ - Disabled geocoding by default for new installations
48
+ - Fixed deprecation warning with Active Record 6.1
49
+
50
+ ## 3.1.0 (2020-12-04)
51
+
52
+ - Added `instance` method
53
+ - Added `request` argument to `user_method`
54
+ - Updated Ahoy.js to 0.3.8
55
+ - Removed `exclude_method` call when geocoding
56
+
1
57
  ## 3.0.5 (2020-09-09)
2
58
 
3
59
  - Added `group_prop` method
data/LICENSE.txt CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2014-2020 Andrew Kane
1
+ Copyright (c) 2014-2023 Andrew Kane
2
2
 
3
3
  MIT License
4
4
 
data/README.md CHANGED
@@ -2,20 +2,20 @@
2
2
 
3
3
  :fire: Simple, powerful, first-party analytics for Rails
4
4
 
5
- Track visits and events in Ruby, JavaScript, and native apps. Data is stored in your database by default so you can easily combine it with other data.
5
+ 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.
6
6
 
7
7
  :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
8
8
 
9
9
  :tangerine: Battle-tested at [Instacart](https://www.instacart.com/opensource)
10
10
 
11
- [![Build Status](https://travis-ci.org/ankane/ahoy.svg?branch=master)](https://travis-ci.org/ankane/ahoy)
11
+ [![Build Status](https://github.com/ankane/ahoy/workflows/build/badge.svg?branch=master)](https://github.com/ankane/ahoy/actions)
12
12
 
13
13
  ## Installation
14
14
 
15
15
  Add this line to your application’s Gemfile:
16
16
 
17
17
  ```ruby
18
- gem 'ahoy_matey'
18
+ gem "ahoy_matey"
19
19
  ```
20
20
 
21
21
  And run:
@@ -46,6 +46,18 @@ And restart your web server.
46
46
 
47
47
  ### JavaScript
48
48
 
49
+ For Rails 7 / Importmap, add to `config/importmap.rb`:
50
+
51
+ ```ruby
52
+ pin "ahoy", to: "ahoy.js"
53
+ ```
54
+
55
+ And add to `app/javascript/application.js`:
56
+
57
+ ```javascript
58
+ import "ahoy"
59
+ ```
60
+
49
61
  For Rails 6 / Webpacker, run:
50
62
 
51
63
  ```sh
@@ -55,7 +67,7 @@ yarn add ahoy.js
55
67
  And add to `app/javascript/packs/application.js`:
56
68
 
57
69
  ```javascript
58
- import ahoy from "ahoy.js";
70
+ import ahoy from "ahoy.js"
59
71
  ```
60
72
 
61
73
  For Rails 5 / Sprockets, add to `app/assets/javascripts/application.js`:
@@ -70,6 +82,14 @@ Track an event with:
70
82
  ahoy.track("My second event", {language: "JavaScript"});
71
83
  ```
72
84
 
85
+ ### Native Apps
86
+
87
+ Check out [Ahoy iOS](https://github.com/namolnad/ahoy-ios) and [Ahoy Android](https://github.com/instacart/ahoy-android).
88
+
89
+ ### Geocoding Setup
90
+
91
+ To enable geocoding, see the [Geocoding section](#geocoding).
92
+
73
93
  ### GDPR Compliance
74
94
 
75
95
  Ahoy provides a number of options to help with GDPR compliance. See the [GDPR section](#gdpr-compliance-1) for more info.
@@ -135,17 +155,11 @@ end
135
155
  ahoy.track("Viewed book", {title: "The World is Flat"});
136
156
  ```
137
157
 
138
- Track events automatically with:
139
-
140
- ```javascript
141
- ahoy.trackAll();
142
- ```
143
-
144
158
  See [Ahoy.js](https://github.com/ankane/ahoy.js) for a complete list of features.
145
159
 
146
160
  #### Native Apps
147
161
 
148
- For Android, check out [Ahoy Android](https://github.com/instacart/ahoy-android). For other platforms, see the [API spec](#api-spec).
162
+ See the docs for [Ahoy iOS](https://github.com/namolnad/ahoy-ios) and [Ahoy Android](https://github.com/instacart/ahoy-android).
149
163
 
150
164
  #### AMP
151
165
 
@@ -181,9 +195,9 @@ Order.joins(:ahoy_visit).group("device_type").count
181
195
  Here’s what the migration to add the `ahoy_visit_id` column should look like:
182
196
 
183
197
  ```ruby
184
- class AddVisitIdToOrders < ActiveRecord::Migration[6.0]
198
+ class AddAhoyVisitToOrders < ActiveRecord::Migration[7.0]
185
199
  def change
186
- add_column :orders, :ahoy_visit_id, :bigint
200
+ add_reference :orders, :ahoy_visit
187
201
  end
188
202
  end
189
203
  ```
@@ -196,7 +210,7 @@ visitable :sign_up_visit
196
210
 
197
211
  ### Users
198
212
 
199
- Ahoy automatically attaches the `current_user` to the visit. With [Devise](https://github.com/plataformatec/devise), it attaches the user even if he or she signs in after the visit starts.
213
+ Ahoy automatically attaches the `current_user` to the visit. With [Devise](https://github.com/plataformatec/devise), it attaches the user even if they sign in after the visit starts.
200
214
 
201
215
  With other authentication frameworks, add this to the end of your sign in method:
202
216
 
@@ -306,71 +320,118 @@ Set other [cookie options](https://api.rubyonrails.org/classes/ActionDispatch/Co
306
320
  Ahoy.cookie_options = {same_site: :lax}
307
321
  ```
308
322
 
309
- ### Geocoding
323
+ You can also [disable cookies](#anonymity-sets--cookies)
310
324
 
311
- Disable geocoding with:
325
+ ### Token Generation
326
+
327
+ Ahoy uses random UUIDs for visit and visitor tokens by default, but you can use your own generator like [Druuid](https://github.com/recurly/druuid).
312
328
 
313
329
  ```ruby
314
- Ahoy.geocode = false
330
+ Ahoy.token_generator = -> { Druuid.gen }
315
331
  ```
316
332
 
317
- The default job queue is `:ahoy`. Change this with:
333
+ ### Throttling
334
+
335
+ You can use [Rack::Attack](https://github.com/kickstarter/rack-attack) to throttle requests to the API.
318
336
 
319
337
  ```ruby
320
- Ahoy.job_queue = :low_priority
338
+ class Rack::Attack
339
+ throttle("ahoy/ip", limit: 20, period: 1.minute) do |req|
340
+ if req.path.start_with?("/ahoy/")
341
+ req.ip
342
+ end
343
+ end
344
+ end
321
345
  ```
322
346
 
323
- #### Geocoding Performance
347
+ ### Exceptions
324
348
 
325
- To avoid calls to a remote API, download the [GeoLite2 City database](https://dev.maxmind.com/geoip/geoip2/geolite2/) and configure Geocoder to use it.
349
+ Exceptions are rescued so analytics do not break your app. Ahoy uses [Safely](https://github.com/ankane/safely) to try to report them to a service by default. To customize this, use:
326
350
 
327
- Add this line to your application’s Gemfile:
351
+ ```ruby
352
+ Safely.report_exception_method = ->(e) { Rollbar.error(e) }
353
+ ```
354
+
355
+ ## Geocoding
356
+
357
+ 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).
358
+
359
+ To enable geocoding, add this line to your application’s Gemfile:
360
+
361
+ ```ruby
362
+ gem "geocoder"
363
+ ```
364
+
365
+ And update `config/initializers/ahoy.rb`:
328
366
 
329
367
  ```ruby
330
- gem 'maxminddb'
368
+ Ahoy.geocode = true
331
369
  ```
332
370
 
333
- And create an initializer at `config/initializers/geocoder.rb` with:
371
+ Geocoding is performed in a background job so it doesn’t slow down web requests. The default job queue is `:ahoy`. Change this with:
372
+
373
+ ```ruby
374
+ Ahoy.job_queue = :low_priority
375
+ ```
376
+
377
+ ### Local Geocoding
378
+
379
+ For privacy and performance, we recommend geocoding locally. Add this line to your application’s Gemfile:
380
+
381
+ ```ruby
382
+ gem "maxminddb"
383
+ ```
384
+
385
+ For city-level geocoding, download the [GeoLite2 City database](https://dev.maxmind.com/geoip/geoip2/geolite2/) and create `config/initializers/geocoder.rb` with:
334
386
 
335
387
  ```ruby
336
388
  Geocoder.configure(
337
389
  ip_lookup: :geoip2,
338
390
  geoip2: {
339
- file: Rails.root.join("lib", "GeoLite2-City.mmdb")
391
+ file: "path/to/GeoLite2-City.mmdb"
340
392
  }
341
393
  )
342
394
  ```
343
395
 
344
- If you use Heroku, you can use an unofficial buildpack like [this one](https://github.com/temedica/heroku-buildpack-maxmind-geolite2) to avoid including the database in your repo.
396
+ For country-level geocoding, install the `geoip-database` package. It’s preinstalled on Heroku. For Ubuntu, use:
345
397
 
346
- ### Token Generation
398
+ ```sh
399
+ sudo apt-get install geoip-database
400
+ ```
347
401
 
348
- Ahoy uses random UUIDs for visit and visitor tokens by default, but you can use your own generator like [Druuid](https://github.com/recurly/druuid).
402
+ And create `config/initializers/geocoder.rb` with:
349
403
 
350
404
  ```ruby
351
- Ahoy.token_generator = -> { Druuid.gen }
405
+ Geocoder.configure(
406
+ ip_lookup: :maxmind_local,
407
+ maxmind_local: {
408
+ file: "/usr/share/GeoIP/GeoIP.dat",
409
+ package: :country
410
+ }
411
+ )
352
412
  ```
353
413
 
354
- ### Throttling
355
-
356
- You can use [Rack::Attack](https://github.com/kickstarter/rack-attack) to throttle requests to the API.
414
+ ### Load Balancer Geocoding
357
415
 
358
- ```ruby
359
- class Rack::Attack
360
- throttle("ahoy/ip", limit: 20, period: 1.minute) do |req|
361
- if req.path.start_with?("/ahoy/")
362
- req.ip
363
- end
364
- end
365
- end
366
- ```
416
+ Some load balancers can add geocoding information to request headers.
367
417
 
368
- ### Exceptions
418
+ - [nginx](https://nginx.org/en/docs/http/ngx_http_geoip_module.html)
419
+ - [Google Cloud](https://cloud.google.com/load-balancing/docs/custom-headers)
420
+ - [Cloudflare](https://support.cloudflare.com/hc/en-us/articles/200168236-Configuring-Cloudflare-IP-Geolocation)
369
421
 
370
- Exceptions are rescued so analytics do not break your app. Ahoy uses [Safely](https://github.com/ankane/safely) to try to report them to a service by default. To customize this, use:
422
+ Update `config/initializers/ahoy.rb` with:
371
423
 
372
424
  ```ruby
373
- Safely.report_exception_method = ->(e) { Rollbar.error(e) }
425
+ Ahoy.geocode = false
426
+
427
+ class Ahoy::Store < Ahoy::DatabaseStore
428
+ def track_visit(data)
429
+ data[:country] = request.headers["<country-header>"]
430
+ data[:region] = request.headers["<region-header>"]
431
+ data[:city] = request.headers["<city-header>"]
432
+ super(data)
433
+ end
434
+ end
374
435
  ```
375
436
 
376
437
  ## GDPR Compliance
@@ -431,7 +492,13 @@ Ahoy can switch from cookies to [anonymity sets](https://privacypatterns.org/pat
431
492
  Ahoy.cookies = false
432
493
  ```
433
494
 
434
- Previously set cookies are automatically deleted.
495
+ Previously set cookies are automatically deleted. If you use JavaScript tracking, also set:
496
+
497
+ ```javascript
498
+ ahoy.configure({cookies: false});
499
+ ```
500
+
501
+ Note: With anonymity sets, visits no longer expire after 4 hours of inactivity. A new visit is only created when the IP mask or user agent changes (for instance, when a user updates their browser). There are plans to address this in the next major version.
435
502
 
436
503
  ## Data Retention
437
504
 
@@ -606,7 +673,7 @@ Group by properties with:
606
673
  Ahoy::Event.group_prop(:product_id, :category).count
607
674
  ```
608
675
 
609
- Note: MySQL and MariaDB always return string keys (include `"null"` for `nil`) for `group_prop`.
676
+ Note: MySQL and MariaDB always return string keys (including `"null"` for `nil`) for `group_prop`.
610
677
 
611
678
  ### Funnels
612
679
 
@@ -639,6 +706,24 @@ daily_visits = Ahoy::Visit.group_by_day(:started_at).count # uses Groupdate
639
706
  Prophet.forecast(daily_visits)
640
707
  ```
641
708
 
709
+ ### Anomaly Detection
710
+
711
+ To detect anomalies in visits and events, check out [AnomalyDetection.rb](https://github.com/ankane/AnomalyDetection.rb).
712
+
713
+ ```ruby
714
+ daily_visits = Ahoy::Visit.group_by_day(:started_at).count # uses Groupdate
715
+ AnomalyDetection.detect(daily_visits, period: 7)
716
+ ```
717
+
718
+ ### Breakout Detection
719
+
720
+ To detect breakouts in visits and events, check out [Breakout](https://github.com/ankane/breakout).
721
+
722
+ ```ruby
723
+ daily_visits = Ahoy::Visit.group_by_day(:started_at).count # uses Groupdate
724
+ Breakout.detect(daily_visits)
725
+ ```
726
+
642
727
  ### Recommendations
643
728
 
644
729
  To make recommendations based on events, check out [Disco](https://github.com/ankane/disco#ahoy).
@@ -690,62 +775,19 @@ Send a `POST` request to `/ahoy/events` with `Content-Type: application/json` an
690
775
 
691
776
  ## Upgrading
692
777
 
693
- ### 3.0
694
-
695
- If you installed Ahoy before 2.1 and want to keep legacy user agent parsing and bot detection, add to your Gemfile:
696
-
697
- ```ruby
698
- gem "browser", "~> 2.0"
699
- gem "user_agent_parser"
700
- ```
701
-
702
- And add to `config/initializers/ahoy.rb`:
703
-
704
- ```ruby
705
- Ahoy.user_agent_parser = :legacy
706
- ```
778
+ ### 4.0
707
779
 
708
- ### 2.2
780
+ There are two notable changes to geocoding:
709
781
 
710
- 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`:
782
+ 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).
711
783
 
712
- ```ruby
713
- Ahoy.bot_detection_version = 2
714
- ```
715
-
716
- ### 2.1
717
-
718
- 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`:
719
-
720
- ```ruby
721
- Ahoy.user_agent_parser = :device_detector
722
- ```
723
-
724
- Backfill existing records with:
725
-
726
- ```ruby
727
- Ahoy::Visit.find_each do |visit|
728
- client = DeviceDetector.new(visit.user_agent)
729
- device_type =
730
- case client.device_type
731
- when "smartphone"
732
- "Mobile"
733
- when "tv"
734
- "TV"
735
- else
736
- client.device_type.try(:titleize)
737
- end
738
-
739
- visit.browser = client.name
740
- visit.os = client.os_name
741
- visit.device_type = device_type
742
- visit.save(validate: false) if visit.changed?
743
- end
744
- ```
784
+ 2. The `geocoder` gem is now an optional dependency. To use geocoding, add it to your Gemfile:
745
785
 
746
- ### 2.0
786
+ ```ruby
787
+ gem "geocoder"
788
+ ```
747
789
 
748
- See the [upgrade guide](docs/Ahoy-2-Upgrade.md)
790
+ Also, check out the [upgrade notes](https://github.com/ankane/ahoy.js#upgrading) for Ahoy.js.
749
791
 
750
792
  ## History
751
793
 
@@ -769,10 +811,10 @@ bundle install
769
811
  bundle exec rake test
770
812
  ```
771
813
 
772
- To test query methods, start PostgreSQL, MySQL, and MongoDB and use:
814
+ To test different adapters, use:
773
815
 
774
816
  ```sh
775
- createdb ahoy_test
776
- mysqladmin create ahoy_test
777
- bundle exec rake test:query_methods
817
+ ADAPTER=postgresql bundle exec rake test
818
+ ADAPTER=mysql2 bundle exec rake test
819
+ ADAPTER=mongoid bundle exec rake test
778
820
  ```
@@ -5,19 +5,27 @@ module Ahoy
5
5
  skip_after_action(*filters, raise: false)
6
6
  skip_around_action(*filters, raise: false)
7
7
 
8
- before_action :verify_request_size
9
- before_action :renew_cookies
10
-
11
8
  if respond_to?(:protect_from_forgery)
12
9
  protect_from_forgery with: :null_session, if: -> { Ahoy.protect_from_forgery }
13
10
  end
14
11
 
12
+ before_action :verify_request_size
13
+ before_action :check_params
14
+ before_action :renew_cookies
15
+
15
16
  protected
16
17
 
17
18
  def ahoy
18
19
  @ahoy ||= Ahoy::Tracker.new(controller: self, api: true)
19
20
  end
20
21
 
22
+ def check_params
23
+ if ahoy.send(:missing_params?)
24
+ logger.info "[ahoy] Missing required parameters"
25
+ render plain: "Missing required parameters\n", status: :bad_request
26
+ end
27
+ end
28
+
21
29
  # set proper ttl if cookie generated from JavaScript
22
30
  # approach is not perfect, as user must reload the page
23
31
  # for new cookie settings to take effect
@@ -28,7 +36,7 @@ module Ahoy
28
36
  def verify_request_size
29
37
  if request.content_length > Ahoy.max_content_length
30
38
  logger.info "[ahoy] Payload too large"
31
- render plain: "Payload too large\n", status: 413
39
+ render plain: "Payload too large\n", status: :payload_too_large
32
40
  end
33
41
  end
34
42
  end
@@ -1,4 +1,5 @@
1
1
  # for smooth update from Ahoy 1 -> 2
2
+ # TODO remove in 5.0
2
3
  module Ahoy
3
4
  class GeocodeJob < ActiveJob::Base
4
5
  queue_as { Ahoy.job_queue }
@@ -6,6 +6,8 @@ module Ahoy
6
6
  location =
7
7
  begin
8
8
  Geocoder.search(ip).first
9
+ rescue NameError
10
+ raise "Add the geocoder gem to your Gemfile to use geocoding"
9
11
  rescue => e
10
12
  Ahoy.log "Geocode error: #{e.class.name}: #{e.message}"
11
13
  nil
@@ -14,6 +16,7 @@ module Ahoy
14
16
  if location && location.country.present?
15
17
  data = {
16
18
  country: location.country,
19
+ country_code: location.try(:country_code).presence,
17
20
  region: location.try(:state).presence,
18
21
  city: location.try(:city).presence,
19
22
  postal_code: location.try(:postal_code).presence,
@@ -24,7 +24,11 @@ module Ahoy
24
24
  def user
25
25
  @user ||= begin
26
26
  if Ahoy.user_method.respond_to?(:call)
27
- Ahoy.user_method.call(controller)
27
+ if Ahoy.user_method.arity == 1
28
+ Ahoy.user_method.call(controller)
29
+ else
30
+ Ahoy.user_method.call(controller, request)
31
+ end
28
32
  else
29
33
  controller.send(Ahoy.user_method) if controller.respond_to?(Ahoy.user_method, true)
30
34
  end
@@ -39,12 +39,12 @@ module Ahoy
39
39
  end
40
40
 
41
41
  def set_ahoy_request_store
42
- previous_value = Thread.current[:ahoy]
42
+ previous_value = Ahoy.instance
43
43
  begin
44
- Thread.current[:ahoy] = ahoy
44
+ Ahoy.instance = ahoy
45
45
  yield
46
46
  ensure
47
- Thread.current[:ahoy] = previous_value
47
+ Ahoy.instance = previous_value
48
48
  end
49
49
  end
50
50
  end
@@ -53,7 +53,12 @@ module Ahoy
53
53
 
54
54
  def visit
55
55
  unless defined?(@visit)
56
- @visit = visit_model.where(visit_token: ahoy.visit_token).first if ahoy.visit_token
56
+ if defined?(Mongoid::Document) && visit_model < Mongoid::Document
57
+ # find_by raises error by default when not found
58
+ @visit = visit_model.where(visit_token: ahoy.visit_token).first if ahoy.visit_token
59
+ else
60
+ @visit = visit_model.find_by(visit_token: ahoy.visit_token) if ahoy.visit_token
61
+ end
57
62
  end
58
63
  @visit
59
64
  end
data/lib/ahoy/engine.rb CHANGED
@@ -26,5 +26,12 @@ module Ahoy
26
26
  alias_method :call, :call_with_quiet_ahoy
27
27
  end
28
28
  end
29
+
30
+ # for importmap
31
+ initializer "ahoy.importmap" do |app|
32
+ if defined?(Importmap)
33
+ app.config.assets.precompile << "ahoy.js"
34
+ end
35
+ end
29
36
  end
30
37
  end
data/lib/ahoy/model.rb CHANGED
@@ -7,7 +7,7 @@ module Ahoy
7
7
  end
8
8
  class_eval %{
9
9
  def set_ahoy_visit
10
- self.#{name} ||= Thread.current[:ahoy].try(:visit_or_create)
10
+ self.#{name} ||= Ahoy.instance.try(:visit_or_create)
11
11
  end
12
12
  }
13
13
  end