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 {type Metrics, metrics} from '#/analytics/metrics'
import * as refParams from '#/analytics/misc/refParams' import * as refParams from '#/analytics/misc/refParams'
import * as env from '#/env' import * as env from '#/env'
import {useGeolocation} from '#/geolocation' import {useGeolocationServiceResponse} from '#/geolocation/service'
import {device} from '#/storage' import {device} from '#/storage'
export * as utils from '#/analytics/utils' export * as utils from '#/analytics/utils'
@@ -104,7 +104,7 @@ const Context = createContext<AnalyticsBaseContextType>({
referrerSrc: refParams.src, referrerSrc: refParams.src,
referrerUrl: refParams.url, referrerUrl: refParams.url,
}, },
geolocation: device.get(['mergedGeolocation']) || { geolocation: device.get(['geolocationServiceResponse']) || {
countryCode: '', countryCode: '',
regionCode: '', regionCode: '',
}, },
@@ -137,7 +137,7 @@ export function AnalyticsContext({
} }
} }
const sessionId = useSessionId() const sessionId = useSessionId()
const geolocation = useGeolocation() const geolocation = useGeolocationServiceResponse()
const parentContext = useContext(Context) const parentContext = useContext(Context)
const childContext = useMemo(() => { const childContext = useMemo(() => {
const combinedMetadata = { const combinedMetadata = {
+2
View File
@@ -7,6 +7,7 @@ import {
type MergeableMetadata, type MergeableMetadata,
type SessionMetadata, type SessionMetadata,
} from '#/analytics/metadata' } from '#/analytics/metadata'
import {getIPGeolocationString} from '#/geolocation/util'
/** /**
* Thin `useMemo` wrapper that marks the metadata as memoized and provides a * Thin `useMemo` wrapper that marks the metadata as memoized and provides a
@@ -47,5 +48,6 @@ export function getAnalyticsHeaders() {
return { return {
'X-Bsky-Device-Id': getDeviceId(), 'X-Bsky-Device-Id': getDeviceId(),
'X-Bsky-Session-Id': getSessionId(), '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 * "countryCode-regionCode", or just "countryCode" if regionCode is not
* available. * 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() { export function getIPGeolocationString() {
const geo = device.get(['mergedGeolocation']) const geo = device.get(['geolocationServiceResponse'])
if (!geo) return if (!geo) return
const {countryCode, regionCode} = geo const {countryCode, regionCode} = geo
if (countryCode) { if (countryCode) {