diff --git a/plugins/tiddlywiki/googleanalytics/googleanalytics.js b/plugins/tiddlywiki/googleanalytics/googleanalytics.js index 1634e465b..229ec6a30 100644 --- a/plugins/tiddlywiki/googleanalytics/googleanalytics.js +++ b/plugins/tiddlywiki/googleanalytics/googleanalytics.js @@ -17,7 +17,7 @@ exports.name = "google-analytics"; exports.platforms = ["browser"]; exports.synchronous = true; -var CONFIG_CONSENT_REQUIRED_TITLE = "$:/config/cookie-consent-required", +var CONFIG_CONSENT_REQUIRED_TITLE = "$:/config/cookie-consent-required", // "yes" or "no" (the default) CONSENT_TITLE = "$:/state/consent-banner/accepted"; // "": undeclared, "yes": accepted, "no": declined exports.startup = function() { @@ -25,15 +25,16 @@ exports.startup = function() { initialiseGoogleAnalytics = function() { console.log("Initialising Google Analytics"); hasInitialised = true; - var gaAccount = $tw.wiki.getTiddlerText("$:/GoogleAnalyticsAccount","").replace(/\n/g,""), - gaDomain = $tw.wiki.getTiddlerText("$:/GoogleAnalyticsDomain","auto").replace(/\n/g,""); - // Using ga "isogram" function - (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','https://www.google-analytics.com/analytics.js','ga'); - ga('create',gaAccount,gaDomain); - ga('send','pageview'); + var gaMeasurementID = $tw.wiki.getTiddlerText("$:/GoogleAnalyticsMeasurementID","").replace(/\n/g,""); + var url ="https://www.googletagmanager.com/gtag/js?id=" + gaMeasurementID; + window.dataLayer = this.window.dataLayer || []; + window.gtag = function() { window.dataLayer?.push(arguments); }; + window.gtag("js",new Date()); + window.gtag("config",gaMeasurementID); + const scriptElement = window.document.createElement("script"); + scriptElement.async = true; + scriptElement.src = url; + window.document.head.appendChild(scriptElement); }; // Initialise now if consent isn't required if($tw.wiki.getTiddlerText(CONFIG_CONSENT_REQUIRED_TITLE) !== "yes") { diff --git a/plugins/tiddlywiki/googleanalytics/readme.tid b/plugins/tiddlywiki/googleanalytics/readme.tid index f10803be1..47504412c 100644 --- a/plugins/tiddlywiki/googleanalytics/readme.tid +++ b/plugins/tiddlywiki/googleanalytics/readme.tid @@ -1,6 +1,6 @@ title: $:/plugins/tiddlywiki/googleanalytics/readme -This plugin enables you to use Google Analytics to track access to your online TiddlyWiki document. Based upon the [[official Google code|https://developers.google.com/analytics/devguides/collection/analyticsjs]]. +This plugin enables you to use Google Analytics to track access to your online TiddlyWiki document. By default, the user is not asked for permission before initialising Google Analytics. This plugin also optionally integrates with the "Consent Banner" plugin (also found in the official plugin library) so that Google Analytics is not initialised until the user grants explicit permission. diff --git a/plugins/tiddlywiki/googleanalytics/settings.tid b/plugins/tiddlywiki/googleanalytics/settings.tid index d88fe0579..976b052c5 100644 --- a/plugins/tiddlywiki/googleanalytics/settings.tid +++ b/plugins/tiddlywiki/googleanalytics/settings.tid @@ -1,7 +1,6 @@ title: $:/plugins/tiddlywiki/googleanalytics/settings -You have only two value to set, only first is mandatory: +You have only two value to set, only the first is mandatory: -# ''[[Google Analytics Account|$:/GoogleAnalyticsAccount]]'': (mandatory) a code of the form `UA-XXXXXX-XX` where X are digits
<$edit-text tiddler="$:/GoogleAnalyticsAccount" default="" tag="input"/> +# ''[[Google Analytics Measurement ID|$:/GoogleAnalyticsMeasurementID]]'': (mandatory) a code of the form `G-XXXXXXXXXX` where X are digits or uppercase letters
<$edit-text tiddler="$:/GoogleAnalyticsMeasurementID" default="" tag="input"/> -# ''[[Google Analytics Domain|$:/GoogleAnalyticsDomain]]'': (optional) the website URL where the TiddlyWiki file is published. Defaults to `auto` if not set.
<$edit-text tiddler="$:/GoogleAnalyticsDomain" default="" tag="input"/> diff --git a/plugins/tiddlywiki/googleanalytics/usage.tid b/plugins/tiddlywiki/googleanalytics/usage.tid index d6572f27d..1fbd5fc2f 100644 --- a/plugins/tiddlywiki/googleanalytics/usage.tid +++ b/plugins/tiddlywiki/googleanalytics/usage.tid @@ -7,7 +7,7 @@ If you don't already have an account: # Go to the Google Analytics website: http://www.google.com/analytics/ # Click the ''Access Google Analytics'' button and follow instructions to set up your account # Enter the URL where the wiki is hosted -# Note the Tracking ID for this domain of the form `UA-XXXXXX-XX` +# Note the Tracking ID for this domain of the form `G-XXXXXXXXXX` !! Install the plugin on your local copy of the TiddlyWiki @@ -20,5 +20,5 @@ If you don't already have an account: !! Upload the new version of your TiddlyWiki -# Upload the saved TiddlyWiki to TiddlySpot, GitHub, GitLab or other web host +# Upload the saved TiddlyWiki to Tiddlyhost, GitHub, GitLab or other web host # Return to your Google Analytics page to check that your site is being tracked