const jsesc = require("jsesc"); const { escape } = require("html-escaper"); /** * Returns a string that represents the javascript that is required to insert the Matomo Analytics script * into a webpage. * @param {string} matomoUrl Domain name with tld to use with the Matomo Analytics script. * @param {string} siteId Site ID to use with the Matomo Analytics script. * @returns {string} HTML script tags to inject into page */ function getMatomoAnalyticsScript(matomoUrl, siteId) { let escapedMatomoUrlJS = jsesc(matomoUrl, { isScriptContext: true }); let escapedSiteIdJS = jsesc(siteId, { isScriptContext: true }); if (escapedMatomoUrlJS) { escapedMatomoUrlJS = escapedMatomoUrlJS.trim(); } if (escapedSiteIdJS) { escapedSiteIdJS = escapedSiteIdJS.trim(); } // Escape the domain url for use in an HTML attribute. let escapedMatomoUrlHTMLAttribute = escape(escapedMatomoUrlJS); // Escape the website id for use in an HTML attribute. let escapedSiteIdHTMLAttribute = escape(escapedSiteIdJS); return ` `; } module.exports = { getMatomoAnalyticsScript, };