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

feat: Add confidence to all values #27

Merged
merged 2 commits into from
Aug 20, 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
80 changes: 46 additions & 34 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 Down Expand Up @@ -79,47 +79,59 @@

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(
{ ...createPageview(), ip: '89.160.20.129' },
await resetMetaWithMmdb(removeCityNameFromLookupResult)
)
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',
$geoip_subdivision_2_code: null,
$geoip_subdivision_2_name: null,
$geoip_postal_code: null,
})
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',
$initial_geoip_subdivision_2_code: null,
$initial_geoip_subdivision_2_name: null,
$initial_geoip_postal_code: null,
})
expect(event!.properties!.$set).toMatchInlineSnapshot(`
Object {
"$geoip_accuracy_radius": 76,
"$geoip_city_confidence": null,
"$geoip_city_name": null,
"$geoip_continent_code": "EU",
"$geoip_continent_name": "Europe",
"$geoip_country_code": "SE",
"$geoip_country_confidence": null,
"$geoip_country_name": "Sweden",
"$geoip_latitude": 58.4167,
"$geoip_longitude": 15.6167,
"$geoip_postal_code": null,
"$geoip_postal_code_confidence": null,
"$geoip_subdivision_1_code": "E",
"$geoip_subdivision_1_confidence": null,
"$geoip_subdivision_1_name": "Östergötland County",
"$geoip_subdivision_2_code": null,
"$geoip_subdivision_2_confidence": null,
"$geoip_subdivision_2_name": null,
"$geoip_time_zone": "Europe/Stockholm",
}
`)
expect(event!.properties!.$set_once).toMatchInlineSnapshot(`
Object {
"$initial_geoip_accuracy_radius": 76,
"$initial_geoip_city_confidence": null,
"$initial_geoip_city_name": null,
"$initial_geoip_continent_code": "EU",
"$initial_geoip_continent_name": "Europe",
"$initial_geoip_country_code": "SE",
"$initial_geoip_country_confidence": null,
"$initial_geoip_country_name": "Sweden",
"$initial_geoip_latitude": 58.4167,
"$initial_geoip_longitude": 15.6167,
"$initial_geoip_postal_code": null,
"$initial_geoip_postal_code_confidence": null,
"$initial_geoip_subdivision_1_code": "E",
"$initial_geoip_subdivision_1_confidence": null,
"$initial_geoip_subdivision_1_name": "Östergötland County",
"$initial_geoip_subdivision_2_code": null,
"$initial_geoip_subdivision_2_confidence": null,
"$initial_geoip_subdivision_2_name": null,
"$initial_geoip_time_zone": "Europe/Stockholm",
}
`)
})

test('error is thrown if meta.geoip is not provided', async () => {
Expand Down
70 changes: 35 additions & 35 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,39 @@
import { Plugin } from '@posthog/plugin-scaffold'

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

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,
$geoip_subdivision_1_code: null,
$geoip_country_name: null,
$geoip_country_code: null,
$geoip_continent_name: null,
$geoip_continent_code: null,
$geoip_postal_code: null,
$geoip_latitude: null,
$geoip_longitude: null,
$geoip_accuracy_radius: null,
$geoip_time_zone: null,
const props = {
city_name: null,
city_confidence: null,
subdivision_2_name: null,
subdivision_2_code: null,
subdivision_2_confidence: null,
subdivision_1_name: null,
subdivision_1_code: null,
subdivision_1_confidence: null,
country_name: null,
country_code: null,
country_confidence: null,
continent_name: null,
continent_code: null,
postal_code: null,
postal_code_confidence: null,
latitude: null,
longitude: null,
accuracy_radius: null,
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,
$initial_geoip_subdivision_1_code: null,
$initial_geoip_country_name: null,
$initial_geoip_country_code: null,
$initial_geoip_continent_name: null,
$initial_geoip_continent_code: null,
$initial_geoip_postal_code: null,
$initial_geoip_latitude: null,
$initial_geoip_longitude: null,
$initial_geoip_accuracy_radius: null,
$initial_geoip_time_zone: null,
}
const defaultLocationSetProps = Object.entries(props).reduce((acc, [key]) => {
acc[`$geoip_${key}`] = null
return acc
}, {} as Record<string, any>)

Check warning on line 28 in index.ts

View workflow job for this annotation

GitHub Actions / Code quality

Unexpected any. Specify a different type


const defaultLocationSetOnceProps = Object.entries(props).reduce((acc, [key]) => {
acc[`$initial_geoip_${key}`] = null
return acc
}, {} as Record<string, any>)

Check warning on line 34 in index.ts

View workflow job for this annotation

GitHub Actions / Code quality

Unexpected any. Specify a different type



const plugin: Plugin = {
processEvent: async (event, { geoip }) => {
Expand All @@ -51,7 +48,7 @@
}
const response = await geoip.locate(ip)
if (response) {
const location: Record<string, any> = {}

Check warning on line 51 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
Expand All @@ -59,13 +56,15 @@
if (response.country) {
location['country_name'] = response.country.names?.en
location['country_code'] = response.country.isoCode
location['country_confidence'] = response.country.confidence ?? null
}
if (response.continent) {
location['continent_name'] = response.continent.names?.en
location['continent_code'] = response.continent.code
}
if (response.postal) {
location['postal_code'] = response.postal.code
location['postal_code_confidence'] = response.postal.confidence ?? null
}
if (response.location) {
location['latitude'] = response.location?.latitude
Expand All @@ -77,6 +76,7 @@
for (const [index, subdivision] of response.subdivisions.entries()) {
location[`subdivision_${index + 1}_code`] = subdivision.isoCode
location[`subdivision_${index + 1}_name`] = subdivision.names?.en
location[`subdivision_${index + 1}_confidence`] = subdivision.confidence ?? null
}
}

Expand Down
Loading