-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.ts
119 lines (107 loc) · 4.87 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import { Plugin } from '@posthog/plugin-scaffold'
const props = {
city_name: null,
city_confidence: null,
subdivision_2_name: null,
subdivision_2_code: null,
subdivision_1_name: null,
subdivision_1_code: null,
country_name: null,
country_code: null,
continent_name: null,
continent_code: null,
postal_code: null,
latitude: null,
longitude: null,
accuracy_radius: null,
time_zone: null,
}
const defaultLocationSetProps = Object.entries(props).reduce((acc, [key]) => {
acc[`$geoip_${key}`] = null
return acc
}, {} as Record<string, any>)
const defaultLocationSetOnceProps = Object.entries(props).reduce((acc, [key]) => {
acc[`$initial_geoip_${key}`] = null
return acc
}, {} as Record<string, any>)
const plugin: Plugin = {
processEvent: async (event, { geoip }) => {
if (!geoip) {
throw new Error('This PostHog version does not have GeoIP capabilities! Upgrade to PostHog 1.24.0 or later')
}
let ip = event.properties?.$ip || event.ip
if (ip && !event.properties?.$geoip_disable) {
ip = String(ip)
if (ip === '127.0.0.1') {
ip = '13.106.122.3' // Spoofing an Australian IP address for local development
}
const response = await geoip.locate(ip)
if (response) {
const location: Record<string, any> = {}
if (response.city) {
location['city_name'] = response.city.names?.en
if (typeof response.city.confidence === 'number') {
// NOTE: Confidence is part of the enterprise maxmind DB, not typically installed
location['city_confidence'] = response.city.confidence
}
}
if (response.country) {
location['country_name'] = response.country.names?.en
location['country_code'] = response.country.isoCode
if (typeof response.country.confidence === 'number') {
// NOTE: Confidence is part of the enterprise maxmind DB, not typically installed
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
if (typeof response.postal.confidence === 'number') {
// NOTE: Confidence is part of the enterprise maxmind DB, not typically installed
location['postal_code_confidence'] = response.postal.confidence ?? null
}
}
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) {
for (const [index, subdivision] of response.subdivisions.entries()) {
location[`subdivision_${index + 1}_code`] = subdivision.isoCode
location[`subdivision_${index + 1}_name`] = subdivision.names?.en
if (typeof subdivision.confidence === 'number') {
// NOTE: Confidence is part of the enterprise maxmind DB, not typically installed
location[`subdivision_${index + 1}_confidence`] = subdivision.confidence ?? null
}
}
}
if (!event.properties) {
event.properties = {}
}
if (!event.properties.$set) {
event.properties.$set = {}
}
if (!event.properties.$set_once) {
event.properties.$set_once = {}
}
event.properties.$set = { ...defaultLocationSetProps, ...(event.properties.$set ?? {}) }
event.properties.$set_once = {
...defaultLocationSetOnceProps,
...(event.properties.$set_once ?? {}),
}
for (const [key, value] of Object.entries(location)) {
event.properties[`$geoip_${key}`] = value
event.properties.$set![`$geoip_${key}`] = value
event.properties.$set_once![`$initial_geoip_${key}`] = value
}
}
}
return event
},
}
module.exports = plugin