Skip to content

Commit

Permalink
Google Analytics version 4 snippet.
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Scherz committed Jun 21, 2023
1 parent 96ce61e commit b38ead9
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 0 deletions.
125 changes: 125 additions & 0 deletions app/assets/javascripts/hyrax/analytics_events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
class TrackingTags {
constructor(provider) {
this.provider = provider
switch(this.provider) {
case 'matomo':
this.tracker = new MatomoTagTracker();
break;
case 'google':
this.tracker = new UATagTracker();
break;
case 'ga4':
this.tracker = new GA4TagTracker();
break;
default:
console.error('Unsupport analytics provider ' + this.provider + ', supported values are: matomo, google, ga4');
}
}

// Track an event with the configured provider
trackTagEvent(category, action, name) {
this.tracker.trackEvent(category, action, name);
}

// Track a page view with the configured provider
trackPageView() {
this.tracker.trackPageView();
}

// Deprecated: use trackTagEvent and trackPageView instead.
analytics() {
return this;
}

// Deprecated: use trackTagEvent and trackPageView instead.
push(params) {
if (params[0] == 'trackPageView' || params[0] == '_trackPageView') {
this.tracker.trackPageView();
} else {
this.tracker.trackTagEvent(params[1], params[2], params[3]);
}
}

// Deprecated
pageView() {
return 'trackPageView';
}

// Deprecated
trackEvent() {
return 'trackEvent';
}
}

class GA4TagTracker {
trackEvent(category, action, name) {
gtag('event', action, {
'category': category,
'name': name
});
}

trackPageView() {
// No operation necessary, this event is automatically collected
}
}

class UATagTracker {
trackEvent(category, action, name) {
_gaq.push(['_trackEvent', category, action, name]);
}

trackPageView() {
_gaq.push(['_trackPageView']);
}
}

class MatomoTagTracker {
trackEvent(category, action, name) {
_paq.push(['trackEvent', category, action, name]);
}

trackPageView() {
_paq.push(['trackPageView']);
}
}

function trackPageView() {
window.trackingTags.trackPageView();
}

function trackAnalyticsEvents() {
$('span.analytics-event').each(function(){
var eventSpan = $(this);
window.trackingTags.trackTagEvent(eventSpan.data('category'), eventSpan.data('action'), eventSpan.data('name'));
})
}

function setupTracking() {
var provider = $('meta[name="analytics-provider"]').prop('content')
if (provider === undefined) {
return;
}
window.trackingTags = new TrackingTags(provider);
trackPageView();
trackAnalyticsEvents();
}

if (typeof Turbolinks !== 'undefined') {
$(document).on('turbolinks:load', function() {
setupTracking();
});
} else {
$(document).on('ready', function() {
setupTracking();
});
}

$(document).on('click', '#file_download', function(e) {
window.trackingTags.trackTagEvent('file-set', 'file-set-download', $(this).data('label'));
window.trackingTags.trackTagEvent('file-set-in-work', 'file-set-in-work-download', $(this).data('work-id'));
$(this).data('collection-ids').forEach(function (collection) {
window.trackingTags.trackTagEvent('file-set-in-collection', 'file-set-in-collection-download', collection);
window.trackingTags.trackTagEvent('work-in-collection', 'work-in-collection-download', collection);
});
});
9 changes: 9 additions & 0 deletions app/views/_ga.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=<%= Hyrax::Analytics.config.analytics_id %>"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '<%= Hyrax::Analytics.config.analytics_id %>');
</script>
<meta name="analytics-provider" content="ga4">
10 changes: 10 additions & 0 deletions app/views/_ga4.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=<%= Hyrax::Analytics.config.analytics_id %>"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());

gtag('config', '<%= Hyrax::Analytics.config.analytics_id %>');
</script>
<meta name="analytics-provider" content="ga4">

0 comments on commit b38ead9

Please sign in to comment.