Use IP-based geo

This commit is contained in:
Eric Bailey
2026-02-18 15:45:48 -06:00
parent c490c181c7
commit 15e1daec59
3 changed files with 13 additions and 6 deletions
+3 -3
View File
@@ -24,7 +24,7 @@ import {
import {type Metrics, metrics} from '#/analytics/metrics'
import * as refParams from '#/analytics/misc/refParams'
import * as env from '#/env'
import {useGeolocation} from '#/geolocation'
import {useGeolocationServiceResponse} from '#/geolocation/service'
import {device} from '#/storage'
export * as utils from '#/analytics/utils'
@@ -104,7 +104,7 @@ const Context = createContext<AnalyticsBaseContextType>({
referrerSrc: refParams.src,
referrerUrl: refParams.url,
},
geolocation: device.get(['mergedGeolocation']) || {
geolocation: device.get(['geolocationServiceResponse']) || {
countryCode: '',
regionCode: '',
},
@@ -137,7 +137,7 @@ export function AnalyticsContext({
}
}
const sessionId = useSessionId()
const geolocation = useGeolocation()
const geolocation = useGeolocationServiceResponse()
const parentContext = useContext(Context)
const childContext = useMemo(() => {
const combinedMetadata = {
+2
View File
@@ -7,6 +7,7 @@ import {
type MergeableMetadata,
type SessionMetadata,
} from '#/analytics/metadata'
import {getIPGeolocationString} from '#/geolocation/util'
/**
* Thin `useMemo` wrapper that marks the metadata as memoized and provides a
@@ -47,5 +48,6 @@ export function getAnalyticsHeaders() {
return {
'X-Bsky-Device-Id': getDeviceId(),
'X-Bsky-Session-Id': getSessionId(),
'X-Bsky-IP-Geolocation': getIPGeolocationString(),
}
}
+8 -3
View File
@@ -128,12 +128,17 @@ export function mergeGeolocations(
}
/**
* Get's the merged geolocation as a string in the format of
* Get's the IP-based geolocation as a string in the format of
* "countryCode-regionCode", or just "countryCode" if regionCode is not
* available.
*
* IMPORTANT: this method should only return IP-based data, not the user's GPS
* based data. IP-based data we can already infer from requests, but for
* consistency between frontend and backend, we sometimes want to share the
* value we have on the frontend with the backend.
*/
export function getGeolocationString() {
const geo = device.get(['mergedGeolocation'])
export function getIPGeolocationString() {
const geo = device.get(['geolocationServiceResponse'])
if (!geo) return
const {countryCode, regionCode} = geo
if (countryCode) {