Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Google Analytics 4 Support #5970

Closed
cudevmaxwell opened this issue Feb 24, 2023 · 4 comments · Fixed by #6074 or #6815
Closed

Google Analytics 4 Support #5970

cudevmaxwell opened this issue Feb 24, 2023 · 4 comments · Fixed by #6074 or #6815

Comments

@cudevmaxwell
Copy link
Contributor

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
Copy link
Contributor Author

Legato doesn't seem to support GA4.

@cazzerson
Copy link

Thank you for working on this! I just wanted to link to this (one-year-old) incomplete Legato PR for future reference: tpitale/legato#141

@crisr15
Copy link

crisr15 commented May 12, 2023

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

@dlpierce dlpierce linked a pull request May 24, 2023 that will close this issue
@jeremyf
Copy link
Contributor

jeremyf commented Dec 6, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants