Extract Google Analytics ID out to an environment variable

TODO- in the future, consider putting this in a credential. We're not able to change the production credentials file currently, but we should change it in the future when we're able
pull/1/head
Liam Morley 4 years ago
parent 1186579d1b
commit 48f2d64c71

@ -1,8 +1,16 @@
class HomeController < ActionController::Base class HomeController < ActionController::Base
before_action :set_google_analytics_id
def index def index
@districts = District.all.order(:name) @districts = District.all.order(:name)
@schools = School.all @schools = School.all
@categories = Category.sorted.map { |category| CategoryPresenter.new(category: category) } @categories = Category.sorted.map { |category| CategoryPresenter.new(category: category) }
end end
private
def set_google_analytics_id
@google_analytics_id = ENV['GOOGLE_ANALYTICS_ID']
end
end end

@ -3,6 +3,7 @@ class SqmApplicationController < ActionController::Base
layout "sqm/application" layout "sqm/application"
before_action :set_schools_and_districts before_action :set_schools_and_districts
before_action :authenticate_district before_action :authenticate_district
before_action :set_google_analytics_id
private private
@ -34,4 +35,8 @@ class SqmApplicationController < ActionController::Base
end end
end end
def set_google_analytics_id
@google_analytics_id = ENV['GOOGLE_ANALYTICS_ID']
end
end end

@ -6,6 +6,18 @@
<%= stylesheet_link_tag 'sqm_application', media: 'all', 'data-turbolinks-track': 'reload' %> <%= stylesheet_link_tag 'sqm_application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= stylesheet_link_tag 'welcome', media: 'all', 'data-turbolinks-track': 'reload' %> <%= stylesheet_link_tag 'welcome', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %> <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script src="https://www.googletagmanager.com/gtag/js?id=<%= @google_analytics_id %>" type="javascript"></script>
<script type="javascript">
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag('js', new Date());
gtag('config', <%= @google_analytics_id.to_json.html_safe %>);
</script>
</head> </head>
<body> <body>

@ -11,7 +11,7 @@
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %> <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
<!-- Global site tag (gtag.js) - Google Analytics --> <!-- Global site tag (gtag.js) - Google Analytics -->
<script src="https://www.googletagmanager.com/gtag/js?id=UA-132936999-1" type="javascript"></script> <script src="https://www.googletagmanager.com/gtag/js?id=<%= @google_analytics_id %>" type="javascript"></script>
<script type="javascript"> <script type="javascript">
window.dataLayer = window.dataLayer || []; window.dataLayer = window.dataLayer || [];
@ -20,7 +20,7 @@
} }
gtag('js', new Date()); gtag('js', new Date());
gtag('config', 'UA-132936999-1'); gtag('config', <%= @google_analytics_id.to_json.html_safe %>);
</script> </script>
</head> </head>

Loading…
Cancel
Save