summaryrefslogtreecommitdiffstats
path: root/server/google-analytics.js
blob: 57ae7b754e26ec80791f9465144aab8bff42241e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
const jsesc = require("jsesc");
const { escape } = require("html-escaper");

/**
 * Returns a string that represents the javascript that is required to insert the Google Analytics scripts
 * into a webpage.
 * @param {string} tagId Google UA/G/AW/DC Property ID to use with the Google Analytics script.
 * @returns {string} HTML script tags to inject into page
 */
function getGoogleAnalyticsScript(tagId) {
    let escapedTagIdJS = jsesc(tagId, { isScriptContext: true });

    if (escapedTagIdJS) {
        escapedTagIdJS = escapedTagIdJS.trim();
    }

    // Escape the tag ID for use in an HTML attribute.
    let escapedTagIdHTMLAttribute = escape(tagId);

    return `
        <script async src="https://www.googletagmanager.com/gtag/js?id=${escapedTagIdHTMLAttribute}"></script>
        <script>window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date());gtag('config', '${escapedTagIdJS}'); </script>
    `;
}

module.exports = {
    getGoogleAnalyticsScript,
};