-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from loftwah/dl/analytics
Dl/analytics
- Loading branch information
Showing
23 changed files
with
289 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
class PageViewTracker | ||
def initialize(app) | ||
@app = app | ||
end | ||
|
||
def call(env) | ||
request = ActionDispatch::Request.new(env) | ||
status, headers, response = @app.call(env) | ||
|
||
if html_response?(headers) && !request.path.start_with?('/assets', '/rails/active_storage') | ||
track_page_view(request) | ||
end | ||
|
||
[status, headers, response] | ||
end | ||
|
||
private | ||
|
||
def html_response?(headers) | ||
headers['Content-Type']&.include?('text/html') | ||
end | ||
|
||
def track_page_view(request) | ||
user = User.find_by(username: request.path.split('/').last) | ||
PageView.create( | ||
user: user, | ||
path: request.path, | ||
referrer: request.referrer, | ||
browser: request.user_agent, | ||
visited_at: Time.current, | ||
ip_address: request.ip, | ||
session_id: request.session[:session_id] | ||
) if user | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
class AchievementView < ApplicationRecord | ||
belongs_to :achievement | ||
belongs_to :user | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
class LinkClick < ApplicationRecord | ||
belongs_to :link | ||
belongs_to :user | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class PageView < ApplicationRecord | ||
belongs_to :user | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class CreatePageViews < ActiveRecord::Migration[7.1] | ||
def change | ||
create_table :page_views do |t| | ||
t.references :user, null: false, foreign_key: true | ||
t.string :path | ||
t.string :referrer | ||
t.string :browser | ||
t.datetime :visited_at | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class CreateLinkClicks < ActiveRecord::Migration[7.1] | ||
def change | ||
create_table :link_clicks do |t| | ||
t.references :link, null: false, foreign_key: true | ||
t.references :user, null: false, foreign_key: true | ||
t.datetime :clicked_at | ||
t.string :referrer | ||
t.string :browser | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class CreateAchievementViews < ActiveRecord::Migration[7.1] | ||
def change | ||
create_table :achievement_views do |t| | ||
t.references :achievement, null: false, foreign_key: true | ||
t.references :user, null: false, foreign_key: true | ||
t.datetime :viewed_at | ||
t.string :referrer | ||
t.string :browser | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
6 changes: 6 additions & 0 deletions
6
db/migrate/20240818123616_add_ip_address_and_session_id_to_achievement_views.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class AddIpAddressAndSessionIdToAchievementViews < ActiveRecord::Migration[7.1] | ||
def change | ||
add_column :achievement_views, :ip_address, :string | ||
add_column :achievement_views, :session_id, :string | ||
end | ||
end |
6 changes: 6 additions & 0 deletions
6
db/migrate/20240818123618_add_ip_address_and_session_id_to_link_clicks.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class AddIpAddressAndSessionIdToLinkClicks < ActiveRecord::Migration[7.1] | ||
def change | ||
add_column :link_clicks, :ip_address, :string | ||
add_column :link_clicks, :session_id, :string | ||
end | ||
end |
6 changes: 6 additions & 0 deletions
6
db/migrate/20240818123619_add_ip_address_and_session_id_to_page_views.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class AddIpAddressAndSessionIdToPageViews < ActiveRecord::Migration[7.1] | ||
def change | ||
add_column :page_views, :ip_address, :string | ||
add_column :page_views, :session_id, :string | ||
end | ||
end |
Oops, something went wrong.