hyrax
hyrax copied to clipboard
Google Analytics 4 Support
Per Google, on July 1, 2023, Universal Analytics properties will no longer process data. Hyrax will need to support GA4.
HYRAX_ANALYTICS_PROVIDER
could have an additional option (ga4
) or GA4 properties could be detected by checking the prefix. Adding a new js snippet for GA4 properties will be relatively easy.
It looks like GA4 supports turbolinks page navigations out of the box (need to test) so event tracking will be simplified. No need for setupTracking()
on every page refresh. But Hyrax file download links don't end in extensions, so there's work to do to make sure file downloads are tracked.
The GA4 API is different enough that the easy interop between matomo and UA won't work with GA4. Instead of pushing events to a special array, you need to call the gtag
function. For backwards compatibility, we've updated the TrackingTags
class's analytics()
function like so:
analytics() {
if(this.provider === "matomo") {
return _paq;
}
else {
return {
push: function(e){
if (Array.isArray(e) && e.length == 4) {
window.gtag('event', e[1], {'action': e[2], 'name': e[3]});
}
}
};
}
}
That says 'if you're matomo, return the _paq
array object which works as expected. If not (in our env), we're using GA4. In that case, return a new object with a function called 'push' which translates the array pushes from UA/Matomo into gtag()
calls'. It's just a quick hack to prevent js console errors on our test IRs.
Future work:
- Include a GA4 js snippet if the analytics provider is declared to be (or detected to be) GA4 instead of UA or Matomo.
- If GA4 is being used, don't fire any events GA4 can handle natively.
- If GA4 is being used, translate custom analytics events from the old to new API.
- Document setting up a GA4 property with Enhanced event measurement.
- Investigate the impact the UA->GA4 move will have on exposing analytics in the Hyrax UI.
Legato doesn't seem to support GA4.
Thank you for working on this! I just wanted to link to this (one-year-old) incomplete Legato PR for future reference: https://github.com/tpitale/legato/pull/141
Some suggestions were also added in the palni-palci ticket above but work on implementation is not currently planned on this ticket.
For querying analytics for populating Sushi counter metrics, I wrote up: https://github.com/scientist-softserv/hyrax-analytics-g4/tree/main