Skip to content

Commit

Permalink
New feature #18197: Allow Google Analytics 4 tracking (#2907)
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrieljenik authored and c-schmitz committed Mar 29, 2023
1 parent 2688ccc commit 5b1d93b
Showing 1 changed file with 36 additions and 16 deletions.
52 changes: 36 additions & 16 deletions themes/survey/vanilla/views/subviews/header/google_analytics.twig
Expand Up @@ -15,26 +15,46 @@
See: survey settings
#}
{% if aSurveyInfo.googleanalyticsapikey is defined and aSurveyInfo.googleanalyticsapikey and aSurveyInfo.googleanalyticsstyle %}
{% if aSurveyInfo.googleanalyticsstyle is defined and aSurveyInfo.googleanalyticsstyle == 1 %}
<script>
(function(i,s,o,g,r,a,m){ i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments) },i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
{# Check if the tracking ID is a Universal Analytics (legacy) or a Google Analytics 4 ID #}
{% if aSurveyInfo.googleanalyticsapikey starts with 'UA-' %}
{# Legacy UA #}
{% if aSurveyInfo.googleanalyticsstyle is defined and aSurveyInfo.googleanalyticsstyle == 1 %}
<script>
(function(i,s,o,g,r,a,m){ i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments) },i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', '{{aSurveyInfo.googleanalyticsapikey}}', 'auto'); // Replace with your property ID.
ga('send', 'pageview');
</script>
ga('create', '{{aSurveyInfo.googleanalyticsapikey}}', 'auto'); // Replace with your property ID.
ga('send', 'pageview');
</script>
{% else %}
{% set trackUrl = getGoogleAnalyticsTrackingUrl(aSurveyInfo.sid, aSurveyInfo.trackUrlPageName ?? '') %}
<script>
(function(i,s,o,g,r,a,m){ i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments) }
,i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', '{{ aSurveyInfo.googleanalyticsapikey }}', 'auto');
ga('send', 'pageview');
ga('send', 'pageview', '{{ trackUrl }}');
</script>
{% endif %}
{% else %}
{% set trackUrl = getGoogleAnalyticsTrackingUrl(aSurveyInfo.sid, aSurveyInfo.trackUrlPageName ?? '') %}
{# GA4 #}
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id={{aSurveyInfo.googleanalyticsapikey}}"></script>
<script>
(function(i,s,o,g,r,a,m){ i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments) }
,i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '{{aSurveyInfo.googleanalyticsapikey}}');
ga('create', '{{ aSurveyInfo.googleanalyticsapikey }}', 'auto');
ga('send', 'pageview');
ga('send', 'pageview', '{{ trackUrl }}');
{% if not (aSurveyInfo.googleanalyticsstyle is defined and aSurveyInfo.googleanalyticsstyle == 1) %}
{% set trackUrl = getGoogleAnalyticsTrackingUrl(aSurveyInfo.sid, aSurveyInfo.trackUrlPageName ?? '') %}
gtag('event', 'page_view', { page_title: '{{ trackUrl }}' })
{% endif %}
</script>
{% endif %}
{% endif %}

0 comments on commit 5b1d93b

Please sign in to comment.