2013-11-25 23:14:12 +00:00
|
|
|
/*\
|
|
|
|
title: $:/plugins/tiddlywiki/googleanalytics/googleanalytics.js
|
|
|
|
type: application/javascript
|
|
|
|
module-type: startup
|
|
|
|
|
|
|
|
Runs Google Analytics with the account number in the tiddler `$:/GoogleAnalyticsAccount` and the domain name in `$:/GoogleAnalyticsDomain`
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
2014-05-03 15:32:18 +00:00
|
|
|
// Export name and synchronous status
|
|
|
|
exports.name = "google-analytics";
|
2014-05-03 17:07:20 +00:00
|
|
|
exports.platforms = ["browser"];
|
2014-05-03 15:32:18 +00:00
|
|
|
exports.synchronous = true;
|
|
|
|
|
2023-06-22 07:48:48 +00:00
|
|
|
var CONFIG_CONSENT_REQUIRED_TITLE = "$:/config/cookie-consent-required", // "yes" or "no" (the default)
|
2020-06-22 10:31:54 +00:00
|
|
|
CONSENT_TITLE = "$:/state/consent-banner/accepted"; // "": undeclared, "yes": accepted, "no": declined
|
|
|
|
|
2013-11-25 23:14:12 +00:00
|
|
|
exports.startup = function() {
|
2020-06-22 10:31:54 +00:00
|
|
|
var hasInitialised = false,
|
|
|
|
initialiseGoogleAnalytics = function() {
|
|
|
|
console.log("Initialising Google Analytics");
|
|
|
|
hasInitialised = true;
|
2023-06-22 07:48:48 +00:00
|
|
|
var gaMeasurementID = $tw.wiki.getTiddlerText("$:/GoogleAnalyticsMeasurementID","").replace(/\n/g,"");
|
|
|
|
var url ="https://www.googletagmanager.com/gtag/js?id=" + gaMeasurementID;
|
|
|
|
window.dataLayer = 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);
|
2020-06-22 10:31:54 +00:00
|
|
|
};
|
|
|
|
// Initialise now if consent isn't required
|
|
|
|
if($tw.wiki.getTiddlerText(CONFIG_CONSENT_REQUIRED_TITLE) !== "yes") {
|
|
|
|
initialiseGoogleAnalytics();
|
|
|
|
} else {
|
|
|
|
// Or has been granted already
|
|
|
|
if($tw.wiki.getTiddlerText(CONSENT_TITLE) === "yes") {
|
|
|
|
initialiseGoogleAnalytics();
|
|
|
|
} else {
|
|
|
|
// Or when our config tiddler changes
|
|
|
|
$tw.wiki.addEventListener("change",function(changes) {
|
|
|
|
if(changes[CONSENT_TITLE]) {
|
|
|
|
if(!hasInitialised && $tw.wiki.getTiddlerText(CONSENT_TITLE) === "yes") {
|
|
|
|
initialiseGoogleAnalytics();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2013-11-25 23:14:12 +00:00
|
|
|
};
|
|
|
|
|
2020-06-22 10:31:54 +00:00
|
|
|
|
|
|
|
|
2013-11-25 23:14:12 +00:00
|
|
|
})();
|