overleaf/services/web/app/views/_google_analytics.pug

59 lines
2.9 KiB
Text
Raw Normal View History

if (typeof(ExposedSettings.gaTokenV4) != "undefined" || typeof(ExposedSettings.gaToken) != "undefined")
script(type="text/javascript", nonce=scriptNonce, id="ga-loader" data-ga-token=ExposedSettings.gaToken data-ga-token-v4=ExposedSettings.gaTokenV4 data-cookie-domain=ExposedSettings.cookieDomain data-session-analytics-id=getSessionAnalyticsId()).
var gaSettings = document.querySelector('#ga-loader').dataset;
var gaid = gaSettings.gaTokenV4;
var gaToken = gaSettings.gaToken;
var cookieDomain = gaSettings.cookieDomain;
var sessionAnalyticsId = gaSettings.sessionAnalyticsId;
if(gaid) {
var additionalGaConfig = sessionAnalyticsId ? { 'user_id': sessionAnalyticsId } : {};
window.dataLayer = window.dataLayer || [];
function gtag(){
dataLayer.push(arguments);
}
gtag('js', new Date());
gtag('config', gaid, { 'anonymize_ip': true, ...additionalGaConfig });
}
if (gaToken) {
window.ga = window.ga || function () {
(window.ga.q = window.ga.q || []).push(arguments);
}, window.ga.l = 1 * new Date();
}
var loadGA = window.olLoadGA = function() {
if (gaid) {
var s = document.createElement('script');
s.setAttribute('async', 'async');
s.setAttribute('src', 'https://www.googletagmanager.com/gtag/js?id=' + gaid);
document.querySelector('head').append(s);
}
if (gaToken) {
(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','//www.google-analytics.com/analytics.js','ga');
ga('create', gaToken, cookieDomain.replace(/^\./, ""));
ga('set', 'anonymizeIp', true);
if (sessionAnalyticsId) {
ga('set', 'userId', sessionAnalyticsId);
}
ga('send', 'pageview');
}
};
// Check if consent given (features/cookie-banner)
var oaCookie = document.cookie.split('; ').find(function(cookie) {
return cookie.startsWith('oa=');
});
if(oaCookie) {
var oaCookieValue = oaCookie.split('=')[1];
if(oaCookieValue === '1') {
loadGA();
}
}
if typeof(ExposedSettings.gaTokenV4) === "undefined"
script(type="text/javascript", nonce=scriptNonce).
window.gtag = function() { console.log("would send to GA4", arguments) };
if typeof(ExposedSettings.gaToken) === "undefined"
script(type="text/javascript", nonce=scriptNonce).
window.ga = function() { console.log("would send to GA", arguments) };