docbrown/app/controllers/ga_events_controller.rb
Keshav Biswa 8d57d45ecc Rubocop Style cops enabled (#2056)
* Rubocop enabled style/alias

* Enabled Style/ArrayJoin Cop

* Enabled Style/Attr

* Enabled Case Equality

* Enabled CharacterLiteral

*  Enabled ColonMethodCall Cop

* Enabled CommentAnnotation cop

* Enabled PreferredHashMethods Cop

*  Enabled DoubleNegation Cop

* Enabled EachWithObject Cop

* Enabled EmptyLiteral Cop

* Enabled EvenOdd Cop

* Enabled IfWithSemicolon Cop

* Enabled Lambda and LambdaCall Cop

* Enabled LineEndConcatenation Cop

* Enabled ModuleFunction Cop;

* Enable NegatedIf and NegatedWhile Cop

* Enabled NilComparison Cop

* Enabled Not Cop

* Enabled NumericLiterals Cop

* Enabled OneLineConditional Cop

* Enabled PercentLiteralDelimiters Cop

* Excluded internal/users_controller from negated_if cop

* Reverted the double negation change from github_issue_tag and github_issue.rb"

* Enabled PerlBackrefs Cop

* Changed Regexp.last_match(1) to Regexp.last_match(0)

* Enabled proc cop

* Enabled RaiseArgs Cop

* Reverted Regexp.last_match(0) to Regexp.last_match(1)

* Enabled SelfAssignment Cop

* Enabled SingleLineMethods Cop

* Enabled SpecialGlobalVars Cop

* Enabled VariableInterpolation Cop

* Enabled WhenThen Cop

* Enabled WhileUntilModifier Cop

* Enabled WordArray Cop

* Enabled IfUnlessModifier Cop

* Enabled GuardClause Cop
2019-03-15 18:33:54 -04:00

40 lines
1.5 KiB
Ruby

class GaEventsController < ApplicationController
# No authorization required for entirely public controller
# This controller is for tracking activity when GA script fails
# IP is scrambled as to not be persisted to limit fingerprinting abilities on our end.
def create
json = JSON.parse(request.raw_post)
user_id = user_signed_in? ? current_user.id : nil
client_id = "#{scrambled_ip[0..12]}_#{json['user_agent']}_#{user_id}"
tracker = Staccato.tracker(ApplicationConfig["GA_TRACKING_ID"], client_id)
tracker.pageview(
path: json["path"],
user_id: user_id,
user_language: json["user_language"],
referrer: (json["referrer"] if json["referrer"] && !json["referrer"].start_with?("https://dev.to")),
user_agent: json["user_agent"],
viewport_size: json["viewport_size"],
screen_resolution: json["screen_resolution"],
document_title: json["document_title"],
document_encoding: json["document_encoding"],
document_path: json["document_path"],
cache_buster: rand(100_000_000_000).to_s,
data_source: "web",
)
logger.info("Server-Side Google Analytics Tracking - #{client_id}")
render body: nil
end
def scrambled_ip
crypt = ActiveSupport::MessageEncryptor.new(todays_key)
crypt.encrypt_and_sign(request.env["HTTP_X_FORWARDED_FOR"] || request.remote_ip)
end
def todays_key
Rails.cache.fetch("daily_random_key", expires_in: 48.hours) do
SecureRandom.random_bytes(32)
end
end
end