Project

General

Profile

Support #13511

Google Analytics 4 tagging

Added by scott voth 12 months ago. Updated 11 months ago.

Status:
Resolved
Priority name:
Normal
Assignee:
Category name:
Analytics
Target version:
Start date:
2020-10-28
Due date:
% Done:

0%

Estimated time:

Description

I have been trying to figure out why a social media fellow's Google Analytics aren't working.

It seems that Universal Analytics has been replaced by Google Analytics 4. It is quite different. The Tracking ID (eg. UA-8990737-1) has been replaced by a Measurement ID (eg. G-34SMEHSLW4). I tried plugging the Measurement ID into Settings>>General and it took it, but it doesn't seem to work:

<script type="text/javascript">
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');

ga('create', 'UA-8990737-1', 'auto');
ga('set', 'dimension1', 1);
ga('send', 'pageview');
ga('create', 'G-34SMEHSLW4', 'auto', {'name': 'customGA'});
ga('customGA.send', 'pageview');

</script>

GA suggests a global tag like this:

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-34SMEHSLW4"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());

gtag('config', 'G-34SMEHSLW4');
&lt;/script&gt;

GA's Universal Analytics still works for old users, but it seems like a new user has to use the new Google Analytics 4. I found this out by creating a new Google Account and setting up GA. I didn't see a way to go back to the old version

Do you think we need to add a new script for the new version?

History

#1 Updated by Boone Gorges 12 months ago

  • Target version set to 1.17.7

Thanks for opening this ticket, Scott.

On my read, I think this means we'll need to move the existing implementation entirely over to Google Analytics 4. Here's the documentation: https://support.google.com/analytics/answer/9310895 I think this will mean the following:

1. Upgrade our global Commons GA account so that it's a GA 4 property https://support.google.com/analytics/answer/9744165?hl=en&ref_topic=9303319
2. Rewrite the existing google-analytics.php so that it uses the technique described at https://support.google.com/analytics/answer/9310895
3. I don't think we'll need to make changes to the user-facing config screen. It looks like you can provide either the UA property (legacy, with format UA-) or the new Google Analytics 4 property (G-) when setting up the gtag 'config' variable. So we should be able to keep the single field. Existing users will provide UA-, new users will provide G-, and we'll just use whatever they provide.

I'll do some more research and plan to have something by our next maintenance release.

#2 Updated by scott voth 12 months ago

Thanks Boone - I did find a workaround very well hidden in the process of creating a property. You can actually create Universal Analytics (Tracking) ID (UA-xxx) in addition to the Measurement ID. I tested and it seems to work. But it is extremely hard to spot - Thanks, Google.

But that is just a workaround.

#3 Updated by Boone Gorges 12 months ago

In https://github.com/cuny-academic-commons/cac/commit/927e04eb2a32585d89abae28f3d876284ded2f13 I've updated the Commons GA code in the following ways:

1. Add the GA4 collection code for the main Commons ID. Google recommends that existing UA sites keep the UA implementation alongside GA4, so I've left them both for the main property.
2. When I detect that a custom GA property identifier begins with UA-, I use the old technique for sending the ping to Google.
3. When I detect that a custom GA property identifier begins with G-, I use the new GA4 technique.

This is deployed on the production site, and it appears to be working. Scott, could you test a couple different scenarios and see what you can find?

Assuming everything works, the last question is what to do with the existing help text. We currently say:

<p class="description">Enter your Google Analytics Tracking ID for customized reports of traffic on your Commons site. Tracking IDs have the format UA-XXXXXXXX-X. <a href="https://help.commons.gc.cuny.edu/setting-up-google-analytics-on-your-commons-site/">Learn more.</a></p>

How should this be changed? Should we simply say that the format is now G-XXXXXXX? Should we mention both formats? Do we still call it a "Tracking ID"?

#4 Updated by scott voth 12 months ago

Thanks Boone - I will test. Since older users are still using Universal Analytics, I think we should say something like "Enter your Google Analytics Tracking ID (UA-XXXXXXX-X) or Measurement ID (G-XXXXXXX) for customized reports of traffic on your Commons site."

#5 Updated by scott voth 12 months ago

Hi Boone - Everything looks great. I had my meeting with Social Media Fellow and helped them set up some sites using Measurement ID and everything worked smoothly.

#6 Updated by Boone Gorges 12 months ago

  • Status changed from New to Staged for Production Release

Thanks for testing, Scott!

I've changed the language as suggested. https://github.com/cuny-academic-commons/cac/commit/5c719b46508981a69fe7709b0690b53d8b0c014a This will be part of the next maintenance release.

#7 Updated by Boone Gorges 11 months ago

  • Status changed from Staged for Production Release to Resolved

Also available in: Atom PDF