overleaf/services/web/app/views/_google_analytics.pug
Jakob Ackermann 1ea97c6ae6 Merge pull request #7321 from overleaf/ta-ga-fallback
Fix window-ga Fallback

GitOrigin-RevId: 56b192acbae5c8ba23c63f6d4a7d7334d13b8c73
2022-04-05 12:21:37 +00:00

53 lines
2.6 KiB
Text

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).
var gaSettings = document.querySelector('#ga-loader').dataset;
var gaid = gaSettings.gaTokenV4;
var gaToken = gaSettings.gaToken;
var cookieDomain = gaSettings.cookieDomain;
if(gaid) {
window.dataLayer = window.dataLayer || [];
function gtag(){
dataLayer.push(arguments);
}
gtag('js', new Date());
gtag('config', gaid, { 'anonymize_ip': true });
}
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);
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) };