Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Add confidence to geolocation data #26

Merged
merged 4 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
const DEFAULT_MMDB_FILE_NAME = 'GeoLite2-City-Test.mmdb'

async function resetMetaWithMmdb(
transformResult = (res: City) => res as Record<string, any>,

Check warning on line 14 in index.test.ts

View workflow job for this annotation

GitHub Actions / Code quality

Unexpected any. Specify a different type
file = DEFAULT_MMDB_FILE_NAME
): Promise<PluginMeta> {
const mmdb = await Reader.open(join(__dirname, file))
Expand All @@ -30,12 +30,14 @@
expect(event!.properties).toEqual(
expect.objectContaining({
$geoip_city_name: 'Linköping',
$geoip_city_confidence: null,
$geoip_country_name: 'Sweden',
$geoip_country_code: 'SE',
$geoip_continent_name: 'Europe',
$geoip_continent_code: 'EU',
$geoip_latitude: 58.4167,
$geoip_longitude: 15.6167,
$geoip_accuracy_radius: 76,
$geoip_time_zone: 'Europe/Stockholm',
$geoip_subdivision_1_code: 'E',
$geoip_subdivision_1_name: 'Östergötland County',
Expand Down Expand Up @@ -77,7 +79,7 @@

test('person props default to null if no values present', async () => {
const removeCityNameFromLookupResult = (res: City) => {
const { city, ...remainingResult } = res

Check warning on line 82 in index.test.ts

View workflow job for this annotation

GitHub Actions / Code quality

'city' is assigned a value but never used
return remainingResult
}
const event = await processEvent(
Expand All @@ -86,12 +88,14 @@
)
expect(event!.properties!.$set).toEqual({
$geoip_city_name: null, // default to null
$geoip_city_confidence: null,
$geoip_country_name: 'Sweden',
$geoip_country_code: 'SE',
$geoip_continent_name: 'Europe',
$geoip_continent_code: 'EU',
$geoip_latitude: 58.4167,
$geoip_longitude: 15.6167,
$geoip_accuracy_radius: 76,
$geoip_time_zone: 'Europe/Stockholm',
$geoip_subdivision_1_code: 'E',
$geoip_subdivision_1_name: 'Östergötland County',
Expand All @@ -101,12 +105,14 @@
})
expect(event!.properties!.$set_once).toEqual({
$initial_geoip_city_name: null, // default to null
$initial_geoip_city_confidence: null,
$initial_geoip_country_name: 'Sweden',
$initial_geoip_country_code: 'SE',
$initial_geoip_continent_name: 'Europe',
$initial_geoip_continent_code: 'EU',
$initial_geoip_latitude: 58.4167,
$initial_geoip_longitude: 15.6167,
$initial_geoip_accuracy_radius: 76,
$initial_geoip_time_zone: 'Europe/Stockholm',
$initial_geoip_subdivision_1_code: 'E',
$initial_geoip_subdivision_1_name: 'Östergötland County',
Expand Down
6 changes: 6 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Plugin } from '@posthog/plugin-scaffold'

const ONE_DAY = 60 * 60 * 24 // 24h in seconds

Check warning on line 3 in index.ts

View workflow job for this annotation

GitHub Actions / Code quality

'ONE_DAY' is assigned a value but never used

const defaultLocationSetProps = {
$geoip_city_name: null,
$geoip_city_confidence: null,
$geoip_subdivision_2_name: null,
$geoip_subdivision_2_code: null,
$geoip_subdivision_1_name: null,
Expand All @@ -15,11 +16,13 @@
$geoip_postal_code: null,
$geoip_latitude: null,
$geoip_longitude: null,
$geoip_accuracy_radius: null,
$geoip_time_zone: null,
}

const defaultLocationSetOnceProps = {
$initial_geoip_city_name: null,
$initial_geoip_city_confidence: null,
$initial_geoip_subdivision_2_name: null,
$initial_geoip_subdivision_2_code: null,
$initial_geoip_subdivision_1_name: null,
Expand All @@ -31,6 +34,7 @@
$initial_geoip_postal_code: null,
$initial_geoip_latitude: null,
$initial_geoip_longitude: null,
$initial_geoip_accuracy_radius: null,
$initial_geoip_time_zone: null,
}

Expand All @@ -47,9 +51,10 @@
}
const response = await geoip.locate(ip)
if (response) {
const location: Record<string, any> = {}

Check warning on line 54 in index.ts

View workflow job for this annotation

GitHub Actions / Code quality

Unexpected any. Specify a different type
if (response.city) {
location['city_name'] = response.city.names?.en
location['city_confidence'] = response.city.confidence ?? null
}
if (response.country) {
location['country_name'] = response.country.names?.en
Expand All @@ -65,6 +70,7 @@
if (response.location) {
location['latitude'] = response.location?.latitude
location['longitude'] = response.location?.longitude
location['accuracy_radius'] = response.location?.accuracyRadius
location['time_zone'] = response.location?.timeZone
}
if (response.subdivisions) {
Expand Down
Loading