Skip to content

Commit

Permalink
Merge pull request HyperCable#22 from HyperCable/traffic_campaign
Browse files Browse the repository at this point in the history
add traffic campaign page
  • Loading branch information
hooopo authored Mar 7, 2021
2 parents ff45766 + 5db815f commit c3f0f2a
Show file tree
Hide file tree
Showing 13 changed files with 126 additions and 2 deletions.
3 changes: 3 additions & 0 deletions app/assets/stylesheets/traffic_campaigns.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the traffic_campaigns controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: https://sass-lang.com/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/traffic_sources.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the traffic_sources controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: https://sass-lang.com/
17 changes: 17 additions & 0 deletions app/controllers/traffic_campaigns_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

class TrafficCampaignsController < ApplicationController
layout "detail"

def index
@site = current_user.sites.find_by!(uuid: params[:site_id])

base = QueryBuilder.call(Hyper::Event, @site, params).result
current_scope = QueryBuilder.call(Hyper::Event, @site, period: "realtime").result
@current_visitors_count = current_scope.distinct.count(:client_id)
@top_traffic_campaigns = base
.where(event_name: "page_view")
.select("traffic_campaign, count(distinct client_id) as visitors_count, count(*) as count, count(distinct session_id) as sessions_count")
.group("site_id, traffic_campaign").order("2 desc").limit(100)
end
end
17 changes: 17 additions & 0 deletions app/controllers/traffic_sources_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

class TrafficSourcesController < ApplicationController
layout "detail"

def index
@site = current_user.sites.find_by!(uuid: params[:site_id])

base = QueryBuilder.call(Hyper::Event, @site, params).result
current_scope = QueryBuilder.call(Hyper::Event, @site, period: "realtime").result
@current_visitors_count = current_scope.distinct.count(:client_id)
@top_traffic_sources = base
.where(event_name: "page_view")
.select("traffic_source, count(distinct client_id) as visitors_count, count(*) as count, count(distinct session_id) as sessions_count")
.group("site_id, traffic_source").order("2 desc").limit(100)
end
end
4 changes: 4 additions & 0 deletions app/helpers/traffic_campaigns_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

module TrafficCampaignsHelper
end
4 changes: 4 additions & 0 deletions app/helpers/traffic_sources_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

module TrafficSourcesHelper
end
2 changes: 1 addition & 1 deletion app/views/shared/sources/_traffic_campaign.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</div>

<div class="text-center w-full absolute bottom-0 left-0 pb-3">
<a class="leading-snug font-bold text-sm text-gray-500 dark:text-gray-400 hover:text-red-500 dark:hover:text-red-400 transition tracking-wide" href="/hackershare.dev/sources"><svg class="feather mr-1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="margin-top: -2px;"><path d="M8 3H5a2 2 0 0 0-2 2v3m18 0V5a2 2 0 0 0-2-2h-3m0 18h3a2 2 0 0 0 2-2v-3M3 16v3a2 2 0 0 0 2 2h3"></path></svg>MORE
<a class="leading-snug font-bold text-sm text-gray-500 dark:text-gray-400 hover:text-red-500 dark:hover:text-red-400 transition tracking-wide" href="<%= site_traffic_campaigns_path(@site, request.query_parameters) %>"><svg class="feather mr-1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="margin-top: -2px;"><path d="M8 3H5a2 2 0 0 0-2 2v3m18 0V5a2 2 0 0 0-2-2h-3m0 18h3a2 2 0 0 0 2-2v-3M3 16v3a2 2 0 0 0 2 2h3"></path></svg>MORE
</a>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion app/views/shared/sources/_traffic_source.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</div>

<div class="text-center w-full absolute bottom-0 left-0 pb-3">
<a class="leading-snug font-bold text-sm text-gray-500 dark:text-gray-400 hover:text-red-500 dark:hover:text-red-400 transition tracking-wide" href=""><svg class="feather mr-1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="margin-top: -2px;"><path d="M8 3H5a2 2 0 0 0-2 2v3m18 0V5a2 2 0 0 0-2-2h-3m0 18h3a2 2 0 0 0 2-2v-3M3 16v3a2 2 0 0 0 2 2h3"></path></svg>MORE
<a class="leading-snug font-bold text-sm text-gray-500 dark:text-gray-400 hover:text-red-500 dark:hover:text-red-400 transition tracking-wide" href="<%= site_traffic_sources_path(@site, request.query_parameters) %>"><svg class="feather mr-1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="margin-top: -2px;"><path d="M8 3H5a2 2 0 0 0-2 2v3m18 0V5a2 2 0 0 0-2-2h-3m0 18h3a2 2 0 0 0 2-2v-3M3 16v3a2 2 0 0 0 2 2h3"></path></svg>MORE
</a>
</div>
</div>
Expand Down
28 changes: 28 additions & 0 deletions app/views/traffic_campaigns/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<div class="dark:bg-gray-800">
<h1 class="text-xl font-bold dark:text-gray-100">Top Traffic Campaign</h1>
<div class="my-4 border-b border-gray-300">

</div>
<main class="modal__content">
<table class="w-full table-striped table-fixed">
<thead>
<tr>
<th class="p-2 text-xs tracking-wide font-bold text-gray-500 dark:text-gray-400" align="left">Traffic Campaign</th>
<th class="p-2 w-32 text-xs tracking-wide font-bold text-gray-500 dark:text-gray-400" align="right">Visitors</th>
<th class="p-2 w-32 text-xs tracking-wide font-bold text-gray-500 dark:text-gray-400" align="right">Pageviews</th>
<th class="p-2 w-32 text-xs tracking-wide font-bold text-gray-500 dark:text-gray-400" align="right">Sessions</th>
</tr>
</thead>
<tbody>
<% @top_traffic_campaigns.each do |agg| %>
<tr class="text-sm dark:text-gray-200">
<td class="p-2"><%= agg.traffic_campaign %></td>
<td class="p-2 w-32 font-medium" align="right"><%= pretty_num agg.visitors_count %></td>
<td class="p-2 w-32 font-medium" align="right"><%= pretty_num agg.count %></td>
<td class="p-2 w-32 font-medium" align="right"><%= pretty_num agg.sessions_count %></td>
</tr>
<% end %>
</tbody>
</table>
</main>
</div>
28 changes: 28 additions & 0 deletions app/views/traffic_sources/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<div class="dark:bg-gray-800">
<h1 class="text-xl font-bold dark:text-gray-100">Top Traffic Source</h1>
<div class="my-4 border-b border-gray-300">

</div>
<main class="modal__content">
<table class="w-full table-striped table-fixed">
<thead>
<tr>
<th class="p-2 text-xs tracking-wide font-bold text-gray-500 dark:text-gray-400" align="left">Traffic Source</th>
<th class="p-2 w-32 text-xs tracking-wide font-bold text-gray-500 dark:text-gray-400" align="right">Visitors</th>
<th class="p-2 w-32 text-xs tracking-wide font-bold text-gray-500 dark:text-gray-400" align="right">Pageviews</th>
<th class="p-2 w-32 text-xs tracking-wide font-bold text-gray-500 dark:text-gray-400" align="right">Sessions</th>
</tr>
</thead>
<tbody>
<% @top_traffic_sources.each do |agg| %>
<tr class="text-sm dark:text-gray-200">
<td class="p-2"><%= agg.traffic_source %></td>
<td class="p-2 w-32 font-medium" align="right"><%= pretty_num agg.visitors_count %></td>
<td class="p-2 w-32 font-medium" align="right"><%= pretty_num agg.count %></td>
<td class="p-2 w-32 font-medium" align="right"><%= pretty_num agg.sessions_count %></td>
</tr>
<% end %>
</tbody>
</table>
</main>
</div>
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
resources :countries, only: [:index]
resources :referrer_sources, only: [:index]
resources :traffic_mediums, only: [:index]
resources :traffic_sources, only: [:index]
resources :traffic_campaigns, only: [:index]
member do
get "snippet"
get "debug"
Expand Down
9 changes: 9 additions & 0 deletions test/controllers/traffic_campaigns_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

require "test_helper"

class TrafficCampaignsControllerTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end
end
9 changes: 9 additions & 0 deletions test/controllers/traffic_sources_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

require "test_helper"

class TrafficSourcesControllerTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end
end

0 comments on commit c3f0f2a

Please sign in to comment.