-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Thomas Scherz
committed
Jun 21, 2023
1 parent
96ce61e
commit b38ead9
Showing
3 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"> |