Skip to content
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

Support for GA4 Tag Manager #350

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion example/config/tech-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ footer_links:
Accessibility: /hidden-page.html
Hidden Page: /hidden-page.html

# Tracking ID from Google Analytics (e.g. UA-XXXX-Y)
# Tracking ID (if using GA4 e.g G-XXXXXX)
ga4_tracking_id:

# Tracking ID from Google Universal Analytics (e.g. UA-XXXX-Y)
ga_tracking_id:

# Enable multipage navigation in the sidebar
Expand Down
10 changes: 10 additions & 0 deletions lib/source/layouts/_analytics_head.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<% if config[:tech_docs][:ga4_tracking_id].is_a?(String) && !config[:tech_docs][:ga4_tracking_id].empty? %>
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=<%= config[:tech_docs][:ga4_tracking_id]%>"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '<%= config[:tech_docs][:ga4_tracking_id] %>');
</script>
<% end %>
2 changes: 2 additions & 0 deletions lib/source/layouts/core.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<!doctype html>
<html lang="en" class="govuk-template no-js">
<head>
<%= partial 'layouts/analytics_head' %>

<meta content="IE=edge" http-equiv="X-UA-Compatible">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
Expand Down