Skip to content

Commit

Permalink
feat(service): collect google analytics data
Browse files Browse the repository at this point in the history
  • Loading branch information
serenaabbott11 committed Nov 24, 2023
1 parent 11ba40b commit 05d691f
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 26 deletions.
1 change: 0 additions & 1 deletion app/assets/config/manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@
//= link_tree ../../javascript/faf
//= link_tree ../../javascript/misc
//= link_tree ../../javascript/request
//= link_tree ../../javascript/google-tag-manager
30 changes: 28 additions & 2 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def maintenance

protected

helper_method :current_user, :cookie_policy, :record_ga?, :engagement_portal?, :support_portal?, :frameworks_portal?, :current_url_b64
helper_method :current_user, :cookie_policy, :internal_portal?, :google_analytics_id, :hosted_development?, :hosted_staging?, :record_ga?, :engagement_portal?, :support_portal?, :frameworks_portal?, :current_url_b64

# @return [User, Guest]
#
Expand Down Expand Up @@ -79,8 +79,34 @@ def portal_namespace
:none
end

def google_analytics_id
if hosted_development?
"G-JHQ4K916N1"
elsif hosted_staging?
"G-8770N0RLNE"
elsif hosted_production?
"G-PT4157VC9D"
end
end

def internal_portal?
support_portal? || engagement_portal? || frameworks_portal?
end

def hosted_development?
request.url.starts_with?("https://dev.")
end

def hosted_production?
request.url.starts_with?("https://www.")
end

def hosted_staging?
request.url.starts_with?("https://staging.")
end

def record_ga?
!(support_portal? || engagement_portal? || frameworks_portal?)
google_analytics_id.present? && (internal_portal? || cookie_policy.accepted?)
end

def cookie_policy
Expand Down
1 change: 0 additions & 1 deletion app/javascript/google-tag-manager/activate.js

This file was deleted.

7 changes: 0 additions & 7 deletions app/javascript/google-tag-manager/include.js

This file was deleted.

2 changes: 0 additions & 2 deletions app/views/cookie_preferences/update_cookies_accepted.js.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
document.getElementById("cookie-step-1").setAttribute("hidden", true);
document.getElementById("cookie-step-accepted").removeAttribute("hidden");

window.setupGoogleTagManager();
16 changes: 3 additions & 13 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@
<html lang="en" class="govuk-template ">

<head>
<% if Rails.env.production? && record_ga? %>
<%= javascript_include_tag "google-tag-manager/include" %>

<% if cookie_policy.accepted? %>
<%= javascript_include_tag "google-tag-manager/activate" %>
<% end %>
<% if record_ga? %>
<!-- Google tag (gtag.js) --> <script async nonce=<%= content_security_policy_nonce%> src="https://www.googletagmanager.com/gtag/js?id=<%= google_analytics_id %>"></script> <script nonce=<%= content_security_policy_nonce%>> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', "<%= google_analytics_id %>"); </script>
<% end %>

<% if Rails.env.production? && ENV['ApplicationInsights__ConnectionString'].present? %>
Expand Down Expand Up @@ -57,18 +53,12 @@
</head>

<body class="govuk-template__body">
<% if Rails.env.production? && record_ga? && cookie_policy.accepted? %>
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-PBXQDGR"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->
<% end %>

<%= javascript_tag nonce: true do -%>
document.body.className = ((document.body.className) ? document.body.className + ' js-enabled' : 'js-enabled');
<% end -%>

<% if record_ga? %>
<% unless internal_portal? %>
<%= render "cookie_preferences/banner" %>
<% end %>

Expand Down

0 comments on commit 05d691f

Please sign in to comment.