Skip to content

Commit 54bd8b6

Browse files
committed
Only track usage with GTM when consent given
We now record user cookie preferences in a cookie set by a cookie banner when a Google Tag Manager tracking ID is set in the config. This ensures we aren't tracking usage data without user consent when using Google Tag Manager Doing the same when using gtag.js (the other approach to tracking data for Google Analytics 4) would be a breaking change, but it would be good to start asking consent in both cases eventually
1 parent 53ff594 commit 54bd8b6

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed
Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
<% if config[:tech_docs][:ga4_gtm_tracking_id].is_a?(String) && !config[:tech_docs][:ga4_gtm_tracking_id].empty? %>
22
<!-- Google Tag Manager -->
3-
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
4-
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
5-
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
6-
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
7-
})(window,document,'script','dataLayer','<%= config[:tech_docs][:ga4_gtm_tracking_id] %>');</script>
3+
<script>
4+
const cookies_policy_cookie = document.cookie
5+
.split('; ')
6+
.find(cookie => cookie.split('=')[0] === 'cookies_policy')
7+
?.split('=')[1]
8+
9+
if (cookies_policy_cookie && JSON.parse(cookies_policy_cookie).usage) {
10+
(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
11+
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
12+
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
13+
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
14+
})(window,document,'script','dataLayer','<%= config[:tech_docs][:ga4_gtm_tracking_id] %>');
15+
}
16+
</script>
817
<!-- End Google Tag Manager -->
918
<% end %>

0 commit comments

Comments
 (0)