hyrax icon indicating copy to clipboard operation
hyrax copied to clipboard

Google Analytics 4 Support

Open cudevmaxwell opened this issue 2 years ago • 4 comments

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:

  1. Include a GA4 js snippet if the analytics provider is declared to be (or detected to be) GA4 instead of UA or Matomo.
  2. If GA4 is being used, don't fire any events GA4 can handle natively.
  3. If GA4 is being used, translate custom analytics events from the old to new API.
  4. Document setting up a GA4 property with Enhanced event measurement.
  5. Investigate the impact the UA->GA4 move will have on exposing analytics in the Hyrax UI.

cudevmaxwell avatar Feb 24 '23 17:02 cudevmaxwell

Legato doesn't seem to support GA4.

cudevmaxwell avatar Mar 02 '23 17:03 cudevmaxwell

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

cazzerson avatar May 02 '23 20:05 cazzerson

Some suggestions were also added in the palni-palci ticket above but work on implementation is not currently planned on this ticket.

crisr15 avatar May 12 '23 00:05 crisr15

For querying analytics for populating Sushi counter metrics, I wrote up: https://github.com/scientist-softserv/hyrax-analytics-g4/tree/main

jeremyf avatar Dec 06 '23 16:12 jeremyf