091025a3f66bf4635fe4d7c5b457160e78914af3 max Wed Oct 7 05:33:42 2020 -0700 making analytics link tracking a bit more careful, by only modifying external inks, also fixing the indentation, refs #21916 diff --git src/hg/lib/googleAnalytics.c src/hg/lib/googleAnalytics.c index 7c0413f..3ef2a5d 100644 --- src/hg/lib/googleAnalytics.c +++ src/hg/lib/googleAnalytics.c @@ -35,53 +35,49 @@ " ga('set', 'transport', 'beacon');\n" " ga('send', 'pageview');\n" "\n" , analyticsKey); // to reduce the risk of breaking existing click handlers, only track links on hgc and hgTracks pages char* scriptName = getenv("SCRIPT_NAME"); if (!scriptName) return; char* cgiName = basename(scriptName); if (sameWord(cgiName, "hgc") || sameWord(cgiName, "hgTracks")) { // see https://support.google.com/analytics/answer/1136920?hl=en jsInlineF( -" function anchorClicked(ev) {\n" + " function onExtAnchorClick(ev) {\n" " /* user clicked an anchor: if external link, send event to Google analytics and navigate to the link */\n" -" var isExternal = (ev.currentTarget.target.toLowerCase()==='_blank');\n" " var url = ev.currentTarget.href;\n" " var hostname = null;\n" " if (typeof URL===undefined) {\n" " hostname = url.split('//')[1].split('/')[0];\n" // for MSIE " } else {\n" " var urlObj = new URL(url);\n" " hostname = urlObj.hostname;\n" " if (hostname.indexOf('.ncbi.')!==-1)\n" // for NCBI, we keep the first part of the pathname " hostname = hostname+'/'+urlObj.pathname.split('/')[1];\n" " }\n" -" if (isExternal) {\n" " ga('send', 'event', 'outbound', 'click', hostname, url,\n" " { 'transport': 'beacon', 'hitCallback': function(){window.open(url);} });\n" -" } else {\n" -" document.location=url;\n" -" }\n" " return false;\n" " }" " $(document).ready(function() {\n" " if (!window.ga || ga.loaded)\n" // When using an Adblocker, the ga object does not exist " return;\n" " var anchors = document.getElementsByTagName('a');\n" " for (var i = 0; i < anchors.length; i++) { \n" " var a = anchors[i];\n" -" if (a.attributes.href && a.attributes.href.value!=='#')\n" // do not run on javascript links -" a.onclick = anchorClicked;" // addEventHandler would not work here, the default click stops propagation. + " var isExternal = (a.target.toLowerCase()==='_blank');\n" + " if (a.attributes.href && a.attributes.href.value!=='#' && isExternal)\n" // do not run on javascript links + " a.onclick = onExtAnchorClick;" // addEventHandler would not work here, the default click stops propagation. " };\n" " // on hgTracks: send an event with the current db, so we can report popularity of assemblies to NIH\n" " if (typeof hgTracks !== undefined)\n" " ga('send', 'event', 'hgTracks', 'load', getDb());\n" " });" ); } }