Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support cloudflare #143

Merged
merged 1 commit into from
Sep 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions app/middleware/page_view_tracker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,18 @@ def html_response?(headers)
def track_page_view(request)
user = User.find_by(username: request.path.split('/').last)
if user
location = OFFLINE_GEOCODER.search(request.ip)
# Extract the original IP from the Cloudflare headers if available
real_ip = request.headers['CF-Connecting-IP'] || request.headers['X-Forwarded-For']&.split(',')&.first || request.ip

location = OFFLINE_GEOCODER.search(real_ip)

PageView.create(
user: user,
path: request.path,
referrer: request.referrer,
browser: request.user_agent,
visited_at: Time.current,
ip_address: request.ip,
ip_address: real_ip,
session_id: request.session[:session_id],
country: location[:country],
city: location[:name],
Expand All @@ -46,5 +49,5 @@ def track_page_view(request)
Rails.logger.info "Duplicate page view detected and ignored"
rescue => e
Rails.logger.error "Error tracking page view: #{e.message}"
end
end
end
Loading