fix ip getter

pull/1644/head
Sergei Meza 2022-10-18 15:38:21 +09:00
parent d0f5c6dec7
commit 0336f41e12
2 changed files with 25 additions and 58 deletions

View File

@ -45,7 +45,8 @@ export async function getSession(req) {
throw new Error(`Website not found: ${website_uuid}`);
}
const { userAgent, browser, os, ip, country, device } = await getClientInfo(req, payload);
const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
const { userAgent, browser, os, country, device } = await getClientInfo(req, payload);
let session_uuid = uuid(websiteId, hostname, ip, userAgent);
if (process.env.CROSSDOMAIN_TRACKING) {

View File

@ -71,7 +71,6 @@
let currentUrl = `${pathname}${search}`;
let currentRef = document.referrer;
let cache;
let ip;
/* Collect metrics */
@ -81,40 +80,9 @@
screen,
language,
url: currentUrl,
ip,
});
const getClientIPAddress = () => {
return new Promise((res, rej) => {
if (window.ip || ip) {
ip = ip || window.ip;
res(ip);
return;
}
console.log('fetching user ip');
fetch('https://api64.ipify.org/?format=json')
.then(res => res.json())
.then(data => {
ip = data.ip;
window.ip = data.ip;
res(ip);
return;
})
.catch(err => {
console.error(err);
rej(err);
});
});
};
const collect = (type, payload) => {
if (!ip) {
setTimeout(() => {
collect(type, payload);
}, 100);
return;
}
if (trackingDisabled()) return;
return fetch(endpoint, {
@ -226,7 +194,6 @@
};
/* Global */
getClientIPAddress().then(() => {
if (!window.umami) {
const umami = eventValue => trackEvent(eventValue);
umami.trackView = trackView;
@ -259,5 +226,4 @@
update();
}
});
})(window);