Merge pull request #18464 from overleaf/ar-pass-analytics-id-to-google-analytics

Add analytics ID to google analytics when configuring

GitOrigin-RevId: e2372f68217583a90df427a853e62513f1535006
This commit is contained in:
Andrew Rumble 2024-05-29 10:25:50 +01:00 committed by Copybot
parent 1664722769
commit 22e0e51796
2 changed files with 10 additions and 2 deletions

View file

@ -331,6 +331,9 @@ async function analyticsIdMiddleware(req, res, next) {
// generate an `analyticsId` if needed
session.analyticsId = crypto.randomUUID()
}
res.locals.getSessionAnalyticsId = () => session.analyticsId
next()
}

View file

@ -1,16 +1,18 @@
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).
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 });
gtag('config', gaid, { 'anonymize_ip': true, ...additionalGaConfig });
}
if (gaToken) {
window.ga = window.ga || function () {
@ -31,6 +33,9 @@ if (typeof(ExposedSettings.gaTokenV4) != "undefined" || typeof(ExposedSettings.g
})(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');
}
};