joshmn / ahoy_captain

A full-featured, mountable analytics dashboard for your Rails app, powered by the Ahoy gem.
MIT License
356 stars 18 forks source link

Ahoy captain not recognizing data or failing to initialize #6

Closed oyenmwen closed 10 months ago

oyenmwen commented 1 year ago

Hi I have a rails 7 esbuild app and am trying to get it to work with ahoy_captain. When I visit /ahoy_captain, I do not get any results:

img

I also get this error and other warnings in the console:

img

I followed the installation instructions to the tee (although, rails g ahoy_captain:migration does not work).

I also checked that ahoy_visits table has data

Any help is appreciated!

joshmn commented 1 year ago

What happens if you select a period like year-to-date? What happens if you try to fetch the data using ActiveRecord based on the queries its reporting? Anything curious in your Rails logs?

http://localhost:3000/ahoy_captain?period=ytd should get you there quick.

oyenmwen commented 1 year ago

Same results come up. Here are the logs for it:

17:24:03 web.1  | Processing by AhoyCaptain::Stats::UniqueVisitorsController#index as HTML
17:24:03 web.1  |   Parameters: {"period"=>"ytd"}
17:24:03 web.1  |   Ahoy::Visit Count (0.9ms)  SELECT COUNT(DISTINCT "ahoy_visits"."id") AS "count_id", DATE_TRUNC('day', "ahoy_visits"."started_at"::timestamptz AT TIME ZONE 'Etc/UTC')::date AS "date_trunc_day_ahoy_visits_started_at_timestamptz_at_time_zone_" FROM "ahoy_visits" LEFT OUTER JOIN "ahoy_events" ON "ahoy_events"."visit_id" = "ahoy_visits"."id" WHERE ("ahoy_visits"."started_at" > '2023-01-01 00:00:00' AND "ahoy_visits"."started_at" < '2023-08-03 21:24:03.807444' AND "ahoy_events"."time" > '2023-01-01 00:00:00' AND "ahoy_events"."time" < '2023-08-03 21:24:03.807502') AND ("ahoy_visits"."started_at" IS NOT NULL) GROUP BY DATE_TRUNC('day', "ahoy_visits"."started_at"::timestamptz AT TIME ZONE 'Etc/UTC')::date /*application:Blens,controller:unique_visitors,action:index*/
17:24:03 web.1  |   Rendering layout ...
17:24:03 web.1  |   Rendering ...
17:24:03 web.1  |   Rendered .... (Duration: 0.2ms | Allocations: 119)
17:24:03 web.1  |   Rendered layout .... (Duration: 58.9ms | Allocations: 50800)
17:24:03 web.1  | Completed 200 OK in 66ms (Views: 59.5ms | ActiveRecord: 0.9ms | Allocations: 54561)
17:24:03 web.1  | 

When I try the query using Active Record I actually get no results as well. I think its because of my ahoy_events table is empty. So i guess I need to track events as well. Thought it could work with visits only.

Removing the left outer join of ahoy_events does return results though...

SELECT COUNT(DISTINCT "ahoy_visits"."id") AS "count_id", 
         DATE_TRUNC('day', "ahoy_visits"."started_at"::timestamptz AT TIME ZONE 'Etc/UTC')::date 
       AS "date_trunc_day_ahoy_visits_started_at_timestamptz_at_time_zone_" 
FROM "ahoy_visits"
WHERE ("ahoy_visits"."started_at" > '2023-01-01 00:00:00' 
        AND "ahoy_visits"."started_at" < '2023-08-03 21:24:03.807444' 
        AND "ahoy_visits"."started_at" IS NOT NULL
       ) 
GROUP BY DATE_TRUNC('day', "ahoy_visits"."started_at"::timestamptz AT TIME ZONE 'Etc/UTC')

While with ahoy_events return 0 results

SELECT COUNT(DISTINCT "ahoy_visits"."id") AS "count_id", 
       DATE_TRUNC('day', "ahoy_visits"."started_at"::timestamptz AT TIME ZONE 'Etc/UTC')::date AS "date_trunc_day_ahoy_visits_started_at_timestamptz_at_time_zone_" 
FROM "ahoy_visits" LEFT OUTER JOIN "ahoy_events" 
ON "ahoy_events"."visit_id" = "ahoy_visits"."id" 
WHERE ("ahoy_visits"."started_at" > '2023-01-01 00:00:00' AND "ahoy_visits"."started_at" < '2023-08-03 21:24:03.807444' 
    AND "ahoy_events"."time" > '2023-01-01 00:00:00' 
    AND "ahoy_events"."time" < '2023-08-03 21:24:03.807502') 
    AND ("ahoy_visits"."started_at" IS NOT NULL) 
GROUP BY DATE_TRUNC('day', "ahoy_visits"."started_at"::timestamptz AT TIME ZONE 'Etc/UTC')

What are my paths forward?

davidalejandroaguilar commented 1 year ago

Same thing happened to me (installed both ahoy and ahoy_captain at the same time, hence, no data).

I just pointed the gem to the main branch (since there was no release yesterday) and it's fixed there. Try the 0.8 release and see if it goes away.

I still get NaN on "Views per visit" but at least the 0's are good.

oyenmwen commented 1 year ago

Yes that helped, thanks so much. Still get the NaN as well

joshmn commented 1 year ago

@oyenmwen should be fixed in 0.81 which was just released.

there's a fix for queries coming up here shortly. just need to figure out why some are misbehaving.

joshmn commented 1 year ago

@oyenmwen give 0.82 a try. lots of query fixes.

davidalejandroaguilar commented 1 year ago

I'm not getting data on 0.83, yet I do when I'm on 0.8.

0.8

image

Query

23:00:03 web.1             | Started GET "/ahoy_captain/stats/unique_visitors?period=all" for 127.0.0.1 at 2023-08-07 23:00:03 -0700
23:00:03 web.1             | Processing by AhoyCaptain::Stats::UniqueVisitorsController#index as HTML
23:00:03 web.1             |   Parameters: {"period"=>"all"}
23:00:03 web.1             |   Ahoy::Visit Count (1.2ms)  SELECT COUNT(DISTINCT "ahoy_visits"."visitor_token") AS "count_visitor_token", DATE_TRUNC('month', "ahoy_visits"."started_at"::timestamptz AT TIME ZONE 'Etc/UTC')::date AS "date_trunc_month_ahoy_visits_started_at_timestamptz_at_time_zon" FROM "ahoy_visits" LEFT OUTER JOIN "ahoy_events" ON "ahoy_events"."visit_id" = "ahoy_visits"."id" WHERE (("ahoy_visits"."started_at" > '2004-08-01 00:00:00' AND "ahoy_visits"."started_at" < '2023-08-08 06:00:03.948980') OR ("ahoy_events"."time" > '2004-08-01 00:00:00' AND "ahoy_events"."time" < '2023-08-08 06:00:03.949274')) AND ("ahoy_visits"."started_at" IS NOT NULL) GROUP BY DATE_TRUNC('month', "ahoy_visits"."started_at"::timestamptz AT TIME ZONE 'Etc/UTC')::date

0.83

image

Query

23:02:43 web.1             | Started GET "/ahoy_captain/?period=all" for 127.0.0.1 at 2023-08-07 23:02:43 -0700
23:02:43 web.1             | Processing by AhoyCaptain::Stats::UniqueVisitorsController#index as HTML
23:02:43 web.1             |   Parameters: {"period"=>"all"}
23:02:43 web.1             |   Ahoy::Visit Count (0.8ms)  SELECT COUNT(DISTINCT "ahoy_visits"."visitor_token") AS "count_visitor_token", DATE_TRUNC('month', "ahoy_visits"."started_at"::timestamptz AT TIME ZONE 'Etc/UTC')::date AS "date_trunc_month_ahoy_visits_started_at_timestamptz_at_time_zon" FROM "ahoy_visits" LEFT OUTER JOIN "ahoy_events" ON "ahoy_events"."visit_id" = "ahoy_visits"."id" WHERE ("ahoy_visits"."started_at" > '2004-08-01 00:00:00' AND "ahoy_visits"."started_at" < '2023-08-08 06:02:43.956371') AND ("ahoy_events"."time" > '2004-08-01 00:00:00' AND "ahoy_events"."time" < '2023-08-08 06:02:43.956679') AND ("ahoy_visits"."started_at" IS NOT NULL) GROUP BY DATE_TRUNC('month', "ahoy_visits"."started_at"::timestamptz AT TIME ZONE 'Etc/UTC')::date

Data

select started_at from ahoy_visits;

         started_at         
----------------------------
 2023-08-05 07:20:42.858902
 2023-08-08 01:21:02.29804
 2023-08-08 05:39:20.770545
 2023-08-08 05:48:14.778695
 2023-08-08 05:48:32.6127

select time from ahoy_events;

 time 
------
(0 rows)

I think it's this part of the query:

AND ("ahoy_events"."time" > '2004-08-01 00:00:00' AND "ahoy_events"."time" < '2023-08-08 06:14:32.823934')

If you don't have any events, it will not fetch any records.

joshmn commented 1 year ago

Are your page view events setup correctly?

I would check:

AhoyCaptain.event.with_routes.count 
AhoyCaptain.event.where(name: AhoyCaptain.config.event[:view_name]).count
davidalejandroaguilar commented 1 year ago

@joshmn Thanks for your response, no I don't have page view events set up since there was no mention of that on the ahoy repo or the ahoy_captain one. I now see that https://github.com/joshmn/ahoy_captain/commit/e7ad0c60ba1902d9ab0ba514e02097e21bb4e360 added clarification, however, I think this is not clear:

By default, AhoyCaptain assumes you're tracking controller and action in your Ahoy::Event properties, and a page view event is named $view.

I assumed it means ahoy_captain expect us to track all actions like this section in the ahoy readme does. Something like:

class ApplicationController < ActionController::Base
  after_action :track_page_view

  protected

  def track_page_view
    ahoy.track "$view", controller: controller_name, action: action_name, url: request.url, params: request.path_parameters
  end
end

If so, it'd probably be good to add the above snippet to the README, or inject it via a generator. In addition, these configs should probably be enabled by default, in order to provide good, batteries included defaults:

config.event.url_column = "properties->>'url'"
config.event.url_exists = "JSONB_EXISTS(properties, 'controller') AND JSONB_EXISTS(properties, 'action')"

I tested the above and it works great!

joshmn commented 1 year ago

@davidalejandroaguilar how's this? https://github.com/joshmn/ahoy_captain/commit/f8246fcc8520088d9d495e310c134331d31cf455 :)

davidalejandroaguilar commented 1 year ago

That works, thank you very much for your time in both responding and building this gem 😺

oyenmwen commented 10 months ago

So my problem was that I wasnt tracking actions with ahoy events. Worked once i added that. Thanks!