3f08e8a24916a75afd749c152097f58344819a3a
max
  Thu Mar 2 07:11:02 2023 -0800
Ignore the previous commit in code review, that was all wrong, I was looking at the wrong analytics page and thought it was working. The GA4 system is entirely different and the code now spread out over five source files. Also: The part of cart.c that outputs the <HEAD> is getting worse and worse. To test the new code, set the analyticsKey to the value in the cgi-bin-max sandbox and go to https://analytics.google.com/analytics/web/#/p356133243/realtime. All staff should have access (if not email me or Lou or Hiram), refs #30725

diff --git src/hg/lib/googleAnalytics.c src/hg/lib/googleAnalytics.c
index 42ddf3e..6b6d62e 100644
--- src/hg/lib/googleAnalytics.c
+++ src/hg/lib/googleAnalytics.c
@@ -1,59 +1,66 @@
 /* Copyright (C) 2014 The Regents of the University of California 
  * See kent/LICENSE or http://genome.ucsc.edu/license/ for licensing information. */
 
 #include "common.h"
 #include "hgConfig.h"
 #include "hPrint.h"
 #include "googleAnalytics.h"
+#include "htmshell.h"
 
+void googleAnalyticsSetGa4Key()
+/* if the google analytics key is a GA4 key, set the variable in htmlshell */
+{
+char *analyticsKey = cfgOption("analyticsKey");
+if (analyticsKey && startsWith("G-", analyticsKey))
+    htmlSetGa4Key(analyticsKey);
+}
 
 void googleAnalytics()
 /* check for analytics configuration item and output google hooks if OK */
 {
 static boolean done = FALSE;
 
 if (done)
     return;
 
 done = TRUE;	/*	do not repeat this by mistake	*/
 
 char *analyticsKey = cfgOption("analyticsKey");
 
 // link tracking broke in Firefox once. In case this ever happens again, we can
 // now switch off outlink tracking quickly
 boolean trackClicks = cfgOptionBooleanDefault("analytics.trackClicks", TRUE);
 boolean trackButtons = cfgOptionBooleanDefault("analytics.trackButtons", TRUE);
 
 /*	if config is missing or empty, nothing happens here	*/
 if (isEmpty(analyticsKey))
     return;
 
-/* updated to Universal Analytics code 2014-06-19 */
-
+// new GA4 tags start with G- and should be used with the entirely new system called Google Tag Manager,
+// which is a different Javascript, new functions, totally different features.
 if (startsWith("G-", analyticsKey))
     {
     jsInlineF(
-        "<!-- Google tag (gtag.js) -->\n"
-        "<script async src=\"https://www.googletagmanager.com/gtag/js?id=%s\"></script>\n"
-        "<script>\n"
+        "// Google tag load (gtag.js)\n"
         "   window.dataLayer = window.dataLayer || [];\n"
         "   function gtag(){dataLayer.push(arguments);}\n"
         "   gtag('js', new Date()); gtag('config', '%s');\n"
-        "</script>\n", analyticsKey, analyticsKey);
+        "// Google tag load end\n", analyticsKey);
     return;
     }
+
 // replace analytics.js below with analytics_debug.js to activate ga debugging
 // It will log all events and all data that is sent to the javascript console, very handy
 jsInlineF(
 "  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){\n"
 "  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),\n"
 "  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)\n"
 "  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');\n"
 "  ga('create', '%s', 'auto');\n"
 "  ga('require', 'displayfeatures');\n"
 "  ga('set', 'transport', 'beacon');\n" // default to beacons. Old browser cannot use them, so ga will use an alternative.
 "  ga('send', 'pageview');\n"
 "\n"
 , analyticsKey);
 
 // to reduce the risk of breaking existing click handlers, only track links on hgc and hgTracks pages