From bde5064c9c9ead3dfb8e4285dabc97aad77414b1 Mon Sep 17 00:00:00 2001 From: Jeremy Friesen Date: Thu, 6 Jan 2022 12:21:07 -0500 Subject: [PATCH] Skip google analytics when ga_tracking_id is nil (#15967) * 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]:https://github.com/forem/forem/blob/528bd2baa608292f3e603c7a7587832a46cc791a/app/views/layouts/admin.html.erb#L31 [2]:https://github.com/forem/forem/blob/528bd2baa608292f3e603c7a7587832a46cc791a/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 --- .env_sample | 3 +- .../initializeBaseTracking.js.erb | 46 ++++++++++--------- app/controllers/ga_events_controller.rb | 38 +++++++-------- app/views/pages/badge.html.erb | 34 +++++++------- 4 files changed, 64 insertions(+), 57 deletions(-) diff --git a/.env_sample b/.env_sample index 50100c7a5..5890ec9fe 100644 --- a/.env_sample +++ b/.env_sample @@ -117,7 +117,8 @@ HONEYCOMB_API_KEY="" # Google analytics # (https://developers.google.com/analytics/devguides/reporting/core/v4) -GA_TRACKING_ID="Optional" +# When blank don't render Google Analytics. +GA_TRACKING_ID="" # Mailchimp for mails # (https://mailchimp.com/developer/) diff --git a/app/assets/javascripts/initializers/initializeBaseTracking.js.erb b/app/assets/javascripts/initializers/initializeBaseTracking.js.erb index afe3c100b..4de0846ae 100644 --- a/app/assets/javascripts/initializers/initializeBaseTracking.js.erb +++ b/app/assets/javascripts/initializers/initializeBaseTracking.js.erb @@ -2,28 +2,30 @@ function initializeBaseTracking() { var wait = 0; var addedGA = false; var gaTrackingCode = document.body.dataset.gaTracking; - var waitingOnGA = setInterval(function() { - if (!addedGA) { - (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ - (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), - m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) - })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); - } - addedGA = true; - wait++; - if (window.ga && ga.create) { - ga('create', gaTrackingCode, 'auto'); - ga('set', 'anonymizeIp', true); - ga('send', 'pageview', location.pathname + location.search); - clearInterval(waitingOnGA); - } - if (wait > 85) { - clearInterval(waitingOnGA); - fallbackActivityRecording(); - } - }, 25); - eventListening(); - trackCustomImpressions(); + if (gaTrackingCode) { + var waitingOnGA = setInterval(function() { + if (!addedGA) { + (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ + (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), + m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) + })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); + } + addedGA = true; + wait++; + if (window.ga && ga.create) { + ga('create', gaTrackingCode, 'auto'); + ga('set', 'anonymizeIp', true); + ga('send', 'pageview', location.pathname + location.search); + clearInterval(waitingOnGA); + } + if (wait > 85) { + clearInterval(waitingOnGA); + fallbackActivityRecording(); + } + }, 25); + eventListening(); + trackCustomImpressions(); + } } function fallbackActivityRecording() { diff --git a/app/controllers/ga_events_controller.rb b/app/controllers/ga_events_controller.rb index f6bc7e938..21d283b98 100644 --- a/app/controllers/ga_events_controller.rb +++ b/app/controllers/ga_events_controller.rb @@ -6,24 +6,26 @@ class GaEventsController < ApplicationController # 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(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", - ) + 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 diff --git a/app/views/pages/badge.html.erb b/app/views/pages/badge.html.erb index cad5f7ec2..af9159956 100644 --- a/app/views/pages/badge.html.erb +++ b/app/views/pages/badge.html.erb @@ -103,24 +103,26 @@ } } - + ga('create', '<%= Settings::General.ga_tracking_id %>', 'auto'); + ga('set', 'anonymizeIp', true); + ga('send', 'pageview'); + ga('send', 'event', 'view', '/badge version impression', '<%= @html_variant.name %>', null); + + <% end %> <% end %>