@naturalcycles/js-lib
Version:
Standard library for universal (browser + Node.js) javascript
60 lines (59 loc) • 2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.loadGTag = loadGTag;
exports.loadGTM = loadGTM;
exports.loadHotjar = loadHotjar;
const js_lib_1 = require("@naturalcycles/js-lib");
const script_util_1 = require("./script.util");
/* eslint-disable unicorn/prefer-global-this */
/**
* Pass enabled = false to only init window.gtag, but not load actual gtag script (e.g in dev mode).
*/
async function loadGTag(gtagId, enabled = true) {
if ((0, js_lib_1.isServerSide)())
return;
window.dataLayer ||= [];
window.gtag ||= function gtag() {
// biome-ignore lint/complexity/useArrowFunction: ok
// biome-ignore lint/style/noArguments: ok
window.dataLayer.push(arguments);
};
window.gtag('js', new Date());
window.gtag('config', gtagId);
if (!enabled)
return;
await (0, script_util_1.loadScript)(`https://www.googletagmanager.com/gtag/js?id=${gtagId}`);
}
async function loadGTM(gtmId, enabled = true) {
if ((0, js_lib_1.isServerSide)())
return;
window.dataLayer ||= [];
window.dataLayer.push({
'gtm.start': Date.now(),
event: 'gtm.js',
});
if (!enabled)
return;
await (0, script_util_1.loadScript)(`https://www.googletagmanager.com/gtm.js?id=${gtmId}`);
}
function loadHotjar(hjid) {
if ((0, js_lib_1.isServerSide)())
return;
;
((h, o, t, j, a, r) => {
h.hj =
h.hj ||
function hj() {
// biome-ignore lint/style/noArguments: ok
;
(h.hj.q = h.hj.q || []).push(arguments);
};
h._hjSettings = { hjid, hjsv: 6 };
a = o.querySelectorAll('head')[0];
r = o.createElement('script');
r.async = 1;
r.src = t + h._hjSettings.hjid + j + h._hjSettings.hjsv;
a.append(r);
})(window, document, 'https://static.hotjar.com/c/hotjar-', '.js?sv=');
/* eslint-enable */
}