Skip to content

Commit d0d92fd

Browse files
committed
Render Google tag script directly
1 parent 101733e commit d0d92fd

4 files changed

Lines changed: 9 additions & 6 deletions

File tree

app/public/js/google_analytics.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,4 @@
1010

1111
window.gtag("js", new Date());
1212
window.gtag("config", measurementId);
13-
14-
const script = document.createElement("script");
15-
script.async = true;
16-
script.src = `https://www.googletagmanager.com/gtag/js?id=${encodeURIComponent(measurementId)}`;
17-
document.head.appendChild(script);
1813
})();

app/views/layout.erb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747
</script>
4848
<script defer src="<%= h asset_path('/js/navigation.js') %>"></script>
4949
<% if google_analytics_measurement_id %>
50-
<script defer src="<%= h asset_path('/js/google_analytics.js') %>"></script>
50+
<script src="<%= h asset_path('/js/google_analytics.js') %>"></script>
51+
<script async src="<%= h google_analytics_script_url %>"></script>
5152
<% end %>
5253
</head>
5354
<body>

lib/polish_open_source_rank/web/presentation/view_helpers.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ def google_analytics_measurement_id
3737
measurement_id.empty? ? nil : measurement_id
3838
end
3939

40+
def google_analytics_script_url
41+
"https://www.googletagmanager.com/gtag/js?#{Rack::Utils.build_query(id: google_analytics_measurement_id)}"
42+
end
43+
4044
def number(value)
4145
value.to_i.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\1 ').reverse
4246
end

spec/polish_open_source_rank/web/app_spec.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@ def user(_access_token)
155155
'<meta name="google-analytics-measurement-id" content="G-ABC123DEF4">'
156156
)
157157
expect(with_analytics.body).to include('/js/google_analytics.js')
158+
expect(with_analytics.body).to include(
159+
'<script async src="https://www.googletagmanager.com/gtag/js?id=G-ABC123DEF4"></script>'
160+
)
158161
end
159162

160163
it 'redirects legacy latest ranking routes to canonical section paths', :aggregate_failures do

0 commit comments

Comments
 (0)