Skip to content

Commit

Permalink
[Bug Fix] Undefined method for nil in analytics v2 (#1467)
Browse files Browse the repository at this point in the history
Addresses: #1436

Co-authored-by: Prashant <[email protected]>
  • Loading branch information
donrestarone and alis-khadka authored Mar 16, 2023
1 parent 89fb1c8 commit ef6b997
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
5 changes: 4 additions & 1 deletion app/controllers/comfy/admin/v2/dashboard_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@ def dashboard
@visits = Ahoy::Visit.where(started_at: date_range)

filtered_events = Ahoy::Event.joins(:visit)
filtered_events = filtered_events.jsonb_search(:properties, { page_id: params[:page] }) if params[:page].present?

Ahoy::Event::EVENT_CATEGORIES.values.each do |event_category|
if event_category == Ahoy::Event::EVENT_CATEGORIES[:page_visit]
events = filtered_events.where(name: 'comfy-cms-page-visit')
else
events = filtered_events.jsonb_search(:properties, { category: event_category })
end
events = events.jsonb_search(:properties, { page_id: params[:page] }) if params[:page].present?

events = events.filter_records_with_video_details_missing if event_category == Ahoy::Event::EVENT_CATEGORIES[:video_view]

instance_variable_set("@previous_period_#{event_category}_events", events.where(time: previous_period(params[:interval], @start_date, @end_date)))
instance_variable_set("@#{event_category}_events", events.where(time: date_range))
end
Expand Down
1 change: 1 addition & 0 deletions app/helpers/dashboard_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ def top_three_videos(video_view_events, previous_video_view_events)
api_resource = ApiResource.find_by(id: video_event[:resource_id])

video_event[:name] = video_event[:names].uniq.first
video_event[:duration] = video_event[:duration] || 0
video_event[:namespace_id] = video_event[:namespace_ids].uniq.first
video_event[:previous_period_total_views] = total_views(previous_period_event)
video_event[:previous_period_total_watch_time] = total_watch_time(previous_period_event)
Expand Down
10 changes: 10 additions & 0 deletions app/models/ahoy/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ class Ahoy::Event < ApplicationRecord
joins("INNER JOIN (#{subquery}) as api_resourced_events ON api_resourced_events.id = #{table_name}.id")
}

scope :filter_records_with_video_details_missing, -> {
self
.where(
"(properties ->> 'watch_time') IS NOT NULL"\
" AND (properties ->> 'video_start') IS NOT NULL"\
" AND (properties ->> 'total_duration') IS NOT NULL"\
" AND (properties ->> 'resource_id') IS NOT NULL"
)
}

# For events_list page, sorting on the grouped query
# https://stackoverflow.com/a/35987240
ransacker :count do
Expand Down
24 changes: 24 additions & 0 deletions test/controllers/admin/comfy/v2/dashboard_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,28 @@ class Comfy::Admin::V2::DashboardControllerTest < ActionDispatch::IntegrationTes
assert_select ".vr-analytics-page-visit-events-donut-chart script", { count: 1, text: /\"Single time visitor\",2\],\[\"Recurring visitors\",1/ }
assert_select ".vr-analytics-section-header .vr-analytics-percent-change .positive", { count: 1, text: /66.67 %/}
end

test "#dashboard: should not throw error if a video detail is missing in the ahoy_events" do
@subdomain.update!(tracking_enabled: true)
visit = Ahoy::Visit.first

video_watch_event_1 = visit.events.create(name: 'test-video-watched', user_id: @user.id, time: (Time.now.beginning_of_month - 4.days), properties: {"label"=>"watched_this_video", "page_id"=>@page.id, "category"=>"video_view", "resource_id"=>@api_resource.id, "video_start"=>true, "total_duration"=>11944.444})
video_watch_event_2 = visit.events.create(name: 'test-video-watched', user_id: @user.id, time: (Time.now.beginning_of_month - 4.days), properties: {"label"=>"watched_this_video", "page_id"=>@page.id, "category"=>"video_view", "watch_time"=>11891, "video_start"=>true, "total_duration"=>11944.444})
video_watch_event_3 = visit.events.create(name: 'test-video-watched', user_id: @user.id, time: (Time.now.beginning_of_month - 4.days), properties: {"label"=>"watched_this_video", "page_id"=>@page.id, "category"=>"video_view", "watch_time"=>11891, "resource_id"=>@api_resource.id, "total_duration"=>11944.444})
video_watch_event_4 = visit.events.create(name: 'test-video-watched', user_id: @user.id, time: (Time.now.beginning_of_month - 4.days), properties: {"label"=>"watched_this_video", "page_id"=>@page.id, "category"=>"video_view", "watch_time"=>11891, "resource_id"=>@api_resource.id, "video_start"=>true})

video_watch_event_1_page_2 = visit.events.create(name: 'test-video-watched', user_id: @user.id, time: Time.now, properties: {"label"=>"watched_this_video", "page_id"=>@page_2.id, "category"=>"video_view", "resource_id"=>@api_resource.id, "video_start"=>true, "total_duration"=>11944.444})
video_watch_event_2_page_2 = visit.events.create(name: 'test-video-watched', user_id: @user.id, time: Time.now, properties: {"label"=>"watched_this_video", "page_id"=>@page_2.id, "category"=>"video_view", "watch_time"=>11891, "video_start"=>true, "total_duration"=>11944.444})
video_watch_event_3_page_2 = visit.events.create(name: 'test-video-watched', user_id: @user.id, time: Time.now, properties: {"label"=>"watched_this_video", "page_id"=>@page_2.id, "category"=>"video_view", "watch_time"=>11891, "resource_id"=>@api_resource.id, "total_duration"=>11944.444})
video_watch_event_4_page_2 = visit.events.create(name: 'test-video-watched', user_id: @user.id, time: Time.now, properties: {"label"=>"watched_this_video", "page_id"=>@page_2.id, "category"=>"video_view", "watch_time"=>11891, "resource_id"=>@api_resource.id, "video_start"=>true})

@user.update(can_manage_analytics: true)
sign_in(@user)

assert_nothing_raised do
get v2_dashboard_url
end

assert_response :success
end
end

0 comments on commit ef6b997

Please sign in to comment.