Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

improved geolocation data collection process

Blue Mouse0328840464b1
diff --git a/apps/production/src/analytics/analytics.controller.ts b/apps/production/src/analytics/analytics.controller.ts
index 02ab94c8b..066ba837e 100644
--- a/apps/production/src/analytics/analytics.controller.ts
+++ b/apps/production/src/analytics/analytics.controller.ts
@@ -9,7 +9,6 @@ import * as dayjs from 'dayjs'
import * as utc from 'dayjs/plugin/utc'
import * as dayjsTimezone from 'dayjs/plugin/timezone'
import { hash } from 'blake3'
-import timezones from 'countries-and-timezones'
import {
Controller,
Body,
@@ -847,16 +846,20 @@ export class AnalyticsController {
}
}
- const { city = 'NULL', region = 'NULL' } =
- this.analyticsService.getIPDetails(ip)
+ const {
+ city = 'NULL',
+ region = 'NULL',
+ country = 'NULL',
+ } = this.analyticsService.getGeoDetails(ip, eventsDTO.tz)
const ua = UAParser(userAgent)
const dv = ua.device.type || 'desktop'
const br = ua.browser.name
const os = ua.os.name
- // trying to get country from timezome, otherwise using CloudFlare's IP based country code as a fallback
+
+ // Using cf-ipcountry as a fallback. This is temporary until we stop using Cloudflare
const cc =
- timezones.getCountryForTimezone(eventsDTO.tz)?.id ||
+ country ||
(headers['cf-ipcountry'] === 'XX' ? 'NULL' : headers['cf-ipcountry'])
const dto = customLogDTO(
@@ -945,17 +948,19 @@ export class AnalyticsController {
)
}
- // trying to get country from timezome, otherwise using CloudFlare's IP based country code as a fallback
- const cc =
- timezones.getCountryForTimezone(logDTO.tz)?.id ||
- (headers['cf-ipcountry'] === 'XX' ? 'NULL' : headers['cf-ipcountry'])
-
- const { city = 'NULL', region = 'NULL' } =
- this.analyticsService.getIPDetails(ip)
+ const {
+ city = 'NULL',
+ region = 'NULL',
+ country = 'NULL',
+ } = this.analyticsService.getGeoDetails(ip, logDTO.tz)
const ua = UAParser(userAgent)
const dv = ua.device.type || 'desktop'
const br = ua.browser.name
const os = ua.os.name
+ // Using cf-ipcountry as a fallback. This is temporary until we stop using Cloudflare
+ const cc =
+ country ||
+ (headers['cf-ipcountry'] === 'XX' ? 'NULL' : headers['cf-ipcountry'])
const dto = analyticsDTO(
sessionHash,
@@ -1059,16 +1064,21 @@ export class AnalyticsController {
await this.analyticsService.processInteractionSD(sessionHash, logDTO.pid)
- const { city = 'NULL', region = 'NULL' } =
- this.analyticsService.getIPDetails(ip)
+ const {
+ city = 'NULL',
+ region = 'NULL',
+ country = 'NULL',
+ } = this.analyticsService.getGeoDetails(ip)
const ua = UAParser(userAgent)
const dv = ua.device.type || 'desktop'
const br = ua.browser.name
const os = ua.os.name
- // using CloudFlare's IP based country code
+ // Using cf-ipcountry as a fallback. This is temporary until we stop using Cloudflare
const cc =
- headers['cf-ipcountry'] === 'XX' ? 'NULL' : headers['cf-ipcountry']
+ country ||
+ (headers['cf-ipcountry'] === 'XX' ? 'NULL' : headers['cf-ipcountry'])
+
const dto = analyticsDTO(
sessionHash,
logDTO.pid,
diff --git a/apps/production/src/analytics/analytics.service.ts b/apps/production/src/analytics/analytics.service.ts
index 067889d56..fc21ba9a5 100644
--- a/apps/production/src/analytics/analytics.service.ts
+++ b/apps/production/src/analytics/analytics.service.ts
@@ -13,6 +13,7 @@ import * as _now from 'lodash/now'
import * as _values from 'lodash/values'
import * as _round from 'lodash/round'
import * as _filter from 'lodash/filter'
+import timezones from 'countries-and-timezones'
import * as dayjs from 'dayjs'
import * as utc from 'dayjs/plugin/utc'
import * as dayjsTimezone from 'dayjs/plugin/timezone'
@@ -820,17 +821,27 @@ export class AnalyticsService {
return result
}
- getIPDetails(ip: string): IPGeoDetails {
+ getGeoDetails(ip: string, tz?: string): IPGeoDetails {
const data = lookup.get(ip)
const country = data?.country?.names?.en
const city = data?.city?.names?.en
const region = data?.subdivisions?.[0]?.names?.en
+ if (country) {
+ return {
+ country,
+ city,
+ region,
+ }
+ }
+
+ const tzCountry = timezones.getCountryForTimezone(tz)?.id || null
+
return {
- country,
- city,
- region,
+ country: tzCountry,
+ city: null,
+ region: null,
}
}