overleaf/services/web/app/views/_google_analytics.pug
Andrew Rumble 22e0e51796 Merge pull request #18464 from overleaf/ar-pass-analytics-id-to-google-analytics
Add analytics ID to google analytics when configuring

GitOrigin-RevId: e2372f68217583a90df427a853e62513f1535006
2024-05-30 08:04:07 +00:00

58 lines
2.9 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 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) };