-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support Google Tag Manager and Google Tags via an env variable (#…
…455) This is based on the different patterns we've used over the years for supporting this in our apps - in the end I've gone with having a dedicated analytics partial for "head" and "body" to provide a dedicated grouping that'll also hopefully encourage people to think about the situation if they find these files growing significantly. Resolves #439 --------- Co-authored-by: Eoin Kelly <[email protected]>
- Loading branch information
Showing
6 changed files
with
62 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
variants/backend-base/app/views/application/analytics/_body.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<%# analytics configuration that goes in the <body> %> | ||
|
||
<% google_analytics_id = Rails.application.config.app.google_analytics_id %> | ||
|
||
<% if google_analytics_id&.start_with?("GTM-") %> | ||
<!-- Google Tag Manager (noscript) --> | ||
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=<%= google_analytics_id %>" | ||
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript> | ||
<!-- End Google Tag Manager (noscript) --> | ||
<% end %> |
28 changes: 28 additions & 0 deletions
28
variants/backend-base/app/views/application/analytics/_head.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<%# analytics configuration that goes in the <head> %> | ||
|
||
<% google_analytics_id = Rails.application.config.app.google_analytics_id %> | ||
|
||
<% if google_analytics_id&.start_with?("GTM-") %> | ||
<!-- Google Tag Manager --> | ||
<%= javascript_tag nonce: true, defer: true do -%> | ||
(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start': | ||
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0], | ||
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src= | ||
'https://www.googletagmanager.com/gtm.js?id='+i+dl;var n=d.querySelector('[nonce]'); | ||
n&&j.setAttribute('nonce',n.nonce||n.getAttribute('nonce'));f.parentNode.insertBefore(j,f); | ||
})(window,document,'script','dataLayer','<%= google_analytics_id %>'); | ||
<% end -%> | ||
<!-- End Google Tag Manager --> | ||
<% end %> | ||
|
||
<% if google_analytics_id&.start_with?("G-") %> | ||
<!-- Google tag (gtag.js) --> | ||
<%= javascript_include_tag("https://www.googletagmanager.com/gtag/js?id=#{google_analytics_id}", nonce: true, defer: false) -%> | ||
<%= javascript_tag nonce: true, defer: true do -%> | ||
window.dataLayer = window.dataLayer || []; | ||
function gtag(){dataLayer.push(arguments);} | ||
gtag('js', new Date()); | ||
|
||
gtag('config', '<%= google_analytics_id %>'); | ||
<% end -%> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters