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 %>