Google Analytics 4 and Open edX?

Is Open edX compatible with Google Analytics 4 ? Will GOOGLE_ANALYTICS_ACCOUNT support a “G-” ID instead of a “UA-” ID ?

Since I got no answer, I set up a new instance and used Google Analytics 4 with it. It does not seem to work as I cannot even see the realtime users connected to my new instance.

I make a workaround for Google Analytics 4 .

  1. Add the code below into lms / templates /main.html :
    <% ga_acct1 = static.get_value(“GOOGLE_ANALYTICS_ACCOUNT_4”, settings.GOOGLE_ANALYTICS_ACCOUNT_4) %>
    % if ga_acct1 == ‘PRO’:

    < script >
    window.dataLayer = window.dataLayer || ;
    function gtag(){dataLayer.push(arguments);}
    gtag(‘js’, new Date());
    gtag(‘config’, ‘G-XXXXXXXXXX’);
    < / script >
    % endif

  2. Add a line into lms / envs /production.py:
    GOOGLE_ANALYTICS_ACCOUNT_4 = AUTH_TOKENS.get(‘GOOGLE_ANALYTICS_ACCOUNT_4’)

  3. Add a line into /edx/etc/lms.yml:
    GOOGLE_ANALYTICS_ACCOUNT_4 : ‘PRO’

Because I am not good at JS . Maybe somebody could share a better way to support Google Analytics

I’ll give it a try the next time I work on my Test environment with Koa. Thanks.

In general I think @guangyaw 's approach will work. I used a similar approach with the instructions from this:

and then I was using the Indigo Tutor theme, so I just needed to copy the mentioned main.html into indigo/theme/lms/templates/main.html before editing it, and then re-rendering the Indigo theme. I did use the Universal Analytics snippet instead of the Google Analytics 4 one, but still I think the basic principle is sound, since editing main.html does seem to inject it everywhere.

1 Like

Just a note for everyone’s awareness… I used the above methods, but I recently got an email from Google saying "We will begin to sunset Universal Analytics in 2023"
and

Google Analytics 4 is our next-generation measurement solution, and is replacing Universal Analytics. On July 1, 2023, Universal Analytics properties will stop processing new hits. If you still rely on Universal Analytics, we recommend that you complete your move to Google Analytics 4.

So…Open edX’s lack of support for Google Analytics 4 will cause analytics to break next year…

That was the original reason for my post way back in January 2021. It was implicit when Google first announced Google Analytics 4. The clock is now ticking.