Skip to content

Commit

Permalink
Merge pull request #21 from datopian/ga4-upgrade
Browse files Browse the repository at this point in the history
[ga][s]: updated js scripts to support GA4 property
  • Loading branch information
sagargg authored Jun 8, 2023
2 parents b55025c + fb75328 commit 4327d0c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 11 deletions.
5 changes: 4 additions & 1 deletion ckanext/nhs/fanstatic/js/google_analytics_event_tracking.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ this.ckan.module('google_analytics_event_tracking', function(jQuery, _) {
jQuery('a.resource-url-analytics').on('click', function() {
var resource_name = (jQuery(this).prop('href')).split('/').pop();
if (resource_name && analytics_enabled) {
ga('send', 'event', 'Resource', 'Download', resource_name);
gtag('event', "Download", {
'event_category': "Resource",
'event_label': resource_name,
});
}
});
});
Expand Down
14 changes: 4 additions & 10 deletions ckanext/nhs/fanstatic/js/google_analytics_header.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,10 @@ ckan.module('google_analytics_header', function(jQuery, _) {

this.sandbox.subscribe('analytics_enabled', function (analytics_enabled) {
if(analytics_enabled){
(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', googleanalytics_id, 'auto');
ga('set', 'anonymizeIp', true);
ga('send', 'pageview');

window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', googleanalytics_id);
}
});
},
Expand Down
1 change: 1 addition & 0 deletions ckanext/nhs/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
{% block googleanalytics_header %}
{% resource 'nhs/js/google_analytics_header.js' %}
{% set googleanalytics_config = h.get_googleanalytics_config() %}
<script async src="https://www.googletagmanager.com/gtag/js?id={{ googleanalytics_config.get('googleanalytics_id') }}"></script>
<div class="js-hide" data-module="google_analytics_header"
data-module-googleanalytics_id="{{ googleanalytics_config.get('googleanalytics_id') }}">
</div>
Expand Down

0 comments on commit 4327d0c

Please sign in to comment.