-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: support Google Tag Manager and Google Tags via an env variable #455
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
01d7b7b
feat: support Google Tag Manager and Google Tags via an env variable
G-Rath 1bf0756
fix: update CSP to allow Google Tag Manager when needed
G-Rath 816eab3
fix: copy `app/views/application` directory
G-Rath a23cadf
fix: use `to_json`
G-Rath e7e919b
fix: use tag helpers to include nonce
G-Rath b7b4593
refactor: source google analytics id from app config
G-Rath dddad4b
fix: don't use `to_json`
G-Rath 75c4a7a
fix: use nonce-aware version of GTM snippet
G-Rath 1e3f8fe
fix: update CSP for Google Analytics and Tag Manager
G-Rath f9db9a3
Update Puma configuration to set worker timeout in development enviro…
eoinkelly File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had a quick look at how we implemented this on my most recent client project:
There are two things I think we should steal from this:
javascript_tag nonce: true, ...
to avoid the CSP yelling at us about an embedded<script>
without a noncedataLayer
variable in JS. GTM does not define this for you.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤔 maybe it would be worth linking to https://github.com/google/data-layer-helper in a comment - it's really the best dev focused docs I've found about how to actually use the dataLayer