* Skip google analytics when ga_tracking_id is nil This commit involves short-circuiting Google Analytics calls when the Forem has not configured a `Settings::General.ga_tracking_id`. Note, depending on your local `.env` file (or configured ENV variables) you may have a `GA_TRACKING_ID` value set; I did, it was set to "Optional" which overrode the database setting. We set the HTML data properites in two places: [admin.html.erb][1] and [application.html.erb][2]. In addition, I'm short-circuiting the local fallback analytics call (via [Stacato][https://github.com/tpitale/staccato]). My understanding of Stacato, based on a cursory read, requires a Google Analytics Tracking ID to work. And last, we have a one off of javascript for Google Analytics tracking. This closes #15962. [1]:528bd2baa6/app/views/layouts/admin.html.erb (L31)[2]:528bd2baa6/app/views/layouts/application.html.erb (L55)How to test? - Check your .env file to see if you have set GA_TRACKING_ID. If so, unset it. - Start with a fresh Forem instance. - Open your browser and open the developer tools to inspect the Network activity. - Open the homepage of your local Forem instance (http://localhost:3000) - Filter your Network results for analytics. You shouldn't see any. Also, make sure you're disabling any blockers you might have as that influences things. * Commenting out GA_TRACKING_ID Related to https://github.com/forem/forem/pull/15967 * Favor empty GA_TRACKING_ID env variable
42 lines
1.5 KiB
Ruby
42 lines
1.5 KiB
Ruby
class GaEventsController < ApplicationController
|
|
include ApplicationHelper
|
|
# 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
|
|
if Settings::General.ga_tracking_id.present?
|
|
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(Settings::General.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?(app_url)),
|
|
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",
|
|
)
|
|
end
|
|
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
|